)我们可以通过以下三种方法来创建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);

相关文章:

  • 2022-02-08
  • 2022-01-14
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-30
  • 2021-06-16
  • 2021-09-27
  • 2021-11-12
  • 2021-11-12
  • 2021-10-13
  • 2021-12-03
相关资源
相似解决方案