一无参构造函数的形式:
创建连接、创建命令、执行命令:
string connstr = @"server=.;database=TestDataBase;uid=sa;pwd=130988825";
//连接字符串,可以在服务器资源管理器那里,右键连接,修改连接,高级,在对话框里面可以看到
SqlConnection conn =new SqlConnection(connstr); //将连接字符串传给创建的连接实例
conn.Open(); //打开连接
SqlCommand cmd = new SqlCommand(); //创建一个命令实例
cmd.Connection = conn; //给命令的Connection属性复制,即关联一个连接
cmd.CommandText = @"select count(*) from Student where sId='2'"; // 给命令的CommandText属性复制,即关联一个sql语句
int n = (int)cmd.ExecuteScalar(); //执行命令,执行命令的方法有:ExecuteScalar()、ExecuteNonQuery()、ExecuteScalar()
Console.WriteLine(n.ToString());
Console.ReadKey();
==========================================================================
二、执行命令的几个方法的综述:
1. ExecuteNonQuery------执行增、删、改,不返回任何结果
2. ExecuteReader-------执行查询,返回一个类型化SqlDataReader,用于逐条读取每一行的所有信息
3. ExecuteScalar-------执行查询,返回第一行第一列的object类型、一般用于求总数、或者有没有
==================================================================================
三、三个方法的标准代码
1、ExecuteNonQuery()
此方法执行update, delete, insert语句返回命令所操作的行数,它为一整数
static void Main(string[] args)
{
string connstr = @"server=WIN-VD88F15CMMD;database=Key;uid=sa;pwd=130988825"; //连接字符串
string sqlz = "insert into [Student](sName,sClass,sGender) values('key',1,'男')"; //插入数据的sql语句
string sqlx = "delete from [Student] where sName='key'"; //删除
string sqlg = "update [Student] set sName='胡冬冬',sClass=4 where sName='胡章诚'"; //修改
using (SqlConnection conn = new SqlConnection(connstr))
{
using (SqlCommand cmd = new SqlCommand(sqlz, conn))
{
conn.Open();
int a = cmd.ExecuteNonQuery();
Console.WriteLine("数据增添成功");
Console.ReadKey();
cmd.CommandText = sqlx;
int b = cmd.ExecuteNonQuery();
Console.WriteLine("数据删除成功");
Console.ReadKey();
cmd.CommandText = sqlg;
int c = cmd.ExecuteNonQuery();
Console.WriteLine("修改成功");
Console.ReadKey();
}
}
}
==============================================================================
2、ExecuteReader()
返回一个SqlDataReader 的实例
static void Main(string[] args)
{
string connstr = @"server=win-45022h0f9ns\mysqlserver;database=CallCenter;Persist Security Info=True;uid=sa;pwd=130988825";
string sql = @"select * from Student";
using (SqlConnection conn = new SqlConnection(connstr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
using(SqlDataReader sda = cmd.ExecuteReader())
if (sda.HasRows)
{
while (sda.Read())
{
Console.WriteLine("id为:" + sda[0]);
}
}
Console.ReadKey();
}
}
}
=======================================================================================
3、ExecuteScalar()
很多情况下,需要从SQL语句中返回一个结果,例如给定表中的记录个数,或者服务器当前的日期时间,ExecuteScalar()适用于这些情况
获取单个结果集
static void Main(string[] args)
{
string connstr = @"Data Source=WIN-VD88F15CMMD;Initial Catalog=Key;User ID=sa;pwd=130988825";
string sql = "select count(*) from [Class]";
using (SqlConnection conn = new SqlConnection(connstr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
int n = (int)cmd.ExecuteScalar();
Console.WriteLine(n.ToString());
Console.ReadKey();
}
}
}
该方法返回一个Object对象,如果需要可以把该对象的数据类型转化为合适的数据类型。
二、获取多个结果集
static void Main(string[] args)
{
string connstr = @"server=win-45022h0f9ns\mysqlserver;database=CallCenter;uid=sa;pwd=130988825";
string sql = @"select * from Student;select * from T_scripts";
using (SqlConnection conn = new SqlConnection(connstr))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
SqlDataReader sda = cmd.ExecuteReader();
if (sda.HasRows)
{
while (sda.Read())
{
Console.WriteLine("id为:"+sda[0]);
}
}
Console.WriteLine("=================");
Console.ReadKey();
if (sda.NextResult()) //利用 sda.NextResult() 判断是否还有结果集
{
while (sda.Read())
{
Console.WriteLine("id为:" + sda[0]);
}
}
Console.ReadKey();
sda.Close();
Console.ReadKey();
}
}
}