【问题标题】:How to update specific column in a table vb.net如何更新表中的特定列 vb.net
【发布时间】:2019-01-23 10:13:01
【问题描述】:

我创建了一个数据库,它包含一个名为 Cust_NColor 的列。用户可以使用单击按钮时出现的颜色对话框为名称文本框选择颜色。我希望将所选颜色值更新到 Cust_NColor 列中的数据库中,并且还想使用 TableAdapterManager 更新单个特定列,或者请建议一种替代方法来更新特定列。

【问题讨论】:

标签: sql-server vb.net


【解决方案1】:

你可以试试下面的代码:

Dim cn As New SqlConnection()
Dim CustomersDataSet As New DataSet()
Dim da As SqlDataAdapter
Dim dr As DataRow        
Dim DAUpdateCmd As SqlCommand


cn.ConnectionString = "Server=.;Database=northwind;UID=sa;PWD=;"
cn.Open()

da = New SqlDataAdapter("select * from CustTest order by CustId", cn)

'Initialize the SqlCommand object that will be used as the DataAdapter's UpdateCommand.
'Note that the WHERE clause uses only the CustId field to locate the record that is to be updated.

DAUpdateCmd = New SqlCommand("Update CustTest set CustName = @pCustName where CustId = @pCustId", da.SelectCommand.Connection)


'Create and append the parameters for the Update command.

DAUpdateCmd.Parameters.Add(New SqlParameter("@pCustName", SqlDbType.VarChar))
DAUpdateCmd.Parameters("@pCustName").SourceVersion = DataRowVersion.Current
DAUpdateCmd.Parameters("@pCustName").SourceColumn = "CustName"

DAUpdateCmd.Parameters.Add(New SqlParameter("@pCustId", SqlDbType.Int))
DAUpdateCmd.Parameters("@pCustId").SourceVersion = DataRowVersion.Original
DAUpdateCmd.Parameters("@pCustId").SourceColumn = "CustId"


'Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter.
da.UpdateCommand = DAUpdateCmd        

da.Fill(CustomersDataSet, "Customers")        

Console.WriteLine("Customer Name before Update : " & CustomersDataSet.Tables("Customers").Rows(0)("CustName"))

CustomersDataSet.Tables("Customers").Rows(0)("CustName") = "Jack"
da.Update(CustomersDataSet, "Customers")        

Console.WriteLine("Customer Name updated successfully")

cn.Close()
Console.ReadLine()

您可以详细了解here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2014-03-08
    相关资源
    最近更新 更多