当前位置: 代码迷 >> C# >> C# 执行SQL 存储过程失败:当前命令发生了严重异常.应放弃任何可能产生的结果
  详细解决方案

C# 执行SQL 存储过程失败:当前命令发生了严重异常.应放弃任何可能产生的结果

热度:90   发布时间:2016-05-05 04:24:37.0
C# 执行SQL 存储过程失败:当前命令发生了严重错误.应放弃任何可能产生的结果
C#调用存储过程的时候就报错:当前命令发生了严重错误.应放弃任何可能产生的结果
我把存储过程里面的代码全注释也出错
把我C#执行的语句放到T-Sql里面执行就可以
求大神!!!
------解决思路----------------------
1、任意写一个存储过程,比如

create procedure P_Test
as
select 1
,执行看会不会有这种错误
2、如果依然有这种错误,把你的C#代码贴出来
------解决思路----------------------
首先存储过程含义raiserror('some wrong occur',16,11),在执行ExecuteNonQuery的时候,异常会直接抛出

我也很好奇你的问题,简单的测试了下,没有你的异常。还是检查你的代码吧

以下是我的测试

create table Moduel_PrintManage(ModuleID VARCHAR(100),Name VARCHAR(100),PrintFormat TEXT,IsDefault Bit)

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE P_Test
-- Add the parameters for the stored procedure here
@ModuleID VARCHAR(100),
@Name VARCHAR(200),
@PrintFormat TEXT,
@IsDefault BIT
AS
BEGIN
IF exists(select * from Moduel_PrintManage where ModuleID = @ModuleID and Name = @Name)
begin
declare @Message varchar(200)
set @Message = '打印格式已存在【' + isnull(@Name,'') + '】'
raiserror(@Message,16,11)
end
else
begin
insert into Moduel_PrintManage values(@ModuleID,@Name,@PrintFormat,@IsDefault)
if @@error <> 0
raiserror('some wrong occur',16,11)
end
END



 static void Main(string[] args)
        {
            Console.WriteLine(GetTextByEventKey("subscribe"));

            XDocument document = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "SignUp.xml");


            Console.WriteLine(document.Descendants("text").Select(x => x.Value).ToArray()[0]);

            SqlConnection connection = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=ibcs;");
            connection.Open();
            SqlCommand cmd = new SqlCommand();
            SqlParameter[] _parmaeters = new SqlParameter[] { 
                new SqlParameter("@ModuleID","12222"),
                new SqlParameter("@Name","Good"),
                new SqlParameter("@PrintFormat",""),
                new SqlParameter("@IsDefault","0")
            };
            _parmaeters[2].Value = document.ToString();
            cmd.Parameters.AddRange(_parmaeters);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "P_Test";
            cmd.Connection = connection;
            cmd.ExecuteNonQuery();

            Console.ReadKey();
        }

------解决思路----------------------

这个调试方法不准确。你只少要看看 InnerException,怎么能只看最浅的一个SqlException呢?
  相关解决方案