C# 准备好的报表保存'@名称';将0个值添加到表中
我正在用C#构建一个应用程序,需要将数据放入mySQL数据库。为此目的,我使用事先准备好的声明。使用更新查询的类似方法可以正常工作,但是当我执行此函数时,数据没有正确插入查询,导致数据库包含文本@username、@password值,其余列包含0C# 准备好的报表保存'@名称';将0个值添加到表中,c#,mysql,C#,Mysql,我正在用C#构建一个应用程序,需要将数据放入mySQL数据库。为此目的,我使用事先准备好的声明。使用更新查询的类似方法可以正常工作,但是当我执行此函数时,数据没有正确插入查询,导致数据库包含文本@username、@password值,其余列包含0 public static void Register(trinityRPG.World.Player player, string hashedPassword) { MySqlConnection db = Databas
public static void Register(trinityRPG.World.Player player, string hashedPassword)
{
MySqlConnection db = Database.Core.getConnection();
try
{
db.Open();
string CommandText = "INSERT INTO users (`id`, `username`, `password`, `x`, `y`, `z`, `a`, `health`, `armour`, `skin`) VALUES (NULL, '@username', '@password', '@x', '@y', '@z', '@a', '@health', '@amour', '@skin');";
MySqlCommand cmd = new MySqlCommand(CommandText, db);
cmd.Prepare();
Console.WriteLine("Adding " + player.Name + " into the database");
cmd.Parameters.AddWithValue("@username", player.Name);
cmd.Parameters.AddWithValue("@password", hashedPassword);
cmd.Parameters.AddWithValue("@x", 71.1727);
cmd.Parameters.AddWithValue("@y", -100.62);
cmd.Parameters.AddWithValue("@z", 0.616854);
cmd.Parameters.AddWithValue("@a", 255.659);
cmd.Parameters.AddWithValue("@health", 100);
cmd.Parameters.AddWithValue("@armour", 0);
cmd.Parameters.AddWithValue("@skin", 2);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("[DB driver] Error: " + ex.Message + ex.StackTrace + ex.Source);
}
finally
{
if (db != null)
{
db.Close();
}
}
}
插入行的外观示例:
id username password x y z a health armour skin
8 @username @password 0 0 0 0 0 0 0
ID是一个带有主键的int,自动递增。用户名和密码是字符串。x、 y、z、a、健康和盔甲都是浮标,皮肤是智力
任何帮助都将不胜感激 好吧,您不需要在命令中对参数使用单引号。只要改变你的想法
'@username'
到
以及所有其他参数。使用“@username”
,您的命令将其视为,而不是参数
还可以使用来自动处理MySqlConnection
和MySqlCommand
而不是手动调用.Close()
方法。不要尽可能多地使用AddWithValue
方法。使用.Add()
重载指定参数db类型及其大小
using(MySqlConnection db= new MySqlConnection(conString))
using(MySqlCommand cmd = db.CreateCommand())
{
cmd.CommandText = @"INSERT INTO users (`id`, `username`, `password`, `x`, `y`, `z`, `a`, `health`, `armour`, `skin`)
VALUES (NULL, @username, @password, @x, @y, @z, @a, @health, @amour, @skin)";
cmd.Parameters.AddWithValue("@username", player.Name);
cmd.Parameters.AddWithValue("@password", hashedPassword);
cmd.Parameters.AddWithValue("@x", 71.1727);
cmd.Parameters.AddWithValue("@y", -100.62);
cmd.Parameters.AddWithValue("@z", 0.616854);
cmd.Parameters.AddWithValue("@a", 255.659);
cmd.Parameters.AddWithValue("@health", 100);
cmd.Parameters.AddWithValue("@armour", 0);
cmd.Parameters.AddWithValue("@skin", 2);
db.Open();
cmd.ExecuteNonQuery();
}
顺便说一句,他说得很好
MySQL正在隐式转换字符串“@somethingthatisnotanumber”
对于键入为整数的列,设置为0。没有这种转换
对于char列,这就是显示“@ParameterNameInString”的原因
从字面上看,它们都是
SQL语句在“值”部分的参数名称周围有字符串文字引号:
VALUES (NULL, '@username', '@password', '@x', '@y', '@z', '@a', '@health', '@amour', '@skin');
这指示MySQL为username参数插入字符串文字'@username'。只需删除引号:
VALUES (NULL, @username, @password, @x, @y, @z, @a, @health, @amour, @skin);
当我更改该值时,在执行查询期间会发生以下异常:[DB driver]错误:在命令执行期间遇到致命错误。在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior行为)[0x00000]in:0在MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()[0x00000]in:0在MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()[0x00000]in:0在trinitypg.Database.Player.Register(trinityRPG.World.Player播放器,System.String哈希密码)[0x00000]在:0@FlorianVanDillen我在你的代码中没有看到任何
ExecuteReader
。在哪一行引发此异常?@FlorianVanDillen我也添加了一个示例代码。你能试试吗?因为你没有告诉你的列类型,所以我必须在我的示例中使用AddWithValue
。你的示例代码工作得非常完美。非常感谢!FWIW:MySQL对于键入为整数的列,正在将字符串“@somethingthatisnotanumber”隐式转换为0。对于char列,没有此类转换,这就是“@parameternameinastring”在其中显示的原因。
VALUES (NULL, @username, @password, @x, @y, @z, @a, @health, @amour, @skin);