【发布时间】:2019-07-16 12:24:03
【问题描述】:
第一个 SQL 正在执行,但第二个 SQL 似乎不起作用。 当我将查询更改为第一个查询时,它工作得很好,但是当我这样说时,由于某种原因它似乎不起作用。
我刚刚开始学习 MySQL,我真的在努力学习这门语言并理解这门语言。
//Classic One that checks if the hwid is there
public void checkHWID(string HWID)
{
string line;
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE HWID = @HWID", con))
{
cmd.Parameters.AddWithValue("@HWID", HWID);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
line = reader[1].ToString();
Console.Write(line);
con.Close();
}
else
{
updateHWID(HWID);
}
}
}
}
}
//This one doesn't seem to update the hwid but when i change the query to the first one it works just fine
public void updateHWID(String HWID)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("INSERT INTO USERS(hwid) VALUES(@HWID)", connection))
{
command.Parameters.AddWithValue("@HWID", HWID);
connection.Close();
}
}
}
【问题讨论】:
-
updateHWID中没有对ExecuteNonQuery的呼叫。 (另外,您可能不想调用 inserts 行updateHWID的函数,这只是自找麻烦。) -
使用 Try Catch 看看它为什么不起作用。您是要插入还是更新?
-
@JeroenMostert 有什么不好/不正确的原因吗?
-
一个名为
updateHWID的方法强烈建议它将执行UPDATE,而不是INSERT。可以想象,您有一个系统,其中通过在USERS表中插入一行来实现更高级别的“更新 HWID”——可能性不大。
标签: c# sql sql-insert