简体   繁体   English

如何在 VB.NET 查询中使用 MySQL 变量?

[英]How to use MySQL variables in a VB.NET query?

When using a query with MySQL variables from VB.NET I get an error当使用来自 VB.NET 的 MySQL 变量的查询时,出现错误

Fatal error encountered during command execution.

The problem is when trying to use a MySQL variable like @sum in a VB.NET string, this variable is recognized as a parameter of the MySqlCommand and not as part of the string itself.问题是当尝试在 VB.NET 字符串中使用 @sum 之类的@sum变量时,该变量被识别为 MySqlCommand 的参数,而不是字符串本身的一部分。

Here is the code that I use.这是我使用的代码。

dim query as string = "SET @sum = 0; SELECT @sum := (@sum + debe - haber) AS 'Saldo' from cccustomers WHERE customer = 443"
using con
    If con.State = ConnectionState.Closed Then con.Open()
    Using cmd As New MySqlCommand(query, con)
        Dim adp As New MySqlDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        DataGridView1.DataSource = dt
    end using
end using

I also tried this with exactly the same result:我也试过这个,结果完全一样:

dim sums as string = "@sum"
dim query as string = "SET " & sums & " = 0; SELECT " & sums & " := (" & sums & " + debe - haber) AS 'Saldo' from cccustomers WHERE customer = 443"

And I also tried this:我也试过这个:

dim query as string = "SET @sum = 0; SELECT @sum := (@sum + debe - haber) AS 'Saldo' from cccustomers WHERE customer = 443"
using con
    If con.State = ConnectionState.Closed Then con.Open()
    Using cmd As New MySqlCommand(query, con)
        cmd.Parameters.AddWithValue("@sum", "@sum")
        Dim adp As New MySqlDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        DataGridView1.DataSource = dt
    end using
end using

But in this case, the problem is that it passes me the variable as a string with quotes '@sum' and that gives a different error in the query.但在这种情况下,问题在于它将变量作为带引号'@sum'的字符串传递给我,这在查询中给出了不同的错误。

If I make that same query directly in phpmyadmin it works correctly, the problem I have is: How can I use the MySQL variable in VB.net?如果我直接在 phpmyadmin 中进行相同的查询,它工作正常,我遇到的问题是:如何在 VB.net 中使用 MySQL 变量?

I need the variable because in each line I have to show the accumulated balance and not only show the balance of each particular line.我需要这个变量,因为在每一行中我都必须显示累计余额,而不仅仅是显示每一行的余额。

Thanks for your time.谢谢你的时间。

EDIT: Table Definition, Sample Data and expected Output编辑:表定义、示例数据和预期 Output

CREATE TABLE `stack` (
  `id` int(11) NOT NULL,
  `debe` double NOT NULL,
  `haber` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Volcado de datos para la tabla `stack`
--

INSERT INTO `stack` (`id`, `debe`, `haber`) VALUES
(1, 100, 0),
(2, 100, 0),
(3, 100, 0),
(4, 100, 0),
(5, 0, 50),
(6, 100, 0),
(7, 0, 150),
(8, 100, 0),
(9, 100, 0),
(10, 100, 0),
(11, 0, 100),
(12, 100, 0);

--
-- Índices para tablas volcadas
--

--
-- Indices de la tabla `stack`
--
ALTER TABLE `stack`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT de las tablas volcadas
--

--
-- AUTO_INCREMENT de la tabla `stack`
--
ALTER TABLE `stack`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
COMMIT;

The Query: (works on PhpMyadmin but NOT WORK on VB.NET because the symbol @ in MySqlCommand expects a parameter - I don't want to send a parameter, I want to Send a MySql variable )查询:(适用于 PhpMyadmin 但不适用于 VB.NET,因为MySqlCommand中的符号@需要一个参数 - 我不想发送参数,我想发送一个 MySql 变量

SET @sum = 0; 
SELECT debe, haber, @sum := (@sum + debe - haber) AS 'Saldo' from stack;

Expected output:预计 output:

在 PhpMyAdmin 上查询结果

Code on VB.NET with the SAME QUERY: VB.NET 上的代码与相同的查询:

dim query as string = "SET @sum = 0; SELECT debe, haber, @sum := (@sum + debe - haber) AS 'Saldo' from stack;"
using con
    If con.State = ConnectionState.Closed Then con.Open()
    Using cmd As New MySqlCommand(query, con)
        Dim adp As New MySqlDataAdapter(cmd)
        Dim dt As New DataTable
        adp.Fill(dt)
        DataGridView1.DataSource = dt
    end using
end using

Result on VB.NET结果 VB.NET

在此处输入图像描述

This exception means that @sum is expected to be defined as a parameter , but I don't want to define a parameter, I want to declare a MySQL variable .这个异常意味着@sum应该被定义为一个parameter ,但是我不想定义一个参数,我想声明一个MySQL变量

To use MySQL variables in a VB.NET query, you'll need to use the MySqlCommand class and its Parameters property.要在 VB.NET 查询中使用 MySQL 变量,您需要使用 MySqlCommand class 及其参数属性。 Here's an example of how to do it:以下是如何操作的示例:

Dim conn As New MySqlConnection("connection string")
Dim cmd As New MySqlCommand("SELECT * FROM table WHERE column = @param1 AND 
column2 = @param2", conn)

cmd.Parameters.AddWithValue("@param1", "value1")
cmd.Parameters.AddWithValue("@param2", "value2")

conn.Open()
Dim reader As MySqlDataReader = cmd.ExecuteReader()

' Process the data returned by the query
While reader.Read()
  ' Access column values using reader("column_name")
End While

reader.Close()
conn.Close()

In this example, we create a MySqlCommand object and specify a SQL query with placeholders for the variables (@param1 and @param2).在此示例中,我们创建一个 MySqlCommand object 并指定一个 SQL 查询,其中包含变量(@param1 和@param2)的占位符。 Then, we use the Parameters property to add values for the variables using the AddWithValue() method.然后,我们使用 Parameters 属性通过 AddWithValue() 方法为变量添加值。

When the query is executed, the placeholders are replaced with the actual values specified in the Parameters collection.执行查询时,占位符将替换为 Parameters 集合中指定的实际值。 This allows you to use variables in your queries in a safe and efficient way, as the values are automatically escaped and properly formatted.这允许您以安全有效的方式在查询中使用变量,因为值会自动转义并正确格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM