主题:c#中连接数据库SqlDataAdapter的用法
YinQing
[专家分:0] 发布于 2005-09-19 21:43:00
SqlDataAdapter(string,System.Data.SqlClient.SqlConnection)
SqlDataAdapter(string,string)
SqlDataAdapter(System.Data.SqlClient.SqlCommand)
SqlDataAdapter()
以上四个语句的区别是什么?
请指教,谢谢!
沙发
ryowu [专家分:6470] 发布于 2005-09-20 11:00:00
SqlDataAdapter 构造函数 [C#]
初始化 SqlDataAdapter 类的新实例。
重载列表
初始化 SqlDataAdapter 类的新实例。
受 .NET Framework 精简版的支持。
[Visual Basic] Public Sub New()
[C#] public SqlDataAdapter();
[C++] public: SqlDataAdapter();
[JScript] public function SqlDataAdapter();
将指定的 SqlCommand 作为 SelectCommand 属性,初始化 SqlDataAdapter 类的新实例。
受 .NET Framework 精简版的支持。
[Visual Basic] Public Sub New(SqlCommand)
[C#] public SqlDataAdapter(SqlCommand);
[C++] public: SqlDataAdapter(SqlCommand*);
[JScript] public function SqlDataAdapter(SqlCommand);
使用 SelectCommand 和 SqlConnection 对象初始化 SqlDataAdapter 类的新实例。
受 .NET Framework 精简版的支持。
[Visual Basic] Public Sub New(String, SqlConnection)
[C#] public SqlDataAdapter(string, SqlConnection);
[C++] public: SqlDataAdapter(String*, SqlConnection*);
[JScript] public function SqlDataAdapter(String, SqlConnection);
用 SelectCommand 和一个连接字符串初始化 SqlDataAdapter 类的新实例。
受 .NET Framework 精简版的支持。
[Visual Basic] Public Sub New(String, String)
[C#] public SqlDataAdapter(string, string);
[C++] public: SqlDataAdapter(String*, String*);
[JScript] public function SqlDataAdapter(String, String);
示例
[Visual Basic, C#] 下面的示例将创建一个 SqlDataAdapter 并设置它的一些属性。
[Visual Basic, C#] 注意 此示例显示如何使用 SqlDataAdapter 构造函数的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Public Sub CreateSqlDataAdapter()
Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers", _
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
Dim custConn as SqlConnection = custDA.SelectCommand.Connection
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
custDA.InsertCommand = New SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", custConn)
custDA.UpdateCommand = New SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " & _
"WHERE CustomerID = @oldCustomerID", custConn)
custDA.DeleteCommand = New SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", custConn)
custDA.InsertCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID")
custDA.InsertCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar, 40, "CompanyName")
custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID")
custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar, 40, "CompanyName")
custDA.UpdateCommand.Parameters.Add("@oldCustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original
custDA.DeleteCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original
End Sub
[C#]
public static void CreateSqlDataAdapter()
{
SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers",
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
SqlConnection custConn = custDA.SelectCommand.Connection;
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
custDA.InsertCommand = new SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", custConn);
custDA.UpdateCommand = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", custConn);
custDA.DeleteCommand = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", custConn);
custDA.InsertCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID");
custDA.InsertCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar, 40, "CompanyName");
custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID");
custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar, 40, "CompanyName");
custDA.UpdateCommand.Parameters.Add("@oldCustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
custDA.DeleteCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
}