| 构造方法 | 说明 |
|---|---|
| SqlCommand() | 无参构造方法 |
| SqlCommand(string commandText,SqlConnection conn) | 带参的构造方法,第 1 个参数是要执行的 SQL 语句,第 2 个参数是数据库的连接对象 |
| 属性或方法 | 说明 |
|---|---|
| CommandText | 属性,Command 对象中要执行的 SQL 语句 |
| Connection | 属性,获取或设置数据库的连接对象 |
| CommandType | 属性,获取或设置命令类型 |
| Parameters | 属性,设置 Command 对象中 SQL 语句的参数 |
| ExecuteReader() | 方法,获取执行查询语句的结果 |
| ExecuteScalar() | 方法,返回查询结果中第 1 行第 1 列的值 |
| ExecuteNonQuery() | 方法,执行对数据表的增加、删除、修改操作 |
SqlCommand SqlCommand 类的实例名 = new SqlCommand( SQL 语句 , 数据库连接类的实例 );
其中:SqlCommand SqlCommand 类的实例名 = new SqlCommand( 存储过程名称 , 数据库连接类的实例 );
需要注意的是,存储过程必须是当前数据库实例中的存储过程,并且在调用带参数的存储过程时,还需要在 SqlCommand 类的实例中添加对应的存储过程参数。SqlCommand 类实例 .Parameters.Add( 参数名 , 参数值 );
在这里,参数名与存储过程中定义的参数名要一致。SqlCommand 类的实例 .ExecuteNonQuery();
需要注意的是,如果执行的 SQL 语句在数据库中执行错误,则会产生异常,因此该部分需要进行异常处理。SqlDataReader dr = SqlCommand 类的实例 .ExecuteReader();
此外,如果在执行查询语句后并不需要返回所有的查询结果,而仅需要返回一个值,例如查询表中的记录行数,这时可以使用 ExecuteScalar 方法。具体的代码如下。int returnvalue = SqlCommand 类的实例 .ExecuteScalar();
下面通过实例来演示 SqlCommand 类的使用。
create table userinfo
(
id int identity(1,1) primary key,
name varchar(20),
password varchar(20)
)
为了方便,将表中的 id 设置为主键,并设置为标识列,以保证值的唯一性。
//“注册”按钮的单击事件
private void button1_Click(object sender, EventArgs e)
{
//编写数据库连接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//创建 SqlConnection的实例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打开数据库连接
conn.Open();
string sql = "insert into userinfo(name,password) values('{0}','{1}')";
//填充SQL语句
sql = string.Format(sql, textBox1.Text, textBox2.Text);
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand(sql, conn);
//执行SQL语句
int returnvalue = cmd.ExecuteNonQuery();
//判断SQL语句是否执行成功
if(returnvalue != -1)
{
MessageBox.Show("注册成功!");
}
}
catch(Exception ex)
{
MessageBox.Show("注册失败!"+ex.Message);
}
finally
{
if (conn != null)
{
//关闭数据库连接
conn.Close();
}
}
}
运行窗体,效果如下图所示。
//"登录"按钮的单击事件
private void button1_Click(object sender, EventArgs e)
{
//编写数据库连接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//创建SQLConnection的实例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打开数据库连接
conn.Open();
string sql = "Select count(*) from userinfo where name='{0}' and password='{1}'";
//填充SQL语句
sql = string.Format(sql, textBox1.Text, textBox2.Text);
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand(sql, conn);
//执行SQL语句
int returnvalue = (int)cmd.ExecuteScalar();
//判断SQL语句是否执行成功
if (returnvalue != 0)
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("登录失败!");
}
}
catch (Exception ex)
{
MessageBox.Show("注册失败!" + ex.Message);
}
finally
{
if (conn != null)
{
//关闭数据库连接
conn.Close();
}
}
}
//“取消”按钮的单击事件
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
运行该窗体,效果如下图所示。
//“注册”按钮的单击事件
private void button1_Click(object sender, EventArgs e)
{
//编写数据库连接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//创建 SqlConnection的实例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打开数据库连接
conn.Open();
//判断用户名是否重复
string checkNameSql = "select count(*) from userinfo where name='{0}'";
checkNameSql = string.Format(checkNameSql, textBox1.Text);
//创建SqlCommand对象
SqlCommand cmdCheckName = new SqlCommand(checkNameSql, conn);
//执行SQL语句
int isRepeatName = (int)cmdCheckName.ExecuteScalar();
if (isRepeatName != 0)
{
//用户名重复,则不执行注册操作
MessageBox.Show("用户名已存在!");
return;
}
string sql = "insert into userinfo(name,password) values('{0}','{1}')";
//填充SQL语句
sql = string.Format(sql, textBox1.Text, textBox2.Text);
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand(sql, conn);
//执行SQL语句
int returnvalue = cmd.ExecuteNonQuery();
//判断SQL语句是否执行成功
if(returnvalue != -1)
{
MessageBox.Show("注册成功!");
}
}
catch(Exception ex)
{
MessageBox.Show("注册失败!"+ex.Message);
}
finally
{
if (conn != null)
{
//关闭数据库连接
conn.Close();
}
}
}
运行该窗体,输入已经存在的用户名“张三”,执行效果如下图。create procedure AddUser(@name varchar(20), @password varchar(20)) as begin insert into userinfo (name, password) values (@name, @password); end注册页面与实例 1 一样,在“注册”按钮的单击事件中调用 AddUser 存储过程,代码如下。
//编写数据库连接串
string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root";
//创建 SqlConnection的实例
SqlConnection conn = null;
try
{
conn = new SqlConnection(connStr);
//打开数据库连接
conn.Open();
//创建SqlCommand对象
SqlCommand cmd = new SqlCommand("AddUser", conn);
//设置SQLCommand对象的命令类型(CommandType)是存储过程
cmd.CommandType = CommandType.StoredProcedure;
//设置存储过程需要的参数
cmd.Parameters.AddWithValue("name", textBox1.Text);
cmd.Parameters.AddWithValue("password", textBox2.Text);
//执行存储过程
int returnvalue = cmd.ExecuteNonQuery();
Console.WriteLine(returnvalue);
//判断SQL语句是否执行成功
if(returnvalue != -1)
{
MessageBox.Show("注册成功!");
}
}
catch(Exception ex)
{
MessageBox.Show("注册失败!"+ex.Message);
}
finally
{
if (conn != null)
{
//关闭数据库连接
conn.Close();
}
}
运行该窗体,效果与实例 1 一致。从上面的代码可以看出,调用存储过程并不复杂,只需要在 SqlCommand 对象中将 CommandType 属性的值改成 StoredProcedure,并添加存储过程中所需要的参数即可。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有