将值插入数据库 [英] inserting values to database

查看:97
本文介绍了将值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试通过使用insert into语句将值插入数据库,但成功完成了bulid进程,但是在运行时我遇到了错误,即("INSERT INTO语句中的语法错误"),



i had tried to insert the values to the database by using insert into statement , the bulid process is succeeded , but at the runtime i am getting the error, that ("Syntax error in INSERT INTO statement.") ,



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Customer.mdb")
        con.Open()
        Dim s1 As String = "insert into dbadd(input,output,project)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "') "
        cmd = New OleDbCommand(s1, con)
        cmd.ExecuteNonQuery()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        MsgBox("Successfully inserted")
    End Sub

推荐答案

查询中存在语法错误.有许多种写查询的方法来将数据插入数据库.请尝试以下一种方法:
There is Syntax error in your query.There are so many ways to write query to insert data into database.Try these one :
Dim objda As New OleDbDataAdapter
obj.SelectCommnd=New OledbCommand
objda.SelectCommand.CommandText="Insert into TableName (Field1, Field2, Field3) values(@Field1, @Field2, @Field3)"
objda.SelectCommand.Parameters.AddWithValue("@Field1", TextBox1.Text)
objda.SelectCommand.Parameters.AddWithValue("@Field2", TextBox2.Text)
objda.SelectCommand.Parameters.AddWithValue("@Field3", TextBox3.Text)
   Try                                     'To handle OleDb Exceptions
       objda.SelectCommand.ExecuteNonQuery()
   Catch ex As OleDb.OleDbException
       MessageBox.Show(ex.Message)
   End Try


希望对您有所帮助:)


I hope it will help you :)


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Customer.mdb")
        con.Open()
TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        Dim s1 As String = "insert into dbadd(input,output,project)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "') "
        cmd = New OleDbCommand(s1, con)
        cmd.ExecuteNonQuery()

        MsgBox("Successfully inserted")
    End Sub





尝试说说它现在是否有效





try and say whether it works now


这篇关于将值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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