简体   繁体   English

如何在asp.net页面中将数据插入MySQL数据库?

[英]How to insert data to MySQL database in asp.net page?

i am trying to use MySQL 5 and asp.net 4 我正在尝试使用MySQL 5和asp.net 4

i succeed withe the linking and display the data from MySQL DB but the problem is within the DML Commands !! 我成功地完成了链接并显示了来自MySQL DB的数据,但问题出在DML命令中!

please take a look to my code 请看看我的代码

Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyConnection As String = "server=localhost;User Id=root;password=123;database=cms"

        Dim Connection As New MySqlConnection(MyConnection)

        Connection.Open()

        Dim Sql As String = "INSERT INTO [CMS.tbimages] ( Title,Description) VALUES (parm2,parm3);"
        Dim cmd As New MySqlCommand(Sql, Connection)

        cmd.Parameters.Add(New MySqlParameter("parm2", "hajjaj"))
        cmd.Parameters.Add(New MySqlParameter("parm3", "hajjaj"))

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class

what i did is a simple page for testing the insert command , I added a button when i click on it it should do the insert command!! 我所做的是一个简单的页面,用于测试插入命令,单击时添加了一个按钮,它应该执行插入命令!

but when i click it it gives me error : 但是当我单击它给我错误:

#42000 You have an error in your SQL syntax; #42000您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '[CMS.tbimages] ( Title,Description) VALUES (parm2,parm3)' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的[[CMS.tbimages](Title,Description)VALUES(parm2,parm3)”附近使用

not : i tried many other ways to change the insert statement : 不是:我尝试了许多其他方法来更改插入语句:

"INSERT INTO [tbimages] ( [Title], [Description]) VALUES ( ? , ?)" “插入[tbimages]([Title],[Description])值(?,?)中”
"INSERT INTO [tbimages] ([Title], [Description]) “插入[tbimages]([标题],[描述])
VALUES (?Title , ?Description)" 值(?标题,?说明)”
INSERT INTO [tbimages] ( [Title], [Description]) VALUES (Title, Description) 插入[tbimages]([标题],[描述])值(标题,描述)

Try this: 尝试这个:

Dim Sql As String = "INSERT INTO CMS.tbimages ( Title,Description) VALUES (?parm2,?parm3);"
Dim cmd As New MySqlCommand(Sql, Connection)

cmd.Parameters.Add(New MySqlParameter("?parm2", "hajjaj"))
cmd.Parameters.Add(New MySqlParameter("?parm3", "hajjaj"))

Also, this tutorial may be of help. 另外, 教程可能会有所帮助。

MySQL doesn't use brackets around identifiers, it uses backticks: MySQL不使用标识符周围的括号,而是使用反引号:

Dim Sql As String = "INSERT INTO `CMS.tbimages` (Title,Description) VALUES (?,?)"

If the table names doesn't cause any conflicts, you don't need to enclose it in backticks either: 如果表名没有引起任何冲突,则也不需要将其括在反引号中:

Dim Sql As String = "INSERT INTO CMS.tbimages (Title,Description) VALUES (?,?)"

I'm not sure how your database driver handles parameters, but I think they should be unnamed. 我不确定您的数据库驱动程序如何处理参数,但我认为应将其命名。

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

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