ExecuteNonQuery: Connection 属性尚未初始化。
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSy...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ADO.net
{
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using (SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB1.mdf;Integrated Security=True;User Instance=True"))
{
connection.Open();
Console.Write("数据库连接成功!");
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Insert into Information(Id,Name) values(4,'代笔')";
cmd.ExecuteNonQuery();
Console.WriteLine("插入成功!");
}
}
Console.ReadKey();
}
}
} 展开
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ADO.net
{
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using (SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB1.mdf;Integrated Security=True;User Instance=True"))
{
connection.Open();
Console.Write("数据库连接成功!");
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Insert into Information(Id,Name) values(4,'代笔')";
cmd.ExecuteNonQuery();
Console.WriteLine("插入成功!");
}
}
Console.ReadKey();
}
}
} 展开
3个回答
2012-08-25
展开全部
using (SqlCommand cmd = new SqlCommand())
{cmd.Connection=connection;
cmd.CommandText = "Insert into Information(Id,Name) values(4,'代笔')";
cmd.ExecuteNonQuery();
Console.WriteLine("插入成功!");
}
{cmd.Connection=connection;
cmd.CommandText = "Insert into Information(Id,Name) values(4,'代笔')";
cmd.ExecuteNonQuery();
Console.WriteLine("插入成功!");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
debug开下数据库是否打开
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询