
SqlCommand对象的常用属性 和 SqlCommand对象的常用方法.doc
5页SqlCommand 对象的常用属性 和 SqlCommand 对象的常用方法 1. SqlCommand 对象的常用属性当创建好一个 SqlCommand 对象之后,还要正确设置 SqlCommand 对象的属性才能使用SqlCommand 对象的常用属性,如下表所示SqlCommand 对象的常用属性属 性 说 明Connection 属性 指定 Command 对象所使用的 Connection 对象CommandType 属性指定 Command 对象的类型,有 3 种选择:1 Text:表示 Command 对象用于执行 SQL 语句2 StoredProcedure:表示 Command 对象用于执行存储过程3 TableDirect:表示 Command 对象用于直接处理某个表CommandType 属性的默认值为 TextCommandText 属性根据 CommandType 属性的取值来决定 CommandText 属性的取值,分为 3 种情况:1 如果 CommandType 属性取值为 Text,则 CommandText 属性指出 SQL 语句的内容2 如果 CommandType 属性取值为 StoredProcedure,则 CommandText 属性指出存储过程的名称。
3 如果 CommandType 属性取值为 TableDirect,则 CommandText 属性指出表的名称CommandText 属性的默认值为 SQL 语句CommandTimeout 属性指定 Command 对象用于执行命令的最长延迟时间,以秒为单位,如果在指定时间内仍不能开始执行命令,则返回失败信息默认值为 30 秒Parameters 属性 指定一个参数集合下面通过一个示例来说明,如何正确设置 SqlCommand 对象的 CommandType 属性和 CommandText 属性示例 1:使用 SqlCommand 对象执行一条 SQL 语句,统计 数据库的 Users 表中有多少个用户;使用 SqlCommand 对象执行一个存储过程,统计 数据库的 Users表中有多少个用户程序的主要代码,如下所示:public partial class Form1 : Form{//数据库连接字符串 private static string connString = "Data Source=localhost;Initial Catalog=;Integrated Security=true";//数据库连接对象public static SqlConnection connection = new SqlConnection(connString);public Form1(){InitializeComponent();}//执行 SQL语句private void btnSql_Click(object sender, EventArgs e){try{//查询用的 SQL 语句string selectSql = "select count(*) from Users";//创建 Command对象SqlCommand command = new SqlCommand();//指定 Command对象所使用的 Connection对象command.Connection = connection;//指定 Command对象用于执行 SQL语句command.CommandType = CommandType.Text;//指定要执行的 SQL语句command.CommandText = selectSql; //打开数据库连接connection.Open();//执行查询操作,返回单个值this.txtCount.Text = command.ExecuteScalar().ToString();}catch (Exception ex) {MessageBox.Show(ex.Message);}finally{//关闭数据库连接connection.Close();}}//执行存储过程private void btnStoreProc_Click(object sender, EventArgs e){try{//创建 Command对象SqlCommand command = new SqlCommand();//指定 Command对象所使用的 Connection对象command.Connection = connection;//指定 Command对象用于执行存储过程command.CommandType = CommandType.StoredProcedure;//指定要执行的存储过程的名称command.CommandText = "procSelect1"; //打开数据库连接connection.Open();//执行查询操作,返回单个值this.txtCount.Text = command.ExecuteScalar().ToString();}catch (Exception ex){MessageBox.Show(ex.Message);}finally{//关闭数据库连接connection.Close();}}}补充:在后台数据库中,创建上述名为 “procSelect1”的存储过程的代码,如下所示:create procedure procSelect1asbeginselect count(*) from Usersend2. SqlCommand 对象的常用方法当 SqlCommand 对象的属性设置好之后,就可以调用 SqlCommand 对象的方法来对数据库中的数据进行处理。
SqlCommand 对象的常用方法,如下表所示SqlCommand 对象的常用方法方 法 说 明ExecuteReader 执行查询操作,返回一个具有多行多列的结果集ExecuteScalar 执行查询操作,返回单个值ExecuteNonQuery 执行插入、修改或删除操作,返回本次操作受影响的行数下面通过一个示例来说明,如何使用 SqlCommand 对象的 ExecuteNonQuery 方法示例 2:使用 SqlCommand 对象的 ExecuteNonQuery 方法来执行删除操作,删除 数据库的 Users 表中的记录程序的主要代码,如下所示:public partial class Form1 : Form{//数据库连接字符串 private static string connString = "Data Source=localhost;Initial Catalog=;Integrated Security=true";//数据库连接对象public static SqlConnection connection = new SqlConnection(connString); public Form1(){InitializeComponent();}//执行删除操作private void btnDelete_Click(object sender, EventArgs e){try{//删除记录用的 SQL 语句string deleteSql = String.Format("delete from Users where Id = {0}", this.txtLoginId.Text);//创建 Command对象SqlCommand command = new SqlCommand();//指定 Command对象所使用的 Connection对象command.Connection = connection;//指定 Command对象用于执行 SQL语句command.CommandType = CommandType.Text;//指定要执行的 SQL语句command.CommandText = deleteSql; //打开数据库连接connection.Open();//执行删除操作,返回本次操作受影响的行数int result = command.ExecuteNonQuery();MessageBox.Show("本次操作受影响的行数: " + result.ToString());}catch (Exception ex){MessageBox.Show(ex.Message);}finally{//关闭数据库连接connection.Close();}}}。
