您好如何使用VB 2010创建与ms access 2010的连接字符串,以便更新并保存在该数据库中 [英] Hi how do I create a connection string with ms access 2010 using VB 2010 also to update and save in that database

查看:78
本文介绍了您好如何使用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

推荐答案

首先,停止这样做!

Do不连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

修复的可能性会同时解决您的问题。

First off, stop doing that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
The chances are that fixing that will solve your problem at the same time.
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的连接字符串,以便更新并保存在该数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆