首页 > 代码库 > ado:SqlDataAdapter的两种不同写法,以及SqlCommand的两种不同写法

ado:SqlDataAdapter的两种不同写法,以及SqlCommand的两种不同写法

原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]

SqlDataAdapter:(它是自动打开连接且自动关闭的,所以可以不必显示打开关闭连接)

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pusConn"].ConnectionString);

       // SqlDataAdapter sda = new SqlDataAdapter("select * from authors", conn);//第一种写法
      

        SqlDataAdapter sda = new SqlDataAdapter();//第二种写法
        sda.SelectCommand = new SqlCommand("select * from authors",conn);

        DataSet ds = new DataSet();

        sda.Fill(ds,"t1");

----------------------------

SqlCommand:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pusConn"].ConnectionString);


//SqlCommand cmd=new SqlCommand("delete from authors where au_id=2",conn);//第一种方法

      SqlCommand cmd=new SqlCommand();//第二种方法
        cmd.CommandText = "delete from authors where au_id=2" ;
        cmd.Connection = conn;
        

       conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

ado:SqlDataAdapter的两种不同写法,以及SqlCommand的两种不同写法