Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么sqlParameter请求sourceColumn?_Sql_Sql Server_Database_Parameters_Sqlcommand - Fatal编程技术网

为什么sqlParameter请求sourceColumn?

为什么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

我正在检查Microsoft的一个sqlParameter,并试图理解:

指定a的原因和好处是什么?


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