为什么sqlParameter请求sourceColumn?
我正在检查Microsoft的一个sqlParameter,并试图理解: 指定a的原因和好处是什么?为什么sqlParameter请求sourceColumn?,sql,sql-server,database,parameters,sqlcommand,Sql,Sql Server,Database,Parameters,Sqlcommand,我正在检查Microsoft的一个sqlParameter,并试图理解: 指定a的原因和好处是什么? sql命令已经指定了目标列 在本例中指定源列意味着在数据表或数据集中指定列名属性(DataColumn.ColumnName)。只有在将SqlDataAdapter对象与SqlCommand结合使用时,这一点才很重要,例如: --first, fill a DataTable with data from the database: Dim dt As New DataTable Dim cmd
sql命令已经指定了目标列
在本例中指定
源列
意味着在数据表
或数据集
中指定列名属性(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
和DeleteCommand
的参数指定SourceColumn
。请注意,许多人更喜欢使用SqlCommandBuilder
他们的SqlDataAdapters
作为此适配器和其他简单适配器,而不为他们的SqlCommand
对象指定太多内容,但我更喜欢这种更明确的方法,而不是最简单的情况
我不知道在
SqlCommand使用SqlDataAdapter
时指定SourceColumn
会有什么好处,我不明白这个问题。适配器必须知道使用哪一列才能设置值。@Aツ: 在我看来,这是一个公平的问题。我从未使用过此属性,尽管我使用ADO.NET进行了14次访问years@Aツ 在上面的示例中,insert语句提供了源列。“sql命令已经指定了目标列。”-是,但此参数/属性的名称是源列。当DataAdapter
自动填充并使用此参数所附加的命令时,应使用数据集中的哪个列填充此参数?
--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