【发布时间】:2018-03-15 20:28:33
【问题描述】:
我有一个从指定列读取值的函数。它看起来像这样:
private bool OpenConnection() // Just opens the connection. No error here.
{
try
{
conn.Open();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show("Connection not opened");
return false;
}
}
private void getStats(string user, string cate, string score)
{
if (OpenConnection())
{
try
{
string getuserstats = $"SELECT {cate} FROM scores WHERE user = '{user}'";
MySqlCommand cmd = new MySqlCommand(getuserstats, conn);
MySqlDataReader getscore = cmd.ExecuteReader();
MessageBox.Show(getscore.Read().ToString()); //outputs false.
while(getscore.Read())//does not run
{
MessageBox.Show("Reading!");//does not run
score = getscore.GetString(0);//does not run
MessageBox.Show(score); //does not run
}
getscore.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error getting scores!");
conn.Close();
}
}
else
{
MessageBox.Show("Connection not opened!");
}
}
SQL 命令部分似乎运行良好,我用一些不同的查询对其进行了测试,它们都运行良好。
但是,SQL 阅读器本身似乎没有运行。我也没有收到异常错误。
我使用了一个消息框来显示我的读者阅读的布尔值,它输出了 false。这是为什么呢?
【问题讨论】:
-
你试过直接在工作台上运行
getuserstats吗?它会给你任何结果吗? -
是的。我在工作台上运行了 getuserstats,它运行良好。花括号也适用于其他 sql 代码和阅读器。
-
使用调试器并检查 user 和 cate 的值是什么。我们无法重现您的情况。该代码在形式上是正确的,尽管它可用于在您的数据库上启动 Sql Injection hack。
标签: c# mysql sqldatareader