打印本文 打印本文 关闭窗口 关闭窗口
第三课 SqlCommand对象(翻译)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数14790  更新时间:2007/11/14 13:12:47  文章录入:mintao  责任编辑:mintao
ExecuteNonQuery方法删除数据。下面的例子说明了如何使用EXecuteNonQuery方法删除数据库中的记录。

// prepare command string
 
string deleteString = @"
     delete from Categories
     where CategoryName = ''''Other''''";
 
 // 1. Instantiate a new command
 
SqlCommand cmd = new SqlCommand();
 
 // 2. Set the CommandText property
 
cmd.CommandText = deleteString;
 
 // 3. Set the Connection property
 
cmd.Connection = conn;
 
 // 4. Call ExecuteNonQuery to send command
 
cmd.ExecuteNonQuery();

This example uses the SqlCommand constructor with no parameters.  Instead, it explicity sets the CommandText and Connection properties of the SqlCommand object, cmd. 

这个示例使用了没有参数的SqlCommand构造函数。取而代之的是显式地设置了CommandTextSqlCommand对象的连接属性。

We could have also used either of the two previous SqlCommand constructor overloads, used for the insert or update command, with the same result.  This demonstrates that you can change both the command text and the connection object at any time. 

我们同样能够使用SqlCommand构造函数在前面的两个重载形式——用来插入或者更新命令——得到相同的结果。它说明了在任何时候既能够改变命令文本又能够改变连接对象。

The ExecuteNonQuery method call sends the command to the data base.

ExecuteNonQuery方法调用将命令传递给数据库。

This code is part of the DeleteData method of Listing 1 in the Putting it All Together section later in this lesson.

这些代码是表1

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ...  下一页 >> 

打印本文 打印本文 关闭窗口 关闭窗口