我在Visual Studio中遇到UPDATE命令有问题
我明白了。
我有一个MS Access关系数据库后端,我已经在vb中连接它,我可以显示记录等但是我无法让它更新。 我在Form_Load
中有这个Dim strQuery As String = "SELECT ContractID AS [Contract ID], ProductName AS [Product Name], SigningDate AS [Date Signed], StartDate AS [Start Date], EndDate AS [End Date] FROM ((PolicyContract) INNER JOIN Clients on PolicyContract.ClientID = Clients.ClientID) INNER Join Products on PolicyContract.ProductID = Products.ProductID WHERE PolicyContract.ClientID = '" + clientID + "' ORDER BY PolicyContract.ContractID;"
'initialise the data adapter
daPolicyContract = New OleDbDataAdapter(strQuery, connection)
'initialise the dataset
dsAllroundInsurance = New DataSet
'fill the dataset
daPolicyContract.Fill(dsAllroundInsurance, "PolicyContract")
'bind data grid to data in PolicyContract table
dgvPolicies.DataSource = dsAllroundInsurance.Tables("PolicyContract")
'autosize columns to fit grid size
dgvPolicies.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
它工作正常,它显示正确的记录,但我想更新记录
当我单击一个按钮时,我想更新特定合同的“EndDate”值。当我执行此命令时:
UPDATE ((PolicyContract)
INNER JOIN Clients
ON PolicyContract.ClientID = Clients.ClientID)
INNER JOIN Products
ON PolicyContract.ProductID = Products.ProductID
SET PolicyContract.EndDate = '1/1/1111'
WHERE PolicyContract.ContractID = 'C100102';
在MS Access中它正确更新,所以命令没问题。当我尝试在这样的单击事件处理程序中执行命令时:
daPolicyContract.UpdateCommand = New OleDbCommand("UPDATE ((PolicyContract) INNER JOIN Clients ON PolicyContract.ClientID = Clients.ClientID) INNER JOIN Products ON PolicyContract.ProductID = Products.ProductID SET PolicyContract.EndDate = '1/1/1111' WHERE PolicyContract.ContractID = 'C100102';", connection)
没有任何反应,我没有收到错误,但仍然没有更新。我很确定vb正在执行命令,但它没有更新数据库 任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
我无处可见该命令的执行。您必须确保在命令上调用ExecuteNonQuery()
。否则它将无法执行。