SqlDataAdapter.DeleteCommand删除记录

  使用SqlDataAdapter.DeleteCommand删除记录的实例代码如下
+展开
-C#
SqlConnection nwindConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
nwindConn.Open();
SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);
DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories");

catDA.DeleteCommand = new SqlCommand("DELETE FROM Categories WHERE CategoryID=@CategoryID", nwindConn);
SqlParameter workParm3 = catDA.DeleteCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
workParm3.SourceColumn = "CategoryID";
workParm3.SourceVersion = DataRowVersion.Original;
catDS.Tables["Categories"].Rows[catDS.Tables["Categories"].Rows.Count - 1].Delete();
catDA.Update(catDS, "Categories");

nwindConn.Close();
Console.WriteLine("按任意键继续...");
Console.ReadLine();


  运行失败,无法删除数据库记录,想破脑袋也搞不清楚怎么回事,在网上查了N多资料,从http://maocom.com/resources/program /html/200686/8358.htm 得到一个重要的信息,那就是 SelectCommand 是 select * from tablename 的形式,而不是我的仅仅只有部分字段,因此把上面的
+展开
-C#
SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);

修改成
+展开
-C#
SqlDataAdapter catDA = new SqlDataAdapter("SELECT * FROM Categories", nwindConn);

就可以了。
来源:http://blog.csdn.net/ifan_net/article/details/1378466

加支付宝好友偷能量挖...


评论(0)网络
阅读(117)喜欢(0)Asp.Net/C#/WCF