在C#中通过存储过程向表中插入数据时出错

时间:2017-10-22 09:59:34

标签: c# sql-server stored-procedures

嗨我有一个表名客户,当我通过存储过程在此表中插入数据时,我收到错误:

存储过程如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

alter procedure [dbo].[customerinsert_new]
(@cid int, @full_name varchar(50),@gender varchar(20),@DOB date,@age varchar(50),@marital_status varchar(20),@email varchar(50),@phone varchar(20),
@Country varchar(50),@State varchar(50),@City Varchar(50),@address varchar(100),@Height varchar(50),@weight varchar(50),@BMI varchar(50),    @Subscription int,@referral varchar(50),@joining_date date,@due_date date,
@amount_reccived varchar(50),@amount_left varchar(50),
@c_photo varchar(100),@primary_facility varchar(50),
@occupation varchar(50),@Medical_aliments varchar(200),
@know_about_sculpt varchar(50),@workout_history varchar(100),
@Fitness_goal varchar(50),@no_days_train_weekly varchar(50),
@no_hours_train_daily varchar(50),@heart_condition varchar(50),
@chest_pain varchar(50),@chest_pain_last_month varchar(50),
@lose_consciousness varchar(50),@bone_joint_problem varchar(50),
@Doctor_currently_prescribing_drugs varchar(50),
@other_reason_not_do_physical_activity varchar(50),
@emergency_name varchar(50),@emergency_relation varchar(50),
@emergency_contact varchar(50),@bid varchar(50),
@device varchar(20),@payment_remarks varchar(50),@orderno varchar(50),
@discount varchar(10),@famt varchar(50))

  as
  begin
  insert into customers values
  (@cid,@full_name,@gender,@DOB,@age,@marital_status,@email,@phone,@Country,     @State,@City,@address,@Height,@weight,@BMI,@Subscription,@referral,@joining_date,@due_date,@amount_reccived,@amount_left,@c_photo,@primary_facility,@occupation,@Medical_aliments,@know_about_sculpt,@workout_history, @Fitness_goal,  @no_days_train_weekly,@no_hours_train_daily,@heart_condition,@chest_pain,
  @chest_pain_last_month,@lose_consciousness,@bone_joint_problem,  @Doctor_currently_prescribing_drugs,@other_reason_not_do_physical_activity,
  @emergency_name,@emergency_relation,@emergency_contact,@bid,@device, @payment_remarks,@orderno,@discount,@famt)
  end

,数据库设计如下:

CREATE TABLE [dbo].[customers](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [cid] [int] NOT NULL,
    [full_name] [varchar](50) NOT NULL,
    [gender] [varchar](20) NOT NULL,
    [DOB] [date] NOT NULL,
    [marital_status] [varchar](20) NULL,
    [email] [varchar](50) NULL,
    [phone] [varchar](20) NULL,
    [Country] [varchar](50) NULL,
    [State] [varchar](50) NULL,
    [City] [varchar](50) NULL,
    [address] [varchar](200) NULL,
    [Height] [varchar](50) NULL,
    [weight] [varchar](50) NULL,
    [BMI] [varchar](50) NULL,
    [Subscription] [int] NULL,
    [referral] [varchar](50) NULL,
    [joining_date] [date] NULL,
    [due_date] [date] NULL,
    [amount_reccived] [varchar](50) NULL,
    [amount_left] [varchar](50) NULL,
    [c_photo] [varchar](500) NULL,
    [age] [varchar](50) NULL,
    [primary_facility] [varchar](50) NULL,
    [occupation] [varchar](50) NULL,
    [Medical_aliments] [varchar](200) NULL,
    [know_about_sculpt] [varchar](50) NULL,
    [workout_history] [varchar](100) NULL,
    [Fitness_goal] [varchar](50) NULL,
    [no_days_train_weekly] [varchar](50) NULL,
    [no_hours_train_daily] [varchar](50) NULL,
    [heart_condition] [varchar](50) NULL,
    [chest_pain] [varchar](50) NULL,
    [chest_pain_last_month] [varchar](50) NULL,
    [lose_consciousness] [varchar](50) NULL,
    [bone_joint_problem] [varchar](50) NULL,
    [Doctor_currently_prescribing_drugs] [varchar](50) NULL,
    [other_reason_not_do_physical_activity] [varchar](50) NULL,
    [emergency_name] [varchar](50) NULL,
    [emergency_relation] [varchar](50) NULL,
    [emergency_contact] [varchar](50) NULL,
    [bid] [varchar](50) NULL,
    [device] [varchar](20) NULL,
    [payment_remarks] [varchar](50) NULL,
    [orderno] [varchar](50) NULL,
    [discount] [varchar](10) NULL,
    [famt] [varchar](50) NULL,

我试图通过探查器获取数据,并且我能够看到数据正确,但是收到错误,在探查器上查询:

exec customerinsert_new @cid='1079',@full_name='Testing',@gender='Male',@dob='2017-10-03',@age='25',@marital_status='Married',
@email='h@h.com',@phone='5',@country='India',@state='Haryana',@city='Asankhurd',@address='H.no. 720 Sector 9', @height='6.0',@weight='80',@BMI='23.92',@Subscription='3',@referral='Friend',@joining_date='2017-09-01',@due_date='2017-01-01',     @amount_reccived='1200',@amount_left='6800',@c_photo='10791.png',@primary_facility='Gym',@occupation='Na',@Medical_aliments='Na',
@know_about_sculpt='Friend',@workout_history='NA',@Fitness_goal='Fat Loss',@no_days_train_weekly='2',@no_hours_train_daily='1',     @heart_condition='No',@chest_pain='No',@chest_pain_last_month='No',@lose_consciousness='No',@bone_joint_problem='Yes',     @Doctor_currently_prescribing_drugs='No',@other_reason_not_do_physical_activity='No',@emergency_name='Mann',@emergency_relation='Na',     @emergency_contact='NA',@bid='2',@device='2',@payment_remarks='Cash',@orderno='INV2YQG2812OWX701XOE70112',@discount='0',@famt='8000'

在上面的查询中,像bmi是一个varchar数据类型字段,但我收到错误:

Msg 245,Level 16,State 1,Procedure customerinsert_new,Line 52 转换varchar值时转换失败' 23.90'数据类型int。

并且对于joined_date我收到的错误是date数据类型:

Msg 241,Level 16,State 1,Procedure customerinsert_new,Line 52 从字符串转换日期和/或时间时转换失败。

以下插入命令成功插入数据:

insert into customers(cid,full_name,gender,DOB,marital_status,email,phone,Country,State,City,address,Height,weight,BMI,Subscription,referral,joining_date,
due_date,amount_reccived,amount_left,c_photo,age,primary_facility,occupation,Medical_aliments,know_about_sculpt,workout_history,Fitness_goal,no_days_train_weekly,
no_hours_train_daily,heart_condition,chest_pain,chest_pain_last_month,lose_consciousness,bone_joint_problem,Doctor_currently_prescribing_drugs,
other_reason_not_do_physical_activity,emergency_name,emergency_relation,emergency_contact,bid,payment_remarks,orderno,discount,famt)
 values('1079','Testing','Male','2017/10/23','Married','h@h.com','5','India','Haryana','Asankhurd','H.no. 720 Sector 9','6.0','80','23.90','3',
 'Friend','2017/09/11','2017/12/11','1200','6800','10791.png','25','Gym','Na','Na','Friend','NA','Fat Loss','2','1','No','No',
 'No','No','Yes','No','No','Mann','Na','NA','2','Cash','INV2YQG2812OWX701XOE70112','0','8000')

1 个答案:

答案 0 :(得分:1)

你的设计应该有一个非常好的理由...将来几乎所有列都使用varchar(xy)将是一个很好的回归...你应该总是将数据放在适当的类型中!

工作命令与发布的命令之间的区别在于明确声明的列列表。你应该从不依赖列符合预期的顺序!没有SELECT *而没有INSERT INTO SomeTable [VALUES|SELECT]

insert into customers values
(@cid,@full_name,@gender,@DOB,@age,@m

将列名称放置在正确的顺序中,如后面发布的命令:

insert into customers(col1, col2, ...) values
(@cid,@full_name,@gender,@DOB,@age,@m ...

我很确定,你的表的DDL列的顺序不同。在c_photo之后,没有age。您没有将值插入到您认为的列中。

你最近更换了桌子吗?在这种情况下,您的过程可能需要重新编译...