无法识别 MySql 查询中的 C# 变量
C# variable in a MySql query isn't recognized
我的 C# 代码中有一个 int 变量,我正尝试在 MySql 查询中使用它。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace projeV1
{
public partial class ProjeDetay : Form
{
public ProjeDetay()
{
InitializeComponent();
}
private void ProjeDetay_Load(object sender, EventArgs e)
{
int satir_projem = Projeler.satir_proje;
string connectionString = "Server = localhost; Port = ...; Database = ...; Uid = root; Pwd = ...; Allow User Variables=True";
MySqlConnection databaseConnection = new MySqlConnection(connectionString);
MySqlCommand command;
databaseConnection.Open();
command = databaseConnection.CreateCommand();
string myQuery = "select projeAdi from projeler where projeID = @satir_projem";
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(myQuery, databaseConnection);
command.Parameters.Add("@satir_projem", MySqlDbType.Int32).Value = satir_projem;
DataSet DS = new DataSet();
dataAdapter.Fill(DS);
dataGridView_ProjeDetay.DataSource = DS.Tables[0];
MessageBox.Show(Projeler.satir_proje.ToString());
MessageBox.Show(satir_projem.ToString());
}
}
}
(很抱歉,如果它在编码方面看起来一团糟,但我是新手 ^^)
MessageBox windows 正确显示变量值,因此变量没有问题。例如,当我将查询字符串中的 @satir_projem
替换为“2”之类的数字时(参见下面的示例),结果是正确的,但是当我在查询中使用 @satir_projem 时,它不起作用。我看不出我做错了什么。
示例查询字符串:
"select projeAdi from projeler where projeID = 2"
P.S 1:最初我试图在名为 "Projeler" 的表单中使用的 DataGridView 中获取所选行的索引(这是名为 Projeler.satir_proje 的变量)在另一种形式(称为 ProjeDetay)中,将该索引值分配给另一个称为 "satir_projem" 的变量,并使用该值从我的数据库中获取相关数据,并将该数据放入位于我的第二种形式中的另一个 DataGridView 中(称为 dataGridView_ProjeDetay).
P.S 2:我对这个问题做了很多研究,并尝试了很多我遇到的解决方案;但是,其中 none 对我有用。所以,我在这里 :)
提前致谢。
您正在为未使用的命令设置参数。
相反,你应该使用 DataAdapter.SelectCommand
dataAdapter.SelectCommand.Parameters.Add(...)
MySqlConnection databaseConnection = new MySqlConnection(connectionString);
MySqlCommand command;
string myQuery = "select projeAdi from projeler where projeID = @satir_projem";
databaseConnection.Open();
command = new MySqlCommand(myQuery, databaseConnection);
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
只是想详细说明一下,为什么@Onur_Saatcioglu 的代码会抛出错误。
您正在向命令中添加@satir_projem,但是这样添加到命令中的参数没有传递给数据适配器,数据适配器充当数据集和 SQL 服务器之间的桥梁,用于检索和保存数据。
因此你可以
1) 正如 Lester 所说,创建像
这样的命令
command = new MySqlCommand(myQuery, databaseConnection);
并将命令传递给 DataAdapter
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
2) 正如@Pablo_notPicasso所说,如果你使用的是
dataAdapter.SelectCommand.Parameters.AddWithValue("@satir_projem", satir_projem);
那么"command"没有意义,直接删除即可。
我的 C# 代码中有一个 int 变量,我正尝试在 MySql 查询中使用它。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace projeV1
{
public partial class ProjeDetay : Form
{
public ProjeDetay()
{
InitializeComponent();
}
private void ProjeDetay_Load(object sender, EventArgs e)
{
int satir_projem = Projeler.satir_proje;
string connectionString = "Server = localhost; Port = ...; Database = ...; Uid = root; Pwd = ...; Allow User Variables=True";
MySqlConnection databaseConnection = new MySqlConnection(connectionString);
MySqlCommand command;
databaseConnection.Open();
command = databaseConnection.CreateCommand();
string myQuery = "select projeAdi from projeler where projeID = @satir_projem";
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(myQuery, databaseConnection);
command.Parameters.Add("@satir_projem", MySqlDbType.Int32).Value = satir_projem;
DataSet DS = new DataSet();
dataAdapter.Fill(DS);
dataGridView_ProjeDetay.DataSource = DS.Tables[0];
MessageBox.Show(Projeler.satir_proje.ToString());
MessageBox.Show(satir_projem.ToString());
}
}
}
(很抱歉,如果它在编码方面看起来一团糟,但我是新手 ^^)
MessageBox windows 正确显示变量值,因此变量没有问题。例如,当我将查询字符串中的 @satir_projem
替换为“2”之类的数字时(参见下面的示例),结果是正确的,但是当我在查询中使用 @satir_projem 时,它不起作用。我看不出我做错了什么。
示例查询字符串:
"select projeAdi from projeler where projeID = 2"
P.S 1:最初我试图在名为 "Projeler" 的表单中使用的 DataGridView 中获取所选行的索引(这是名为 Projeler.satir_proje 的变量)在另一种形式(称为 ProjeDetay)中,将该索引值分配给另一个称为 "satir_projem" 的变量,并使用该值从我的数据库中获取相关数据,并将该数据放入位于我的第二种形式中的另一个 DataGridView 中(称为 dataGridView_ProjeDetay).
P.S 2:我对这个问题做了很多研究,并尝试了很多我遇到的解决方案;但是,其中 none 对我有用。所以,我在这里 :)
提前致谢。
您正在为未使用的命令设置参数。
相反,你应该使用 DataAdapter.SelectCommand
dataAdapter.SelectCommand.Parameters.Add(...)
MySqlConnection databaseConnection = new MySqlConnection(connectionString);
MySqlCommand command;
string myQuery = "select projeAdi from projeler where projeID = @satir_projem";
databaseConnection.Open();
command = new MySqlCommand(myQuery, databaseConnection);
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
只是想详细说明一下,为什么@Onur_Saatcioglu 的代码会抛出错误。
您正在向命令中添加@satir_projem,但是这样添加到命令中的参数没有传递给数据适配器,数据适配器充当数据集和 SQL 服务器之间的桥梁,用于检索和保存数据。
因此你可以
1) 正如 Lester 所说,创建像
这样的命令command = new MySqlCommand(myQuery, databaseConnection);
并将命令传递给 DataAdapter
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
2) 正如@Pablo_notPicasso所说,如果你使用的是
dataAdapter.SelectCommand.Parameters.AddWithValue("@satir_projem", satir_projem);
那么"command"没有意义,直接删除即可。