我试图让我的程序导入程序中输入的数据,以更新MySQL中的表。我查看了代码并做了一些研究,它看起来应该可以工作,但数据没有被写入MySQL服务器。任何帮助将不胜感激。下面是我的VB代码和MySQL存储过程。
Public Class OrePriceUpdate
Dim Mysqlconn As MySqlConnection
Dim cmd As MySqlCommand
Private Sub Update_Prices_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Update_Prices_Button.Click
Mysqlconn = New MySqlConnection()
Mysqlconn.ConnectionString = "server=xxxxxx;Uid=xxxx;Pwd=xxxxxx;database=xxxx"
Try
Mysqlconn.Open()
cmd = New MySqlCommand()
cmd.Connection = Mysqlconn
cmd.CommandText = "update_ore_prices"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@veldspar", Veldspar_Isk.Text)
Catch myerror As MySqlException
MessageBox.Show(myerror.Message)
Finally
Mysqlconn.Dispose()
End Try
End Sub
SQL查询
DELIMITER $$
USE `YHI`$$
DROP PROCEDURE IF EXISTS `update_ore_prices`$$
CREATE DEFINER=`YHI`@`%` PROCEDURE `update_ore_prices`(
IN veldspar DECIMAL(10,2)
BEGIN
INSERT INTO Ore_Ice_Prices(
Veldspar
VALUES(
veldspar);
END$$
DELIMITER ;
答案 0 :(得分:0)
您打开连接,创建命令对象,但实际上并未执行它。完成添加参数后,使用以下命令执行:
cmd.ExecuteNonQuery()
有关详细信息,请尝试此link。特别是有关于执行命令的部分:
通常,插入,更新和删除用于写入或更改数据 数据库,而Select用于读取数据。出于这个原因,我们 有不同类型的方法来执行这些查询。方法 如下:
ExecuteNonQuery: Used to execute a command that will not return any data, for example Insert, update or delete. ExecuteReader: Used to execute a command that will return 0 or more records, for example Select. ExecuteScalar: Used to execute a command that will return only 1 value, for example Select Count(*).