30岁,我在做什么?

.NET SQL数据库 Office开发 办公自动化

导航

 MSDN Home >  Visual Studio .NET >  Visual Basic 和 Visual C# >  访问数据 >  直接执行数据库操作
Visual Basic 和 Visual C# 概念  

使用数据命令执行更新或数据库命令

ADO.NET 数据命令使您能够直接对数据库或其他数据源执行命令,而不需要数据集或数据适配器。有关更多信息,请参见 Visual Studio 中的 DataCommand 对象介绍

 




  1. 可针对数据库执行的某些类型的数据命令除了返回一个指出命令成功的值以外不返回任何值。这些命令类型包括:
  • 数据库定义 (DDL) 命令,用于创建和管理数据库结构(如表和存储过程)。
  • 更新命令(“更新”(Update)、“插入”(Insert) 和“删除”(Delete) 语句)。

必须使用数据库命令执行 DDL 操作;没有其他方法可以在 ADO.NET 中执行这些任务。

如果正在使用数据集,则不需要使用单独的数据命令执行数据库更新。而是使用数据适配器更新数据库。有关更多信息,请参见 Visual Studio .NET 中的数据集更新。但是,如果使用的不是数据集,则可以直接向数据库发送更新命令。有关数据集相对于数据命令的优点的更多信息,请参见数据访问策略建议

对于这两种类型的命令,命令都返回一个整数指示操作的成功与否。返回值因是要更新记录还是发出 DDL 命令而异:

  • 如果要创建或修改数据库结构,当操作成功时返回值为 -1。
  • 如果要更新记录,则返回值指示受到该操作影响的记录数。

在两种情况下,如果操作失败,命令都返回零。

执行更新数据库的命令

  1. 向窗体或组件添加一个数据命令,并用要执行的 SQL 语句或存储过程配置它。有关更多信息,请参见向窗体或组件添加数据命令
  2. 如果窗体或组件上尚没有可用的连接对象,请进行添加,然后,从工具箱的“数据”选项卡,将 OleDbCommandSqlCommandOdbcCommandOracleCommand 对象拖到窗体或组件上。
  3. 如果命令采用参数,则配置参数。有关更多信息,请参见为数据适配器配置参数
  4. 如果有参数,则添加代码来设置参数值。有关更多信息,请参见设置和获取数据命令参数
  5. 添加代码以打开与数据命令关联的连接。
  6. 调用命令的 ExecuteNonQuery 方法,将结果设置为一个整数。
    注意   尽管该方法只返回单个整数值,但您调用的存储过程却可能使用命令的 Parameters 集合返回若干个值。有关使用参数的更多信息,请参见设置和获取数据命令参数
  7. 关闭连接。

下面的示例展示如何在数据库中创建新表。该示例假定您已向窗体或组件添加了一个数据命令和一个连接,并已配置命令使用该连接。此代码将 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 对象介绍 | 使用数据命令:高级别进程 | 设置和获取数据命令参数 | 执行返回结果集的数据命令 | 执行返回单个值的数据命令