2023年6月21日发(作者:)
接收sql语句的返回值⾸先,简要介绍⼀下我们需要什么?我们想在sql中⽤ atch,如果成功,就返回我们查询的值,如果失败就返回-1所以有了以下sql语句(写在后台的) string myInsert = @"begin try
insert into ries values(@categoryName); set @result = (select @@identity id); //设置成功返回值,这⼉是我们查询⾃增id end try
begin catch
set @result = -1;//设置失败返回值-1 end catch";Sql语句已经写好,现在就是需要获取到这个@result,后台代码可以参考⼀下 //定义⼀个数据库执⾏指令 SqlCommand insertCommand = new SqlCommand(myInsert, myConnection); //添加集合 (new SqlParameter() { ParameterName = "categoryName", Value = catename}); //获取到result返回值,主要是这个Output ("@result", ).Direction = ; eNonQuery(); //这个32是⾃定义⽅法,是把查询到的object对象转换为int int result = 32(ters["@result"].Value); if (result > 0) { = result; }
注:此篇随笔只供参考使⽤,⽽且也有很多⼩瑕疵,最主要的不是代码,逻辑才是最重要的。
2023年6月21日发(作者:)
接收sql语句的返回值⾸先,简要介绍⼀下我们需要什么?我们想在sql中⽤ atch,如果成功,就返回我们查询的值,如果失败就返回-1所以有了以下sql语句(写在后台的) string myInsert = @"begin try
insert into ries values(@categoryName); set @result = (select @@identity id); //设置成功返回值,这⼉是我们查询⾃增id end try
begin catch
set @result = -1;//设置失败返回值-1 end catch";Sql语句已经写好,现在就是需要获取到这个@result,后台代码可以参考⼀下 //定义⼀个数据库执⾏指令 SqlCommand insertCommand = new SqlCommand(myInsert, myConnection); //添加集合 (new SqlParameter() { ParameterName = "categoryName", Value = catename}); //获取到result返回值,主要是这个Output ("@result", ).Direction = ; eNonQuery(); //这个32是⾃定义⽅法,是把查询到的object对象转换为int int result = 32(ters["@result"].Value); if (result > 0) { = result; }
注:此篇随笔只供参考使⽤,⽽且也有很多⼩瑕疵,最主要的不是代码,逻辑才是最重要的。
发布评论