Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Mysql cultureinfo.invariantculture_C#_Mysql_Cultureinfo - Fatal编程技术网

C#Mysql cultureinfo.invariantculture

C#Mysql cultureinfo.invariantculture,c#,mysql,cultureinfo,C#,Mysql,Cultureinfo,我的insert请求中有一个双精度值。在这个双精度值中,我将逗号改为一个点,并在创建MySqlCommand时使用12.5而不是12.5,就像在MySQL规则中一样。但我的字符串将我的点改为逗号,并用逗号发送insert请求,然后引发了一个异常。为什么MySqlCommand创建的字符串是12,5而不是12.5 string valueCapteur = "12,5"; valueCapteur = valueCapteur.Replace(',', '.'); double.TryParse(

我的insert请求中有一个双精度值。在这个双精度值中,我将逗号改为一个点,并在创建MySqlCommand时使用12.5而不是12.5,就像在MySQL规则中一样。但我的字符串将我的点改为逗号,并用逗号发送insert请求,然后引发了一个异常。为什么MySqlCommand创建的字符串是12,5而不是12.5

string valueCapteur = "12,5";
valueCapteur = valueCapteur.Replace(',', '.');
double.TryParse(valueCapteur, NumberStyles.Any, CultureInfo.InvariantCulture, out double value);
MySqlCommand cmd = new MySqlCommand
{
    Connection = cnn,
    CommandText = "INSERT INTO `notification`(`Value`) VALUES (" + value + ");"
};
cmd.ExecuteNonQuery();
当我调试值=12.5但在cmd.CommandText值=12.5时,您需要的是参数:

或者使用“整洁”之类的工具:


除了避免SQL注入之外,参数的一个关键点是它避免了区域性和格式问题。

使用参数而不是字符串连接。另外,不要将调试器中显示为字符串的内容与实际值混淆。出现
问题的唯一原因是,如果您将数字存储为字符串,而不是使用正确的类型。要么字段的类型是
varchar
,要么实际代码使用字符串连接。但是。。。但是在SQL中不使用
。。。没有参数,没有连接,也没有插值文字。。。您的SQL根本不涉及
。。。是真正的代码
CommandText=“插入到`notification`(`Value`)值({Value});”
?我的错误值在concat字符串中使用,如下“…值(“+Value+”);我将尝试使用参数并编辑我的问题。
using(MySqlCommand cmd = new MySqlCommand {
    Connection = cnn,
    CommandText = "INSERT INTO `notification`(`Value`) VALUES (@value);"
})
{
    cmd.Parameters.AddWithValue("@value", value);
    cmd.ExecuteNonQuery();
}
conn.Execute("INSERT INTO `notification`(`Value`) VALUES (@value);", new { value });