简体   繁体   English

如何使用vb从asp.net插入mysql?

[英]How to insert to mysql from asp.net using vb?

I am trying to insert into a mysql table. 我正在尝试插入mysql表。 Here is my code... 这是我的代码...

        Dim cmdText = "INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') values (@cid,@bus,@notey,@datey)"
    Dim conn As New MySqlConnection("server=theipofserver;user id=id;password=mypass;database=thedatabase")
    Using cmd = New MySqlCommand(cmdText, conn)
        conn.Open()
        Dim testy As String
        testy = TextBox1.Text
        Dim dater = Date.Today + TimeOfDay
        cmd.Parameters.AddWithValue("@cid", c_id)
        cmd.Parameters.AddWithValue("@bus", c_business)
        cmd.Parameters.AddWithValue("@notey", testy)
        cmd.Parameters.AddWithValue("@datey", dater)
        cmd.ExecuteNonQuery()
    End Using

But, I get the following error: 但是,出现以下错误:

MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''cust_id', 'business', 'note', 'date_stamp') values ('ID','null','first one'' at line 1

the table I am trying to insert into has 4 columns that are all varchars with plenty of space (varchar(100)) 我要插入的表有4列都是具有足够空间的varchars(varchar(100))

Here are more details on the error... error occurs on line 31... 以下是有关错误的更多详细信息...错误发生在第31行...

Line 29:             cmd.Parameters.AddWithValue("@notey", testy)
Line 30:             cmd.Parameters.AddWithValue("@datey", dater)
Line 31:             cmd.ExecuteNonQuery()
Line 32:         End Using
Line 33:         TextBox1.Font.Bold = True

So, what am I doing wrong here? 那么,我在这里做错了什么? Any tips on better ways to insert into mysql tables would be greatly appreciated! 任何更好的方法插入MySQL表的技巧将不胜感激!

Don't use quotes around table or column names. 请勿在表名或列名前后使用引号。

INSERT INTO complete_log (`cust_id`, `business`, `note`, `date_stamp`)
values (@cid,@bus,@notey,@datey)

If you want to escape table and column names then use backticks. 如果要转义表名和列名,请使用反引号。 But you only need to escape reserved words in MySQL . 但是您只需要在MySQL中转义保留字即可

Do not specify column names in apostrophies 不要在撇号中指定列名

Instead of 代替

Dim cmdText = "INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') values (@cid,@bus,@notey,@datey)"

Do

Dim cmdText = "INSERT INTO complete_log (cust_id, business, note, date_stamp) values (@cid,@bus,@notey,@datey)"

Change 更改

INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp')

to

INSERT INTO complete_log (cust_id, business, note, date_stamp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM