gpt4 book ai didi

mysql - 使用 VB.NET 从值连接到 mySQL

转载 作者:行者123 更新时间:2023-11-29 07:28:33 31 4
gpt4 key购买 nike

我正在尝试连接到 mySql,但我无法找到我做错的地方。

我的表单包含一个标签,它是一个将发布到 Score 的数字,以及一个文本框,用户可以在其中输入要发布到 Navn 中的姓名。

到目前为止我的代码是:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim connect As New SqlConnection("Data Source = mysqlAdress;Network Library= DBMSSOCN;Initial Catalog=**;User ID=**;Password=**;")
Dim query As String = "INSERT INTO 2048 VALUES(@score, @name)"
Dim command As New SqlCommand(query, connect)

command.Parameters.Add("@score", SqlDbType.Int).Value = Label1.Text
command.Parameters.Add("@navn", SqlDbType.VarChar).Value = TextBox1.Text
connect.Open()
connect.Close()
End Sub

最佳答案

你的表名应该用反引号括起来,如下所示:

Dim query As String = "INSERT INTO `2048` VALUES(@score, @name)"

还要尽量避免使用此类命名约定的表名,即仅使用数字。

您还同时打开和关闭连接。

connect.Open()
connect.Close()

尝试这样:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim connectionString As String = "Data Source = mysqlAdress;Network Library= DBMSSOCN;Initial Catalog=**;User ID=**;Password=**;"
Using SQLConnection As New MySqlConnection(connectionString)
Using sqlCommand As New MySqlCommand()
With sqlCommand
.CommandText = "INSERT INTO `2048` VALUES(@score, @name)"
.Connection = SQLConnection
.CommandType = CommandType.Text
.Parameters.AddWithValue("@score", Label1.Text)
.Parameters.AddWithValue("@name", TextBox1.Text)
End With
Try
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox ex.Message.ToString
Finally
SQLConnection.Close()
End Try
End Using
End Using
End Sub

关于mysql - 使用 VB.NET 从值连接到 mySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608080/

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