gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:21 28 4
gpt4 key购买 nike

我正在尝试使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com