如何在此代码中解决Sql语法错误 [英] How to Solve Sql syntax error in this code

查看:48
本文介绍了如何在此代码中解决Sql语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string cb = "insert into Stock(StockID,ConfigID,Quantity,TotalPrice,StockDate)Values(@StockID,@ConfigID,@Quantity,@TotalPrice,@StockDate)";
 
                cmd = new SqlCommand(cb,con);
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@StockID", txtStockID.Text);
                cmd.Parameters.AddWithValue("@ConfigID",txtConfigID.Text);
                    cmd.Parameters.AddWithValue("@Quantity",txtQty.Text);
                        cmd.Parameters.AddWithValue("@TotalPrice",txtTotalPrice.Text);
                            cmd.Parameters.AddWithValue("@StockDate",dtpStockDate.Value.ToShortDateString());
 
                cmd.ExecuteReader();

推荐答案

Conversion failed when converting the nvarchar value 'S-311678' to data type int.
note :here S-311678 is the Stock Id which is auto generated





我认为错误是自我解释。在您的数据库中,StockID是一个整数,您正在尝试存储一个字符串。更改数据库中的数据类型或存储数字,不带S -



I think the error is self explaining. In you Database, StockID is an Integer,and you're trying to store a string. Change the datatype in your database or store a number,without the "S-"


con.Open();

cmd.ExecuteNonQuery();

con.Close();
con.Open();
cmd.ExecuteNonQuery();
con.Close();


而不是写cmd.ExecuteReader();写下面的代码来执行查询



Instead of writing "cmd.ExecuteReader();" write below code to execute the query

con.Open();
using(con)
{
     cmd.ExecuteNonQuery();
}


这篇关于如何在此代码中解决Sql语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆