使用存储过程进行多次插入查询NOT WORKING

时间:2016-06-24 08:42:00

标签: c# sql-server stored-procedures

当我插入单个记录时,它已成功插入但无法插入多个记录。我收到以下错误消息

  

错误:描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪   有关错误及其来源的更多信息   码。异常详细信息:System.Data.SqlClient.SqlException:错误   将数据类型nvarchar转换为datetime。

我的Sql表

create table railbkg
(

[type] nvarchar(50),
sr nvarchar(50),
tran_no varchar(50),
pnr varchar(50),
trainno varchar(50),
fromstn varchar(50),
fromstn1 varchar(50),
boardingst nvarchar(50),
tostn varchar(50),
tostn1 varchar(50),
doj datetime,
currentstatus varchar(50),
chartprepared varchar(50),
seatno varchar(50),
status varchar(50),
dob varchar(50),
bookid varchar(50),
mth varchar(50),
basicamt nvarchar(50),
fullname1 nvarchar(50),
fullname nvarchar(50),
pax varchar(50),
class varchar(50),
btc varchar(50),
pgc_waiver nvarchar(50),
c nvarchar(50),
ichg nvarchar(50),
stonichg nvarchar(50),
bist nvarchar(50),
pgc nvarchar(50),
stonpg nvarchar(50),
netcc nvarchar(50),
pnr nvarchar(50),
pgcwamt nvarchar(50),
pgcwamtst nvarchar(50),
camt nvarchar(50),
finalamt nvarchar(50),
cancel nvarchar(50),
refund nvarchar(50),
refamt nvarchar(50),
rcptamt nvarchar(50),
refdate varchar(50),
moj varchar(50),
remarks varchar(50),
temp varchar(50),
asignstatus nvarchar(50),
id int not null,
cc_Id int,
rfid int,
statementid nvarchar(50)

)
  

我的存储过程

SP_HELPTEXT SPInsrailbkgCY
CREATE procedure SPInsrailbkgCY          
@type nvarchar(50),          
@sr nvarchar(50),          
@Tran_no varchar(20),          
@pnr varchar(10),          
@trainno varchar(5),          
@fromstn varchar(50),          
@boardingst nvarchar(50),          
@tostn varchar(50),          
@doj datetime,          
@currentstatus varchar(50),          
@seatno varchar(50),          
@status varchar(50),          
@dob datetime,          
@bookid varchar(50),          
@mth varchar(50),          
@basicamt nvarchar(50),          
@Fullname nvarchar(50),          
@pax varchar(50),          
@class varchar(50),          
@btc varchar(50),          
@pgc_waiver nvarchar(50),          
@C nvarchar(50),          
@ichg nvarchar(50),  
--@stonichg nvarchar(50),          
--@bist nvarchar(50),          
@pgc nvarchar(50),          
--@stonpg nvarchar(50),          
--@netcc nvarchar(50),          
@pnr1 nvarchar(50),          
@pgcwamt nvarchar(50),          
@pgcwamtst nvarchar(50),          
--@Camt nvarchar(50),          
--@finalamt nvarchar(50),          
@cancel nvarchar(50),          
@refamt nvarchar(50),          
@rcptamt nvarchar(50),          
@refdate varchar (50),          
@moj varchar(50),          
@remarks varchar(50),          
@temp varchar(50),          
@asignstatus nvarchar(50)         
as          
begin    
declare @ST float    
declare @stonichg as nvarchar(50)       
declare @bist as nvarchar(50)        
declare @pgcamt as nvarchar(50)        
declare @stonpg as nvarchar(50)        
declare @netcc as nvarchar(50)        
declare @Camt as nvarchar(50)         
declare @finalamt as nvarchar(50)        
declare @fcal as nvarchar(50)  
declare @fpgc as nvarchar(50)  
set @st=0.15   
set @fcal= Cast(@basicamt as float)+cast(@ichg as Float)  
 set @fpgc=cast(@pgc as float)  
set @stonichg= round(convert(float,@ichg)*0.145,2)          
--set @bist=(@basicamt+@ichg+@stonichg)  
set @bist= @fcal+ round(convert(float,@ichg)*0.145,2)   
set @pgcamt= round((@fcal+round(convert(float,@ichg)*0.145,2))* @fpgc/100,2)          
set @stonpg= round(round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)*0.145,2)        
set @netcc= @fcal+round(convert(float,@ichg)*0.145,2)+round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)+round(round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)*0.145,2)        
set @camt= (@fcal+round(convert(float,@ichg)*0.145,2)+round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)+round(round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)*0.145,2))*0.03        
set @finalamt= @fcal+round(convert(float,@ichg)*0.145,2)+round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)+round(round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)*0.145,2)+(@fcal+round(convert(float,@ichg)*0.145,2)+round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)+round(round((@fcal+round(convert(float,@ichg)*0.145,2))*@fpgc/100,2)*0.145,2))*0.03        
insert into           
railbkg           
(          
[type]  
,sr          
,Tran_no          
,pnr          
,trainno          
,fromstn          
,boardingst          
,tostn          
,doj          
,currentstatus          
,seatno          
,status          
,dob          
,bookid          
,mth          
,basicamt          
,Fullname          
,pax          
,class          
,btc          
,pgc_waiver          
,C          
,ichg          
,stonichg          
,bist          
,pgc          
,stonpg          
,netcc          
,pnr1          
,pgcwamt          
,pgcwamtst          
,Camt          
,finalamt          
,cancel          
,refamt          
,rcptamt          
,refdate          
,moj          
,remarks          
,temp          
,asignstatus          
)          
values           
(          
null          
,null          
,@Tran_no          
,@pnr          
,@trainno          
,@fromstn          
,@boardingst          
,@tostn          
,@doj          
,@currentstatus          
,@seatno          
,@status          
,@dob          
,@bookid          
,null          
,convert(float,@basicamt)          
,@Fullname          
,@pax          
,@class          
,@btc          
,@pgc_waiver          
,@C          
,convert(float,@ichg)          
,@stonichg        
,@bist        
,@pgcamt  
,@stonpg  
,@netcc              
,@pnr          
,0          
,0          
,@camt        
,@finalamt        
,null          
,null          
,null          
,null          
,convert(datetime,@doj)          
,@remarks          
,null          
,0          
)       
end

我的C#代码

SqlCommand comm = new SqlCommand("SPInsrailbkgcy");//insert in railbkg if commission is yes
                    comm.Connection = con; 
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.Parameters.AddWithValue("@type",DBNull.Value);
                    comm.Parameters.AddWithValue("@sr",DBNull.Value);
                    comm.Parameters.AddWithValue("@Tran_no", lb1.Text);
                    comm.Parameters.AddWithValue("@pnr",lb2.Text);
                    comm.Parameters.AddWithValue("@trainno",lb3.Text);
                    comm.Parameters.AddWithValue("@fromstn",lb4.Text);
                    comm.Parameters.AddWithValue("@boardingst",ddlbroadst.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@tostn",lb5.Text);
                    comm.Parameters.AddWithValue("@doj",lb6.Text);
                    comm.Parameters.AddWithValue("@currentstatus",ddlsts.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@seatno",txtstno.Text);
                    comm.Parameters.AddWithValue("@status",lb7.Text);
                    comm.Parameters.AddWithValue("@dob",lb8.Text);
                    comm.Parameters.AddWithValue("@bookid",dlstprofile.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@mth", DBNull.Value);
                    comm.Parameters.AddWithValue("@basicamt",txt1.Text);
                    comm.Parameters.AddWithValue("@Fullname",dlst1.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@pax",lb10.Text);
                    comm.Parameters.AddWithValue("@class",lb9.Text);
                    comm.Parameters.AddWithValue("@btc",lb11.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@pgc_waiver",dlst3.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@c",dlst4.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@ichg",lb12.Text);
                    comm.Parameters.AddWithValue("@pgc",dlst5.SelectedItem.Text);
                    comm.Parameters.AddWithValue("@pnr1",lb2.Text);
                    comm.Parameters.AddWithValue("@pgcwamt","0");
                    comm.Parameters.AddWithValue("@pgcwamtst","0");
                    comm.Parameters.AddWithValue("@cancel",DBNull.Value);
                    comm.Parameters.AddWithValue("@refamt",DBNull.Value);
                    comm.Parameters.AddWithValue("@rcptamt", DBNull.Value);
                    comm.Parameters.AddWithValue("@refdate",DBNull.Value);
                    comm.Parameters.AddWithValue("@moj",lb6.Text);
                    comm.Parameters.AddWithValue("@remarks",txt2.Text);
                    comm.Parameters.AddWithValue("@temp",DBNull.Value);
                    comm.Parameters.AddWithValue("@asignstatus", "0");
                    con.Open();
                    comm.ExecuteNonQuery();
                    Page.ClientScript.RegisterStartupScript(this.GetType(),
                                 "alert2", "alert2();", true);
                    con.Close();
                    sendmail(lb2.Text);

我不明白这段代码的错误

1 个答案:

答案 0 :(得分:1)

更改这些行:

comm.Parameters.AddWithValue("@dob",lb8.Text);
comm.Parameters.AddWithValue("@doj",lb6.Text);

到这些:

comm.Parameters.Add("@dob", SqlDbType.DateTime).Value = Convert.ToDateTime(lb8.Text);
comm.Parameters.Add("@doj", SqlDbType.DateTime).Value = Convert.ToDateTime(lb6.Text);

从文本框接收值时,所有日期时间列都应转换为日期时间,默认情况下返回字符串值。

或者,通过转换dob& amp;编辑您的SP doj to inserttime in insert query:

CREATE procedure SPInsrailbkgCY (
-- simplified for brevity --     
@doj datetime,          
@dob datetime,
-- simplified for brevity --        
as          
begin
-- simplified for brevity --        
insert into railbkg (  
-- simplified for brevity --
,doj   
,dob 
-- simplified for brevity --         
)          
values           
( 
-- simplified for brevity --
,convert(datetime,@doj)    
,convert(datetime,@dob) 
-- simplified for brevity --
)