搜索
您的当前位置:首页正文

SqlCommand对象的常用属性 和 SqlCommand对象的常用方法

来源:爱go旅游网
SqlCommand对象的常用属性 和 SqlCommand对象的常用方法

1. SqlCommand对象的常用属性

当创建好一个SqlCommand对象之后,还要正确设置SqlCommand对象的属性才能使用。SqlCommand对象的常用属性,如下表所示。

SqlCommand对象的常用属性 属 性 Connection属性 说 明 指定Command对象所使用的Connection对象。 指定Command对象的类型,有3种选择: 1 Text:表示Command对象用于执行SQL语句。 CommandType属性 2 StoredProcedure:表示Command对象用于执行存储过程。 3 TableDirect:表示Command对象用于直接处理某个表。 CommandType属性的默认值为Text。 根据CommandType属性的取值来决定CommandText属性的取值,分为3种情况: 1 如果CommandType属性取值为Text,则CommandText属性指出SQL语句的内CommandText属性 容。 2 如果CommandType属性取值为StoredProcedure,则CommandText属性指出存储过程的名称。 3 如果CommandType属性取值为TableDirect,则CommandText属性指出表的名称。 CommandText属性的默认值为SQL语句。 指定Command对象用于执行命令的最长延迟时间,以秒为单位,如果CommandTimeout属性 在指定时间内仍不能开始执行命令,则返回失败信息。 默认值为30秒。 Parameters属性 指定一个参数集合。

下面通过一个示例来说明,如何正确设置SqlCommand对象的CommandType属性和CommandText属性。

示例1:使用SqlCommand对象执行一条SQL语句,统计QQ数据库的Users表中有多少个用户;使用SqlCommand对象执行一个存储过程,统计QQ数据库的Users表中有多少个用户。

程序的主要代码,如下所示:

public partial class Form1 : Form {

//数据库连接字符串

private static string connString = \"Data Source=localhost;Initial Catalog=QQ;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 procSelect1 as

begin

select count(*) from Users end

2. SqlCommand对象的常用

方法

当SqlCommand对象的属性设置好之后,就可以调用SqlCommand对象的方法来对数据库中的数据进行处理。SqlCommand对象的常用方法,如下表所示。

SqlCommand对象的常用方法

方 法 ExecuteReader ExecuteScalar ExecuteNonQuery 说 明 执行查询操作,返回一个具有多行多列的结果集。 执行查询操作,返回单个值。 执行插入、修改或删除操作,返回本次操作受影响的行数。 下面通过一个示例来说明,如何使用SqlCommand对象的ExecuteNonQuery方法。 示例2:使用SqlCommand对象的ExecuteNonQuery方法来执行删除操作,删除QQ

数据库的Users表中的记录。

程序的主要代码,如下所示:

public partial class Form1 : Form {

//数据库连接字符串

private static string connString = \"Data Source=localhost;Initial Catalog=QQ;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}\

//创建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(); } } }

因篇幅问题不能全部显示,请点此查看更多更全内容

Top