gpt4 book ai didi

c# - MySql 语法错误 : You have an error in your SQL syntax near '' at line 1

转载 作者:行者123 更新时间:2023-12-01 00:22:41 24 4
gpt4 key购买 nike

编辑: 为这个愚蠢的问题道歉。我自己注意力不集中,再加上我仍然只是在学习 SQL 语法,这让我错过了这个问题。

原始问题:我不知道这里发生了什么。我尝试了几种插入数据的方法,但到目前为止都没有奏效,而且我总是遇到同样的异常。我不确定是什么导致了这个问题。我是否缺少命令功能或语法有问题?

string uName = "tess";
string uPwd = "newpassword";
int authorityLevel = 3;

try
{
mySqlCommand = mySqlConnect.CreateCommand();
mySqlCommand.CommandText = "INSERT INTO Users(UserName, UserPassword, AuthorityLevel) VALUES(\"" + uName + "\", \"" + uPwd + "\", " + authorityLevel + ";";
mySqlCommand.ExecuteNonQuery();
}
catch ((MySql.Data.MySqlClient.MySqlException e)
{
Console.WriteLine(e.Message);
}

// Output:
// 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 '' at line 1

最佳答案

你忘记了一个右尖括号 ) 在

mySqlCommand.CommandText = "INSERT INTO Users(UserName, UserPassword, AuthorityLevel) VALUES(\"" + uName + "\", \"" + uPwd + "\", " + authorityLevel + ";";

用这个代替

mySqlCommand.CommandText = "INSERT INTO Users(UserName, UserPassword, AuthorityLevel) VALUES(\"" + uName + "\", \"" + uPwd + "\", " + authorityLevel + ");";

并且您添加了一个额外的左尖括号(在

catch ((MySql.Data.MySqlClient.MySqlException e)

替换为

catch (MySql.Data.MySqlClient.MySqlException e)

关于c# - MySql 语法错误 : You have an error in your SQL syntax near '' at line 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21474288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com