[英]How can I insert into the table using SQL Server
I am stuck in this condition unable to insert into the table tbl_customer its giving error :我陷入这种情况无法插入表 tbl_customer 其给出错误:
Arithmetic overflow error converting expression to data type int.将表达式转换为数据类型 int 时出现算术溢出错误。
The statement has been terminated.该语句已终止。
here is my table structure:这是我的表结构:
create table tbl_customer(
id int identity primary key,
cust_name varchar(50) NOT NULL,
filecode varchar(20) NOT NULL,
cust_mobile int,
cust_cnic varchar(50) NOT NULL,
cust_phone int,
cust_address varchar(200)
)
and here is the code i use to insert:这是我用来插入的代码:
insert into tbl_customer values('Jonah Gordian','LHR001',03451119182,'11-22112-122',1212121212,'abc street 12')
and I used this code in c# to try inserting:我在 c# 中使用此代码尝试插入:
connclass.insert("insert into tbl_customer(cust_name,filecode,cust_mobile,cust_cnic,cust_phone,cust_address) VALUES('" + txtname.Text + "','" + txtfilecode.Text + "','" + int.Parse(txtmob.Text) + "','" + txtcnic.Text + "','" + int.Parse(txtphone.Text) + "','" + txtaddress.Text + "')");
Try like this,试试这样,
CREATE TABLE tbl_customer (
id INT identity PRIMARY KEY
,cust_name VARCHAR(50) NOT NULL
,filecode VARCHAR(20) NOT NULL
,cust_mobile BIGINT --or Varchar(20)
,cust_cnic VARCHAR(50) NOT NULL
,cust_phone INT
,cust_address VARCHAR(200)
)
INSERT INTO tbl_customer
VALUES (
'Jonah Gordian'
,'LHR001'
,03451119182
,'11-22112-122'
,1212121212
,'abc street 12'
)
You have exceeded the int datatype limit.您已超出 int 数据类型限制。 Change the datatype from int to either bigint or Varchar to resolve the issue.将数据类型从 int 更改为 bigint 或 Varchar 以解决该问题。
Note: If you need leading Zeros then you can choose Varchar otherwise you can make use of BigInt.注意:如果您需要前导零,那么您可以选择 Varchar,否则您可以使用 BigInt。
You define cust_mobile
as int
, but try to insert 03451119182
, which is clearly over the limit of 2147483647
.您将cust_mobile
定义为int
,但尝试插入03451119182
,这显然超出了2147483647
的限制。
Change to bigint
or store as a VarChar (including the leading zero).更改为bigint
或存储为 VarChar(包括前导零)。
create table tbl_customer(
id int identity primary key,
cust_name varchar(50) NOT NULL,
filecode varchar(20) NOT NULL,
cust_mobile varchar(20),
cust_cnic varchar(50) NOT NULL,
cust_phone varchar(20),
cust_address varchar(200)
)
insert into tbl_customer
(cust_name, filecode, cust_mobile, cust_cnic, cust_phone, cust_address )
values
('Jonah Gordian','LHR001','03451119182','11-22112-122','1212121212','abc street 12');
And also C# code is open to SQL injection attack, use parameters instead.而且 C# 代码对 SQL 注入攻击开放,请改用参数。 ie: IE:
string sql = @"insert into tbl_customer
(cust_name,filecode,cust_mobile,cust_cnic,cust_phone,cust_address)
VALUES
(@cust_name,@filecode,@cust_mobile,@cust_cnic,@cust_phone,@cust_address)";
using (SqlConnection con = new SqlConnection(@"server=.\SQLExpress;database=yourDbName;Trusted_Connection=yes"))
{
var cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@cust_name", txtname.Text);
cmd.Parameters.AddWithValue("@filecode", txtfilecode.Text);
cmd.Parameters.AddWithValue("@cust_mobile", txtmob.Text);
cmd.Parameters.AddWithValue("@cust_cnic", txtcnic.Text);
cmd.Parameters.AddWithValue("@cust_phone", txtphone.Text);
cmd.Parameters.AddWithValue("@cust_address", txtaddress.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
You exceeded the limit of the int
try with bigint
你超出了使用bigint
尝试int
的限制
this value 3451119182这个值3451119182
see in this link the limits在此链接中查看限制
https://msdn.microsoft.com/pt-br/library/ms187745(v=sql.120).aspx https://msdn.microsoft.com/pt-br/library/ms187745(v=sql.120).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.