通过 C# 代码向 SQL 服务器插入 Null(空 TextBox)值时出错
Error while Inserting Null (empty TextBox)values to SQL Server through C# code
我正在使用 C# 代码将一些值插入 SQL 服务器。我在数据库中创建了我的 RemainingPayment
列作为 Decimal(18,2)
数据类型,并且还允许为空。但是当我从 C# 执行查询时,它会抱怨:
Incorrect syntax near ,
请务必注意,我仅针对具有 Decimal(18,2)
数据类型的 RemainingPayment
列收到该错误。对于具有 nvarchar
数据类型的另一列,它工作正常。
这是我的代码:
try
{
using (SqlConnection con = new SqlConnection(ConStr))
{
string query = "INSERT INTO tblExpEntry(RemainingPayment, CHQNo, TransactionID) VALUES (" + tbRemainingPayment.Text + ", '"+tbCHQNo.Text+"', '"+lbl_ID.Text+"')";
using (SqlCommand cmd = new SqlCommand(query,con))
{
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Transaction Successful.", "Created");
generateTransactionID();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
如果问题出在文本框为空时,那么这就是您的答案。您只需将 tbRemainingPayment.Text 放入通常会产生 ...(12.23, 'whatever','whatever') 的字符串中,但如果文本框为空白,您将得到 ...(, 'whatever','whatever')
将文本值解析为小数,因此如果它为空,您将得到零而不是文本错误。
另一个你永远不应该像这样创建 SQL 的原因!
您应该使用参数来执行 sql 命令,而不是字符串连接。参数包含一个 DataType 字段,该字段显式定义传递给数据库引擎的类型,并且不允许从字符串进行转换以引入无效字符。例如,您的区域设置可以将逗号定义为小数点的分隔符,但您的数据库需要一个点。用于将文本框值连接到字符串的 ToString 方法的隐式调用会产生无效输入(当然,文本框中也会缺少值)
使用参数,您的代码将是
decimal remainingPayment;
if(!decimal.TryParse(tbRemainingPayment.Text, out remainingPayment))
{
// Add a message for your user and return, or continue
// inserting the default value for a decimal (that's zero)
}
using (SqlConnection con = new SqlConnection(ConStr))
{
string query = @"INSERT INTO tblExpEntry
(RemainingPayment , CHQNo, TransactionID)
VALUES(@total, @chq, @transid)";
using (SqlCommand cmd = new SqlCommand(query,con))
{
con.Open();
cmd.Parameters.Add("@total", SqlDbType.Decimal).Value = remainingPayment;
cmd.Parameters.Add("@chq", SqlDbType.NVarChar).Value = tbCHQNo.Text;
cmd.Parameters.Add("@transid", SqlDbType.NVarChar).Value = lbl_ID.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Transaction Successful.", "Created");
generateTransactionID();
}
}
我正在使用 C# 代码将一些值插入 SQL 服务器。我在数据库中创建了我的 RemainingPayment
列作为 Decimal(18,2)
数据类型,并且还允许为空。但是当我从 C# 执行查询时,它会抱怨:
Incorrect syntax near ,
请务必注意,我仅针对具有 Decimal(18,2)
数据类型的 RemainingPayment
列收到该错误。对于具有 nvarchar
数据类型的另一列,它工作正常。
这是我的代码:
try
{
using (SqlConnection con = new SqlConnection(ConStr))
{
string query = "INSERT INTO tblExpEntry(RemainingPayment, CHQNo, TransactionID) VALUES (" + tbRemainingPayment.Text + ", '"+tbCHQNo.Text+"', '"+lbl_ID.Text+"')";
using (SqlCommand cmd = new SqlCommand(query,con))
{
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Transaction Successful.", "Created");
generateTransactionID();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
如果问题出在文本框为空时,那么这就是您的答案。您只需将 tbRemainingPayment.Text 放入通常会产生 ...(12.23, 'whatever','whatever') 的字符串中,但如果文本框为空白,您将得到 ...(, 'whatever','whatever')
将文本值解析为小数,因此如果它为空,您将得到零而不是文本错误。
另一个你永远不应该像这样创建 SQL 的原因!
您应该使用参数来执行 sql 命令,而不是字符串连接。参数包含一个 DataType 字段,该字段显式定义传递给数据库引擎的类型,并且不允许从字符串进行转换以引入无效字符。例如,您的区域设置可以将逗号定义为小数点的分隔符,但您的数据库需要一个点。用于将文本框值连接到字符串的 ToString 方法的隐式调用会产生无效输入(当然,文本框中也会缺少值)
使用参数,您的代码将是
decimal remainingPayment;
if(!decimal.TryParse(tbRemainingPayment.Text, out remainingPayment))
{
// Add a message for your user and return, or continue
// inserting the default value for a decimal (that's zero)
}
using (SqlConnection con = new SqlConnection(ConStr))
{
string query = @"INSERT INTO tblExpEntry
(RemainingPayment , CHQNo, TransactionID)
VALUES(@total, @chq, @transid)";
using (SqlCommand cmd = new SqlCommand(query,con))
{
con.Open();
cmd.Parameters.Add("@total", SqlDbType.Decimal).Value = remainingPayment;
cmd.Parameters.Add("@chq", SqlDbType.NVarChar).Value = tbCHQNo.Text;
cmd.Parameters.Add("@transid", SqlDbType.NVarChar).Value = lbl_ID.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Transaction Successful.", "Created");
generateTransactionID();
}
}