本文介绍了您好如何使用VB 2010创建与ms access 2010的连接字符串,以便更新并保存在该数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是,如果我使用此代码设置更新顺序或保存顺序,它不能正常工作,我的项目即将到期请帮助....
我尝试了什么:
my problem is that if i use this code to set an update sequence or the saving sequence its not working and my project is due soon please help....
What I have tried:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strConnection = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ROLAND\Documents\Visual Studio 2010\Projects\save reloaded\save reloaded\save.accdb"
Dim cn As New OleDbConnection
Dim CMD As New OleDbCommand
cn.ConnectionString = strConnection
MsgBox("INITIATING CONNECTION")
Try
cn.Open()
CMD.Connection = cn
CMD.CommandText = "INSERT INTO save(ID,name,surname,code,tag) VALUES (" & Me.IDTextBox.Text & ",' " & Me.NameTextBox.Text & "','" & Me.SurnameTextBox.Text & "','" & Me.CodeTextBox.Text & "','" & Me.TagTextBox.Text & "')"
CMD.ExecuteNonQuery()
Catch ex As Exception
MsgBox("ERROR")
End Try
MsgBox("CONNECTION SUCCESS RECORD SAVED.... WELL DONE ROLAND... CAP")
cn.Close()
cn.Dispose()
cn = Nothing
推荐答案
Using con As New SqlConnection(strConnect)
con.Open()
Using com As New SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
com.Parameters.AddWithValue("@C1", myValueForColumn1)
com.Parameters.AddWithValue("@C2", myValueForColumn2)
com.ExecuteNonQuery()
End Using
End Using
如果没有,则可能你试图更新一行而不是INSERT它,因为它已经存在。为此,您需要UPDATE查询而不是INSERT:
If it doesn't then it's possible that you are trying to UPDATE a row instead of INSERTing it, because it already exists. For that, you need an UPDATE query instead of INSERT:
Using con As New SqlConnection(strConnect)
con.Open()
Using com As New SqlCommand("UPDATE myTable SET myColumn1=@C1, myColumn2=@C2 WHERE Id=@ID", con)
com.Parameters.AddWithValue("@ID", id)
com.Parameters.AddWithValue("@C1", myValueForColumn1)
com.Parameters.AddWithValue("@C2", myValueForColumn2)
com.ExecuteNonQuery()
End Using
End Using
这篇关于您好如何使用VB 2010创建与ms access 2010的连接字符串,以便更新并保存在该数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!