)我们可以通过以下三种方法来创建SqlDataAdapter对象:
1、通过连接字符串和查询语句
string strConn,strSQL;
strConn=@"Data Source=.\SQLEXPRESS;"+"Initial Catalog=Northwind;Integrated Security=True;";
strSql="SELECT CustomerID,CompanyName FROM Customers";
SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);
这种方法有一个潜在的缺陷。假设应用程序中需要多个SqlDataAdapter对象,用这种方式来创建的话,会导致创建每个SqlDataAdapter时,都同时创建一个新的SqlConnection对象,方法二可以解决这个问题
2、通过查询语句和SqlConnection对象来创建
string strConn,strSQL;
strConn=@"Data Source=.\SQLEXPRESS;"+"Initial Catalog=Northwind;Integrated Security=True;";
SqlConnection cn=new SqlConnection(strConn);
SqlDataAdapter daCustomers,daOrders;
strSql="SELECT CustomerID,CompanyName FROM Customers";
daCustomers=new SqlDataAdapter(strSql,cn);
strSql="SELECT OrderID,CustomerID,OrderDate FROM Orders";
daOrders=new SqlDataAdapter(strSql,cn);
3、通过SqlCommand对象来创建
string strConn,strSQL;
strConn=@"Data Source=.\SQLEXPRESS;"+"Initial Catalog=Northwind;Integrated Security=True;";
strSql="SELECT CustomerID,CompanyName FROM Customers";
SqlConnection cn=new SqlConnection(strConn);SqlCommand cmd=new SqlCommand(strSql,cn);
SqlDataAdapter da=new SqlDataAdapter(cmd);