SqlDataAdapter的创建和使用

来源:互联网 发布:java编程题库 编辑:程序博客网 时间:2024/05/01 19:39

 

1 。 创建及使用

string ConnectString = "user id =" + this._User + ";data source= " + this._Server + ";initial catalog= " + this._DB + ";password=" + this._Pwd;

SqlConnection cn = new SqlConnection();
cn.ConnectionString = ConnectString;

string sSQL = "select * from Meet_Manager where MagName='" + Magname + "'";

SqlDataAdapter da = new SqlDataAdapter(sSQL, cn);

填充给datatable:

DataTable dtManage = new DataTable();
da.Fill(dtManage);

填充给dataset:

dataset ds = new dataset();

da.Fill(ds,"mytable");

注意:

使用SqlDataAdapter 时,不用使用Connection 对象的Open方法,因为SqlDataAdapter会自动开放一个连接,然后提交查询,获取结果,最后关闭连接。如果Fill方法之前就开放了Connection ,那么Connection在操作之后仍然保持开放的状态。

2 . 处理批查询

string strConn,strSQL

strConn = "Porvider = ..."

strSQL = "select * from customers ;"+

"select * from orders";

SqlDataAdapter da = new SqlDataAdapter (strSQL,strConn);

da.TableMappings.Add("Table","customers");

da.TableMappings.Add("Table1","orders");

DataSet ds = new DataSet();

da.Fill(ds)

原创粉丝点击