代码出错!请问如何改正
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=.");
conn.Open();
Console.WriteLine("1");
SqlDataAdapter da = new SqlDataAdapter("select AddressId,City from Person.Address", conn);
DataSet ds = new DataSet();
da.UpdateCommand= new SqlCommand();
da.UpdateCommand.CommandText = "Update CustomerInfo set City=@City where (AddressId=@AddressId)";
SqlParameter AddressId = new SqlParameter("@AddressId", SqlDbType.Int);
SqlParameter City = new SqlParameter("@City", SqlDbType.NVarChar);
AddressId.Value = 4;
City.Value = "234";
da.UpdateCommand.Parameters.Add(AddressId);
da.UpdateCommand.Parameters.Add(City);
da.Fill(ds, "Address");
da.UpdateCommand.ExecuteNonQuery();
da.Update(ds);错误:
未处理 System.InvalidOperationException
Message="ExecuteNonQuery: Connection 属性尚未初始化。"
Source="System.Data"
StackTrace:
在 System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
在 ConsoleApplication1.Program.Main(String[] args) 位置 D:\Test\ConsoleApplication1\ConsoleApplication1\Program.cs:行号 27
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=.");
conn.Open();
Console.WriteLine("1");
SqlDataAdapter da = new SqlDataAdapter("select AddressId,City from Person.Address", conn);
DataSet ds = new DataSet();
da.UpdateCommand= new SqlCommand();
da.UpdateCommand.CommandText = "Update CustomerInfo set City=@City where (AddressId=@AddressId)";
SqlParameter AddressId = new SqlParameter("@AddressId", SqlDbType.Int);
SqlParameter City = new SqlParameter("@City", SqlDbType.NVarChar);
AddressId.Value = 4;
City.Value = "234";
da.UpdateCommand.Parameters.Add(AddressId);
da.UpdateCommand.Parameters.Add(City);
da.Fill(ds, "Address");
da.UpdateCommand.ExecuteNonQuery();
da.Update(ds);错误:
未处理 System.InvalidOperationException
Message="ExecuteNonQuery: Connection 属性尚未初始化。"
Source="System.Data"
StackTrace:
在 System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
在 ConsoleApplication1.Program.Main(String[] args) 位置 D:\Test\ConsoleApplication1\ConsoleApplication1\Program.cs:行号 27
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
解决方案 »
- 问一个WIN7的问题,希望版主别删,在WIN7专区 找不到答案。。
- 为何启动visual studio时打开的却是用户控件测试容器
- 求准确翻译:unhandled exception has occurred in your application.
- 散100分,看看我这水平在上海能拿多少?
- VS2005怎么做打包程序能使程序在安装结束后,自动执行程序。(像QQ一样安装结束后,它会自动启动qq程序)[顶者有分]
- C#中Process调用DOS命令的问题。
- 运行后,点击按钮,为何没有输出?
- 从服务器读出的数据流怎样先存放为服务器的一个文件,再打开来下载?
- 请各位大虾谈谈委托和托管是则么回事?这两个是不是同一概念,最近给这两个概念搞晕了!!!
- 关于调用winmm.dll播放音频文件的问题
- 为什么页面的Request.QueryString["username"].ToString()的值总是传不过
- 用C#创建带用户名和密码的ACCESS数据库的字符串该怎么写?
da.UpdateCommand.CommandText = "Update CustomerInfo set City=@City where (AddressId=@AddressId)";
上面的语句不成立.
幫頂!
da.Update(ds);1.UpdateCommand没有指定Connection属性
2.执行da.Update就不需要执行da.UpdateCommand.ExecuteNonQuery(),重新理解一下Adapter的意义。
=〉
da.UpdateCommand= new SqlCommand(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=.");
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks;Data Source=.");干嘛要多出个@
new SqlConnection(@"Integrated Security=SSPI;Persist