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






