ADO.net访问数据库(四种访问模型) 1次课
引用命名空间:using System.Data.SqlClient;
1增加,删除,修改的通用过程
//1:创建连接对象,就是搭建桥梁
string Connstring = "Data Source=.;Initial Catalog=myschool;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Connstring);
//2.定义要执行sql语句:必须是增删修
string sql = @ " ";
//3. 创建操作对象
SqlCommand sqlcomm = new SqlCommand(sql, sqlconn);
//4.打开连接
sqlconn.Open();
//5 .执行操作: 返回值,执行语句对数据库的影响行数
int num= sqlcomm.ExecuteNonQuery();
//6 关闭连接
sqlconn.Close();
2单值查询 只有一个值(一行一列)的查询
//第一步:创建连接对象,就是搭建桥梁
string Connstring = "Data Source=.;Initial Catalog=myschool;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Connstring);
//第二步:定义要执行sql语句
string sql = "select COUNT(*) from Student";
//第三步:定义操作对象
SqlCommand sqlcomm = new SqlCommand(sql, sqlconn);
//第四步:打开连接
sqlconn.Open();
//第五步:执行操作
object obj=sqlcomm.ExecuteScalar();//获取执行后结果
int n = Convert.ToInt32(obj);//转换成实际类型[按实际类型转换]
//第六步:关闭
sqlconn.Close();
引用命名空间:using System.Data.SqlClient;
1增加,删除,修改的通用过程
//1:创建连接对象,就是搭建桥梁
string Connstring = "Data Source=.;Initial Catalog=myschool;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Connstring);
//2.定义要执行sql语句:必须是增删修
string sql = @ " ";
//3. 创建操作对象
SqlCommand sqlcomm = new SqlCommand(sql, sqlconn);
//4.打开连接
sqlconn.Open();
//5 .执行操作: 返回值,执行语句对数据库的影响行数
int num= sqlcomm.ExecuteNonQuery();
//6 关闭连接
sqlconn.Close();
2单值查询 只有一个值(一行一列)的查询
//第一步:创建连接对象,就是搭建桥梁
string Connstring = "Data Source=.;Initial Catalog=myschool;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Connstring);
//第二步:定义要执行sql语句
string sql = "select COUNT(*) from Student";
//第三步:定义操作对象
SqlCommand sqlcomm = new SqlCommand(sql, sqlconn);
//第四步:打开连接
sqlconn.Open();
//第五步:执行操作
object obj=sqlcomm.ExecuteScalar();//获取执行后结果
int n = Convert.ToInt32(obj);//转换成实际类型[按实际类型转换]
//第六步:关闭
sqlconn.Close();
3. 多行多列的查询 使用sqldataAdapter+DataSet
string Connstring = "Data Source=.;Initial Catalog=Myschool;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Connstring);
//2.查询语句
string sql = "select * from grade";
//3.创建一个"运货车"对象
SqlDataAdapter sqldap = new SqlDataAdapter(sql, sqlconn);
//4.定义一个临时的仓库
DataSet ds = new DataSet();//内存中一个数据库
//5.使用运货车把数据存放到仓库中
sqldap.Fill(ds, "tab");//可以把查询出的数据表,取一个名字tab
//读取ds中的数据方法。。。。
foreach(DataRow dr in ds.Tables[0].Rows){ //循环数据集中的表中的行
Console.WriteLine( dr["列名"].ToString());//即可
}
string Connstring = "Data Source=.;Initial Catalog=Myschool;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Connstring);
//2.查询语句
string sql = "select * from grade";
//3.创建一个"运货车"对象
SqlDataAdapter sqldap = new SqlDataAdapter(sql, sqlconn);
//4.定义一个临时的仓库
DataSet ds = new DataSet();//内存中一个数据库
//5.使用运货车把数据存放到仓库中
sqldap.Fill(ds, "tab");//可以把查询出的数据表,取一个名字tab
//读取ds中的数据方法。。。。
foreach(DataRow dr in ds.Tables[0].Rows){ //循环数据集中的表中的行
Console.WriteLine( dr["列名"].ToString());//即可
}
重点技术:动态构建SQL语句的方法
1.通过string.format() 格式化构建
string sql = string.Format("select COUNT(*) from Student where Sex='{0}'",sex);
2. 通过 “ + ”连接
技巧:
1..sql语句先在数据库写好,保存在数据库中能执行成功,再复制到C#项目中。
2.需要掌握在C#调试检查生成的数据库语句是否正确。
1.通过string.format() 格式化构建
string sql = string.Format("select COUNT(*) from Student where Sex='{0}'",sex);
2. 通过 “ + ”连接
技巧:
1..sql语句先在数据库写好,保存在数据库中能执行成功,再复制到C#项目中。
2.需要掌握在C#调试检查生成的数据库语句是否正确。