我可以插入和读取Excel文件的数据,但无法使用article_no
作为触发器更新数据,
我试过这个
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\crys\Desktop\TEST\Book1.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES""")
cn.Open()
With cm
.Connection = cn
.CommandText = "UPDATE [up$] SET name = '" & TextBox2.Text & "', QC_status ='" & ComboBox1.SelectedItem & "', reason='" & TextBox3.Text & "', date='" & DateTimePicker1.Text & "' WHERE article_no = '" & TextBox1.Text & "'"
If (ExecuteQuery(.CommandText) = True) Then
MsgBox("record updated")
End If
End With
cn.Close()
但它显示错误invalid object 'up$'
。
请有人帮我解决这个问题。
答案 0 :(得分:1)
最后我的查询正确更新excel表,代码如下:
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\crysol\Desktop\TEST\Book1.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES""")
cn.Open()
With cm
.Connection = cn
.CommandText = "update [up$] set [name]=?, [QC_status]=?, [reason]=?, [date]=? WHERE [article_no]=?"
cm = New OleDbCommand(.CommandText, cn)
cm.Parameters.AddWithValue("?", TextBox2.Text)
cm.Parameters.AddWithValue("?", ComboBox1.SelectedItem)
cm.Parameters.AddWithValue("?", TextBox3.Text)
cm.Parameters.AddWithValue("?", DateTimePicker1.Text)
cm.Parameters.AddWithValue("?", TextBox1.Text)
cm.ExecuteNonQuery()
MsgBox("UPDATE SUCCESSFUL")
con.Close()
End With
答案 1 :(得分:0)
一般来说,它是如何完成的。我无法在您的代码中发现任何错误:
Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim myCommand As New System.Data.OleDb.OleDbCommand
Dim sql As String
MyConnection = New System.Data.OleDb.OleDbConnection _
("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + _
"'c:\testfile.xls';Extended Properties=Excel 8.0;")
MyConnection.Open()
myCommand.Connection = MyConnection
sql = "Update [Sheet1$] set name = 'New Name' where id=1"
myCommand.CommandText = sql
myCommand.ExecuteNonQuery()
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
MsgBox("Updated ")
End Sub
End Class
希望这会有所帮助..