直接执行数据库操作—使用数据命令ExecuteNonQuery 方法,执行更新或数据库命令
Posted on 2007-01-02 12:25 .net 世界 阅读(2486) 评论(0) 收藏 举报|
ADO.NET 数据命令使您能够直接对数据库或其他数据源执行命令,而不需要数据集或数据适配器。有关更多信息,请参见 Visual Studio 中的 DataCommand 对象介绍。
必须使用数据库命令执行 DDL 操作;没有其他方法可以在 ADO.NET 中执行这些任务。 如果正在使用数据集,则不需要使用单独的数据命令执行数据库更新。而是使用数据适配器更新数据库。有关更多信息,请参见 Visual Studio .NET 中的数据集更新。但是,如果使用的不是数据集,则可以直接向数据库发送更新命令。有关数据集相对于数据命令的优点的更多信息,请参见数据访问策略建议。 对于这两种类型的命令,命令都返回一个整数指示操作的成功与否。返回值因是要更新记录还是发出 DDL 命令而异:
在两种情况下,如果操作失败,命令都返回零。 执行更新数据库的命令
下面的示例展示如何在数据库中创建新表。该示例假定您已向窗体或组件添加了一个数据命令和一个连接,并已配置命令使用该连接。此代码将 CommandText 属性 设置为一条 SQL Server Create Table 语句,它创建具有两列的查找代码表。在表创建成功后该方法返回 -1。 ' Visual Basic
Dim newtablecmd As String
Dim cmdresults As Integer
newtablecmd = "CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1) PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)"
' The following two property settings can also be set
' in the Properties window, but are shown here for completeness.
OleDbCommand1.CommandType = CommandType.Text
OleDbCommand1.CommandText = newtablecmd
' The connection must be open before you can execute a command.
OleDbConnection1.Open()
cmdresults = OleDbCommand1.ExecuteNonQuery()
OleDbConnection1.Close()
MessageBox.Show("After creating the table, results = " & cmdresults.ToString)
// C#
string newtablecmd;
int cmdresults;
newtablecmd = "CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1) PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)";
// The following two property settings can also be done
// in the Properties window, but are shown here for completeness.
OleDbCommand1.CommandType = CommandType.Text;
OleDbCommand1.CommandText = newtablecmd;
// The connection must be open before you can execute a command.
OleDbConnection1.Open();
cmdresults = OleDbCommand1.ExecuteNonQuery();
OleDbConnection1.Close();
MessageBox.Show("After creating the table, results = " + cmdresults.ToString());
下面的示例展示如何使用数据命令,通过向 SQL Server Pubs 数据库的 Authors 表中插入新记录来更新数据库。在本实例中,命令调用名为“NewAuthor”的存储过程,假定该过程包含一条“插入到”(Insert Into) 语句,带有新作者记录的九个值。数据命令 OleDbCommand2 已配置为具有 Parameters 集合,其内有九个参数,分别表示传递给存储过程的参数。该代码基于窗体中的文本框设置参数值,打开连接,调用 ExecuteNonQuery 方法,然后关闭连接。 ' Visual Basic
Dim cmdresults As Integer
' The following two property settings can also be done
' in the Properties window, but are shown here for completeness.
OleDbcommand2.CommandText = "NewAuthor"
OleDbCommand2.CommandType = CommandType.StoredProcedure
' Set parameter values. In this case, all parameter values
' are strings.
OleDbCommand2.Parameters("au_id").Value = TextBox1.Text
OleDbCommand2.Parameters("au_lname").Value = TextBox2.Text
OleDbCommand2.Parameters("au_fname").Value = TextBox3.Text
OleDbCommand2.Parameters("phone").Value = TextBox4.Text
OleDbCommand2.Parameters("address").Value = TextBox5.Text
OleDbCommand2.Parameters("city").Value = TextBox6.Text
OleDbCommand2.Parameters("st").Value = TextBox7.Text
OleDbCommand2.Parameters("zip").Value = TextBox8.Text
OleDbCommand2.Parameters("contract").Value = CheckBox1.Checked
OleDbConnection2.Open()
Try
cmdresults = OleDbcommand2.ExecuteNonQuery()
Catch ex as Exception
MessageBox.Show("Failed to execute command")
End Try
OleDbConnection2.Close()
MessageBox.Show("Number of records inserted = " & cmdresults.ToString)
// C#
int cmdresults = 0;
// The following two property settings can also be done
// in the Properties window, but are shown here for completeness.
OleDbCommand2.CommandText = "NewAuthor";
OleDbCommand2.CommandType = CommandType.StoredProcedure;
// Set parameter values. In this case, all parameter values
// are strings.
OleDbCommand2.Parameters["au_id"].Value = TextBox1.Text;
OleDbCommand2.Parameters["au_lname"].Value = TextBox2.Text;
OleDbCommand2.Parameters["au_fname"].Value = TextBox3.Text;
OleDbCommand2.Parameters["phone"].Value = TextBox4.Text;
OleDbCommand2.Parameters["address"].Value = TextBox5.Text;
OleDbCommand2.Parameters["city"].Value = TextBox6.Text;
OleDbCommand2.Parameters["st"].Value = TextBox7.Text;
OleDbCommand2.Parameters["zip"].Value = TextBox8.Text;
OleDbCommand2.Parameters["contract"].Value = CheckBox1.Checked;
OleDbConnection2.Open();
try
{
cmdresults = OleDbCommand2.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Failed to execute command");
}
OleDbConnection2.Close();
MessageBox.Show("Number of records inserted = " + cmdresults.ToString());
请参见Visual Studio 中的 DataCommand 对象介绍 | 使用数据命令:高级别进程 | 设置和获取数据命令参数 | 执行返回结果集的数据命令 | 执行返回单个值的数据命令 |
浙公网安备 33010602011771号