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

标签 asp.net mysql sql-server vb.net

我正在尝试使用 MySQL 5 和 asp.net 4

我成功链接并显示了来自 MySQL 数据库的数据,但问题出在 DML 命令中!!

请看我的代码

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

我所做的是一个用于测试插入命令的简单页面,我添加了一个按钮,当我点击它时它应该执行插入命令!!

但是当我点击它时它给我错误:

#42000 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 '[CMS.tbimages] ( Title,Description) VALUES (parm2,parm3)' at line 1

不是:我尝试了许多其他方法来更改插入语句:

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

最佳答案

试试这个:

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"))

此外,this教程可能会有所帮助。

关于asp.net - 如何在asp.net页面中向MySQL数据库中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4772783/

相关文章:

c# - 在 ASP.Net Core 中获取基本 URL

asp.net - 将 ASP.NET 身份存储移动到 EF Sql 数据库

php - 遍历家庭、类别和子类别

sql - 在 FROM 之前在 SQL Server 中创建别名?

SQL Server 更新查询不起作用,但是,SQL 表示行受到影响

mysql - 获取错误代码 : 8155 No column was specified for column 2 of 'pol' in SQL server

asp.net - Telerik RadGrid 具有不同类型的编辑控件

c# - 无法为表中的标识列插入显式值

php - 从 DOM 获取 HTML 并将其存储在 MySql 中

mysql - 在同一个表中选择