vb.net mysql 插入数据,无法使用VB.NET在MySQL中插入数据

Hi,

I''ve being searching all over the internet to know what is wrong with my code. I am unable to insert data into a table in MySQL. I have a set of text boxes where and a button so when I click the button, all the data in the textboxes are added to a MySQL Table. Within the code of the button I have a function that perform the insert task.Public Function InsertCar() As Boolean

Dim iReturn As Boolean

Using SQLConnection As New MySqlConnection(connectionString)

Using sqlCommand As New MySqlCommand()

With sqlCommand

.CommandText = "INSERT INTO members_car (`car_id`, `member_id`, `model`, `color`, `chassis_id`, `plate_number`, `code`) values (@xid,@m_id,@imodel,@icolor,@ch_id,@pt_num,@icode)"

.Connection = SQLConnection

.CommandType = CommandType.Text

.Parameters.AddWithValue("@xid", TextBox20.Text)

.Parameters.AddWithValue("@m_id", TextBox20.Text)

.Parameters.AddWithValue("@imodel", TextBox23.Text)

.Parameters.AddWithValue("@icolor", TextBox24.Text)

.Parameters.AddWithValue("@ch_id", TextBox22.Text)

.Parameters.AddWithValue("@pt_num", TextBox21.Text)

.Parameters.AddWithValue("@icode", ComboBox1.SelectedItem)

.ExecuteNonQuery()

End With

Try

SQLConnection.Open()

sqlCommand.ExecuteNonQuery()

iReturn = True

Catch ex As MySqlException

MsgBox(ex.Message.ToString)

iReturn = False

Finally

SQLConnection.Close()

End Try

End Using

End Using

Return iReturn

End Function

Whenever I press the button, nothing happen and the table remains empty.

解决方案Well no, it won''t

You load up all the parameters, and then execute the non-query - but you haven''t opened the connection yet!

When that is done (and it will throw and exception) you then enter the try block, and open the connection and have another go...

Take out the ExecuteNonQuery call in the WITH block.

Below code will work

Changes

1) Open connection

2) Use of string builder to build insert query

Public Function InsertCar() As Boolean

Dim iReturn As Boolean

Using SQLConnection As New MySqlConnection(connectionString)

SQLConnection.Open()

Dim MSqlQuery As New StringBuilder

MSqlQuery.AppendFormat("INSERT INTO members_car (`car_id`, `member_id`, `model`, `color`, `chassis_id`, `plate_number`, `code`) values ({0},{1},""{2}"",""{3}"",{4},{5},""{6}"")", TextBox20.Text, TextBox20.Text, TextBox23.Text, TextBox24.Text, TextBox22.Text, TextBox21.Text, ComboBox1.SelectedItem)

Using sqlCommand As New MySqlCommand()

With sqlCommand

.CommandText = SqlQuery.ToString()

.Connection = SQLConnection

.CommandType = CommandType.Text

.Parameters.AddWithValue("@xid", TextBox20.Text)

.Parameters.AddWithValue("@m_id", TextBox20.Text)

.Parameters.AddWithValue("@imodel", TextBox23.Text)

.Parameters.AddWithValue("@icolor", TextBox24.Text)

.Parameters.AddWithValue("@ch_id", TextBox22.Text)

.Parameters.AddWithValue("@pt_num", TextBox21.Text)

.Parameters.AddWithValue("@icode", ComboBox1.SelectedItem)

.ExecuteNonQuery()

End With

Try

SQLConnection.Open()

sqlCommand.ExecuteNonQuery()

iReturn = True

Catch ex As MySqlException

MsgBox(ex.Message.ToString)

iReturn = False

Finally

SQLConnection.Close()

End Try

End Using

End Using

Return iReturn

End Function

Hope this helps if yes then accept and vote my answer otherwise revert back with your queries

--Rahul D.

VB.NET MySQL 数据库插入一条数据,您可以使用 MySQL Connector/NET 这个官方提供的 ADO.NET 数据提供程序来实现。以下是一个示例代码: 首先,确保您已经安装了 MySQL Connector/NET 并将其添加到您的项目。 然后,您需要在 VB.NET 代码引入以下命名空间: ```vb Imports MySql.Data.MySqlClient ``` 接下来,您可以使用以下代码将数据插入MySQL 数据库: ```vb 'Define MySQL连接字符串 Dim connStr As String = "server=your_server;user=your_username;password=your_password;database=your_database" '创建MySQL连接 Using conn As New MySqlConnection(connStr) '打开数据库连接 conn.Open() '创建SQL插入语句 Dim sql As String = "INSERT INTO your_table (column1, column2, column3) VALUES (@value1, @value2, @value3)" '创建MySQL命令对象 Using cmd As New MySqlCommand(sql, conn) '添加参数并设置参数值 cmd.Parameters.AddWithValue("@value1", "Value1") cmd.Parameters.AddWithValue("@value2", "Value2") cmd.Parameters.AddWithValue("@value3", "Value3") '执行SQL语句 cmd.ExecuteNonQuery() End Using '关闭数据库连接 conn.Close() End Using ``` 上述代码,您需要将 `your_server`、`your_username`、`your_password`、`your_database` 替换为实际的 MySQL 服务器地址、用户名、密码和数据库名。同时,将 `your_table` 替换为要插入数据的表名,并根据表结构设置正确的列和参数。 通过上述代码,您可以向 MySQL 数据库的指定表插入一条数据。请注意,在实际应用,您可能需要进行错误处理、参数验证和其他安全性措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值