ADO_Net学习笔记---总结

来源:互联网 发布:网络名词流行用语2016 编辑:程序博客网 时间:2024/06/13 03:55
1.ADO.NET
 数据库集:DataSet, DataTable,DataRow(system.Data)
 .Net应用程序集:Command,Connection,DataReader,DataAdapter(system.Data.SqlClient)
2.连接字符串
  (1)准备连接字符串
  string connStr="server=.\SQLEXPRESS;database=inno;uid=sa;pwd=";
  (2)建立连接通道
  SqlConnection conn=new SqlConnection(connStr);
  (3)打开通道
   conn.Open();
   (4)关闭通道
   conn.Close();
3.SqlCommand---执行增删改
  (1)准备连接字符串
  string connStr="server=.\SQLEXPRESS;database=inno;uid=sa;pwd=";
  (2)建立连接通道
  SqlConnection conn=new SqlConnection();
  (3)设置连接字符串
  conn.ConnectionString=connStr;
  (4)准备插入SQL语句
  string sqlStr="insert into t_customer('值1','值2'......)";
  (5)准备Commadn命令
  SqlCommand cmd=new SqlCommand(sqlStr,conn);
  (6)打开通道
  conn.Open();
  (7)执行命令
  res=cmd.ExecuteNonQUery();
  (8)关闭数据库
   conn.Close();
4.ExecuteScalar()查询单个值
  string connStr="server=.\SQLEXPRESS;database=inno;uid=sa;pwd=";
  SqlConnection conn=new SqlConnection();
  string sqlStr="select count(*) from t_customer";

  SqlCommand cmd=new SqlCommand(sqlStr,conn);
  conn.Open();
  object o=cmd.ExecuteScalar();
  conn.Close();
  Console.WriteLine(o.ToString());
5.SqlDataReader 读取器
  string connStr=".......";
  SqlConnection conn=new SqlConnection(connStr);
  string sqlStr="select * from t_customer";
  SqlCommand cmd=new SqlCommand(sqlStr,conn);
  conn.Open();
  SqlDataReader dr=cmd.ExecuteReader()

  if(dr.HasRows)
  {
    while(dr.Read())
    {
      Console.WriteLine(dr[0].Tostring()+"==="+dr[1].Tostring());
    }
  }

6.SqlDataAdapter 适配器(只要有conn就可以,不要command)
 string connStr=@"server=.\SQLEXPRESS;database=inno;uid=sa;pwd=";
 SqlConnection conn=new SqlConnection();
 string sqlStr="select * from t_customer";
 SqlDataAdapter da=new SqlDataAdapter(sqlStr,conn);
 DataSet ds=new DataSet();
 da.Fill(ds);
 DataTable dt=ds.Table[0];


7.调用存储过程(只是含有输入参数)
 string connStr=@"server=.\SQLEXPRESS;database=inno;uid=sa;pwd=";
 SqlConnection conn=new SqlConnection(connStr);
 SqlCommand cmd=new SqlCommand("customerdetail",conn);//存储过程名称
 cmd.CommandType=CommandType.StoredProcedure;//指定为存储过程
 SqlParameter[] sp={new SqlParameter("@name",SqlDbType.NVarChar)};//可以指定多个参数
 sp[0].value="南昌新森林网络";
 cmd.Parameters.AddRange(sp);
 
 SqlDataAdapter da=new SqlDataAdapter(cmd);
 DataTable dt=new DataTable();
 da.Fill(dt);
 
8.调用存储过程(带输出参数)
 string connStr=@"server=.\SQLEXPRESS;database=inno;uid=sa;pwd=";
 SqlConnection conn=new SqlConnection(connStr);
 SqlCommand cmd=new SqlCommand("customerproc",conn)
 cmd.CommandType=CommandType.SotredProcedure;
 SqlParameter[] sp={
   new SqlParameter{"@PageCurrent",SqlDbType.int},
   new SqlParameter{"@PageSize",SqlDbType.int},
   new SqlParameter{"@RecordCount",SqlDbType.int}
}
sp[0].value=3;//第几页
sp[1].value=20;//每页多少
sp[2].Direction=ParameterDirection.Output;
cmd.Parameters.AddRange(sp);

SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);

int RecordCount=cmd.Parameters[2].value;//得到output的值



9.事务
SqlTransation Tran=conn.BeginTransaction();//开始事务
Tran.Commit();//提交事务
Tran.Rollback();//回滚事务






原创粉丝点击