忽略插入字符串 C# 中的引号,SQL 服务器
Ignoring Quotes in Insert String C#, SQL Server
我无法将 NULL
值插入数据库中的 Date
列,因为我需要使用字符串变量。
插入语句:
string insertQuery = "INSERT INTO PersonelListesi (id, ssk_tarih) VALUES(1,'" + sskTarihi + "')";
工作变量示例:
string sskTarihi = "20201212";
不正确的变量示例(我试过):
sskTarihi = "NULL";
System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'
sskTarihi = "'NULL'";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
sskTarihi = "\"'NULL'\"";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
sskTarihi = "\"NULL\"";
System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'
sskTarihi = "\'NULL\'";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
sskTarihi = "\"\'NULL\'\"";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
我希望你明白我的意思和我需要的。感谢您的帮助。提前感谢您的回答。 :)
正如其他人推荐的那样,使用如下所示的参数,其中方法位于 class。
public static void InsertRow(int id, string ssk_tarih)
{
using (var cn = new SqlConnection {ConnectionString = "TODO"})
{
using (var cmd = new SqlCommand {Connection = cn})
{
cmd.CommandText = "INSERT INTO PersonelListesi (id, ssk_tarih) VALUES(@id,@ssk_tarih)";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id;
if (string.IsNullOrWhiteSpace(ssk_tarih))
{
cmd.Parameters.Add("@ssk_tarih", SqlDbType.DateTime).Value = DBNull.Value;
}
else
{
cmd.Parameters.Add("@ssk_tarih", SqlDbType.DateTime).Value = ssk_tarih;
}
cn.Open();
cmd.ExecuteNonQuery();
}
}
}
我无法将 NULL
值插入数据库中的 Date
列,因为我需要使用字符串变量。
插入语句:
string insertQuery = "INSERT INTO PersonelListesi (id, ssk_tarih) VALUES(1,'" + sskTarihi + "')";
工作变量示例:
string sskTarihi = "20201212";
不正确的变量示例(我试过):
sskTarihi = "NULL";
System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'
sskTarihi = "'NULL'";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
sskTarihi = "\"'NULL'\"";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
sskTarihi = "\"NULL\"";
System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'
sskTarihi = "\'NULL\'";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
sskTarihi = "\"\'NULL\'\"";
System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'NULL'
我希望你明白我的意思和我需要的。感谢您的帮助。提前感谢您的回答。 :)
正如其他人推荐的那样,使用如下所示的参数,其中方法位于 class。
public static void InsertRow(int id, string ssk_tarih)
{
using (var cn = new SqlConnection {ConnectionString = "TODO"})
{
using (var cmd = new SqlCommand {Connection = cn})
{
cmd.CommandText = "INSERT INTO PersonelListesi (id, ssk_tarih) VALUES(@id,@ssk_tarih)";
cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id;
if (string.IsNullOrWhiteSpace(ssk_tarih))
{
cmd.Parameters.Add("@ssk_tarih", SqlDbType.DateTime).Value = DBNull.Value;
}
else
{
cmd.Parameters.Add("@ssk_tarih", SqlDbType.DateTime).Value = ssk_tarih;
}
cn.Open();
cmd.ExecuteNonQuery();
}
}
}