您的 SQL 语法 C#.net 有误
You Have an Error on your SQL Syntax C#.net
大家好。我是这个论坛的新手,我想就我的问题寻求帮助。我目前正在创建一个程序,用户可以在其中自己执行查询,或者像 c#.net
中的查询构建器类应用程序一样在短期内执行查询
我正在使用 XAMPP
和 Visual Studio 2012
。
我正在尝试从字符串创建一个 SQL 命令并尝试执行它来填充我的数据网格。但是我收到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'emp_code = 00802' at line 1
我所做的字符串查询是
SELECT * FROM emp_main WHERE emp_code = 00802
当我在 Mysql Syntax Checker 上在线检查时,我的语法没有错误。
这是我在按钮点击事件上的代码
string SelStr = "SELECT * FROM " + tbTblName.Text.Trim().ToString() + " WHERE";
List<string> fildz = new List<string>();
List<string> oper= new List<string>();
List<string> valz = new List<string>();
foreach (Control ctrl in this.panel2.Controls)
{
if (ctrl.Name.Contains("tbField"))
{
fildz.Add(" " + ctrl.Text.ToString());
}
else if (ctrl.Name.Contains("tbOper"))
{
oper.Add(" " + ctrl.Text.ToString());
}
else if (ctrl.Name.Contains("tbVal"))
{
valz.Add(" " + ctrl.Text.ToString() + "AND");
}
}
string finalqry = "";
var results = fildz.Zip(oper, (x, y) => x + y).Zip(valz, (x, y) => x + y);
foreach (var item in results)
{
finalqry += item.ToString();
}
int inx = finalqry.LastIndexOf("AND");
MessageBox.Show( SelStr + finalqry.Substring(0,inx));
try
{
string xqry;
xqry = finalqry.Substring(0, inx).Trim().ToString();
DataTable dt = db.DTquer(xqry);
GridData.DataSource = dt;
GridData.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
这里是我的 DB.cs 文件中抛出错误的代码:
public DataTable DTquer(string thequer)
{
DataTable dt = new DataTable();
MakeCon();
// getConnection().Open();
MySqlCommand cmd = new MySqlCommand(thequer,getConnection());
try
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
getConnection().Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return dt;
}
在此先感谢您的帮助。
你需要做 xqry = SelStr + finalqry.Substring(0, inx).Trim().ToString();
吗?
(当 MessageBox 中显示但此处未显示时,您附加了 SelStr)
大家好。我是这个论坛的新手,我想就我的问题寻求帮助。我目前正在创建一个程序,用户可以在其中自己执行查询,或者像 c#.net
中的查询构建器类应用程序一样在短期内执行查询我正在使用 XAMPP
和 Visual Studio 2012
。
我正在尝试从字符串创建一个 SQL 命令并尝试执行它来填充我的数据网格。但是我收到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'emp_code = 00802' at line 1
我所做的字符串查询是
SELECT * FROM emp_main WHERE emp_code = 00802
当我在 Mysql Syntax Checker 上在线检查时,我的语法没有错误。
这是我在按钮点击事件上的代码
string SelStr = "SELECT * FROM " + tbTblName.Text.Trim().ToString() + " WHERE";
List<string> fildz = new List<string>();
List<string> oper= new List<string>();
List<string> valz = new List<string>();
foreach (Control ctrl in this.panel2.Controls)
{
if (ctrl.Name.Contains("tbField"))
{
fildz.Add(" " + ctrl.Text.ToString());
}
else if (ctrl.Name.Contains("tbOper"))
{
oper.Add(" " + ctrl.Text.ToString());
}
else if (ctrl.Name.Contains("tbVal"))
{
valz.Add(" " + ctrl.Text.ToString() + "AND");
}
}
string finalqry = "";
var results = fildz.Zip(oper, (x, y) => x + y).Zip(valz, (x, y) => x + y);
foreach (var item in results)
{
finalqry += item.ToString();
}
int inx = finalqry.LastIndexOf("AND");
MessageBox.Show( SelStr + finalqry.Substring(0,inx));
try
{
string xqry;
xqry = finalqry.Substring(0, inx).Trim().ToString();
DataTable dt = db.DTquer(xqry);
GridData.DataSource = dt;
GridData.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
这里是我的 DB.cs 文件中抛出错误的代码:
public DataTable DTquer(string thequer)
{
DataTable dt = new DataTable();
MakeCon();
// getConnection().Open();
MySqlCommand cmd = new MySqlCommand(thequer,getConnection());
try
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
getConnection().Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return dt;
}
在此先感谢您的帮助。
你需要做 xqry = SelStr + finalqry.Substring(0, inx).Trim().ToString();
吗?
(当 MessageBox 中显示但此处未显示时,您附加了 SelStr)