为什么 sqlParameter 请求一个 sourceColumn?
Why does the sqlParameter request a sourceColumn?
我正在检查来自 Microsoft 的 sql参数 example 并试图理解:
指定SourceColumn的原因和好处是什么?
sql命令已经指定了目标列。
command = New SqlCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", connection)
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
在此实例中指定 SourceColumn
意味着在 DataTable
或 Dataset
中指定列名 属性 (DataColumn.ColumnName
)。这仅在您将 SqlDataAdapter
对象与 SqlCommand
结合使用时才重要,如下所示:
--first, fill a DataTable with data from the database:
Dim dt As New DataTable
Dim cmdSelect = New SqlCommand("SELECT CustomerID, CompanyName FROM Customers;", connection)
Dim daCustomers As New SqlDataAdapter
daCustomers.SelectCommand = cmdSelect
daCustomers.Fill(dt)
--Now our DataTable has two columns: "CustomerID" and "CompanyName"
--We can help out our DataAdapter by telling it which columns in the database
--correspond with which columns in the DataTable
--which is what you've done with your fourth argument of .Parameters.Add()
command = New SqlCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", connection)
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
daCustomers.InsertCommand = command
如果我们这样做,那么我们就不必指定两个参数的值(在这种情况下,当 InsertCommand
触发时),因为数据适配器只会查看当您调用 daCustomers.Update(dt)
时,相应 DataColumn
和 DataRow
中的值会自动生成。
同样,如果您希望 SqlDataAdapter
更有用,您还需要为定义的 UpdateCommand
和 [=24= 的参数指定 SourceColumn
]s。请注意,很多人更喜欢使用 SqlCommandBuilder
object to automatically configure 他们的 SqlDataAdapters
用于这个和其他简单的适配器,而不为他们的 SqlCommand
对象指定很多东西,但我更喜欢这个除了最简单的情况外,更明确的方法。
我不知道在您 不使用 a SqlDataAdapter
和 SqlCommand
时指定 SourceColumn
可以获得任何好处.
我正在检查来自 Microsoft 的 sql参数 example 并试图理解:
指定SourceColumn的原因和好处是什么?
sql命令已经指定了目标列。
command = New SqlCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", connection)
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
在此实例中指定 SourceColumn
意味着在 DataTable
或 Dataset
中指定列名 属性 (DataColumn.ColumnName
)。这仅在您将 SqlDataAdapter
对象与 SqlCommand
结合使用时才重要,如下所示:
--first, fill a DataTable with data from the database:
Dim dt As New DataTable
Dim cmdSelect = New SqlCommand("SELECT CustomerID, CompanyName FROM Customers;", connection)
Dim daCustomers As New SqlDataAdapter
daCustomers.SelectCommand = cmdSelect
daCustomers.Fill(dt)
--Now our DataTable has two columns: "CustomerID" and "CompanyName"
--We can help out our DataAdapter by telling it which columns in the database
--correspond with which columns in the DataTable
--which is what you've done with your fourth argument of .Parameters.Add()
command = New SqlCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (@CustomerID, @CompanyName)", connection)
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
daCustomers.InsertCommand = command
如果我们这样做,那么我们就不必指定两个参数的值(在这种情况下,当 InsertCommand
触发时),因为数据适配器只会查看当您调用 daCustomers.Update(dt)
时,相应 DataColumn
和 DataRow
中的值会自动生成。
同样,如果您希望 SqlDataAdapter
更有用,您还需要为定义的 UpdateCommand
和 [=24= 的参数指定 SourceColumn
]s。请注意,很多人更喜欢使用 SqlCommandBuilder
object to automatically configure 他们的 SqlDataAdapters
用于这个和其他简单的适配器,而不为他们的 SqlCommand
对象指定很多东西,但我更喜欢这个除了最简单的情况外,更明确的方法。
我不知道在您 不使用 a SqlDataAdapter
和 SqlCommand
时指定 SourceColumn
可以获得任何好处.