类型' System.Data.SqlClient.SqlException'的例外情况发生在System.Data.dll中但未在用户代码中处理

时间:2016-01-19 05:15:43

标签: c# sql-server

我得到错误:

  

'System.Data.SqlClient.SqlException'中出现System.Data.dll类型的例外但未在用户代码中处理

     

其他信息:无效的对象名称sp_VehicleInfo

代码:

protected void btnRegister_Click(object sender, EventArgs e)
{
        VehicleInfo vehicle = new VehicleInfo();
        vehicle.VehicleNo = txtVehicleNo.Text;
        vehicle.Make = txtMake.Text;
        vehicle.Model = txtEmail.Text;
        vehicle.Variety = txtVersion.Text;
        //vehicle.isAc = radAC.Text;
        vehicle.EngineNo = TextBox1.Text;
        vehicle.LExpDate = txtLicenseExpDate.Text;
        vehicle.InsuranceNo = TextBox2.Text;
        vehicle.IExpDate = txtInsuranceExpDate.Text;
        vehicle.Insert(); 
}

这是存储过程

[dbo].[sp_VehicleInfo]              
    @VehicleNo NVARCHAR(50) = NULL,
    @Make NVARCHAR(50) = NULL,
    @Model NVARCHAR(50) = NULL,
    @Variety NVARCHAR(50) = NULL,
    @isAc BIT = NULL,
    @EngineNo NVARCHAR(50) = NULL,
    @LExpDate NVARCHAR(50) = NULL,
    @InsuranceNo NVARCHAR(50) = NULL,
    @IExpDate NVARCHAR(50) = NULL,
    @VehicleId bigint = NULL,
    @mode NVARCHAR(50)
AS
BEGIN
    IF(@mode = 'insert')
    BEGIN
        INSERT INTO sp_VehicleInfo (VehicleNo, Make, Model, Variety, isAc, EngineNo, LExpDate, InsuranceNo, IExpDate)
        OUTPUT inserted.VehicleNo
        VALUES (@VehicleNo, @Make, @Model, @Variety, @isAc, @EngineNo, @LExpDate, @InsuranceNo, @IExpDate)
    END

    IF(@mode = 'update')
    BEGIN
        UPDATE sp_VehicleInfo
        SET VehicleNo = Isnull(@VehicleNo, VehicleNo),
            Make = Isnull(@Make, Make),
            Model = Isnull(@Model, Model),
            Variety = Isnull(@Variety, Variety),
            isAc = Isnull(@isAc, isAc),
            EngineNo = Isnull(@EngineNo, EngineNo),
            LExpDate = Isnull(@LExpDate, LExpDate),
            InsuranceNo = Isnull(@InsuranceNo, InsuranceNo),
            IExpDate = Isnull(@IExpDate, IExpDate)
        WHERE 
            VehicleId = @VehicleId
    END

    IF(@mode = 'get' )
    BEGIN
        SELECT * 
        FROM sp_VehicleInfo 
        WHERE VehicleId = @VehicleId
    END

    IF(@mode = 'delete') 
    BEGIN
        DELETE FROM sp_VehicleInfo  
        WHERE VehicleId = @VehicleId
    END
END

1 个答案:

答案 0 :(得分:0)

这是一条常见的错误消息。您试图在代码中的某个位置访问sp_VehicleInfo,看起来它不是数据库中的有效对象。根据您遵循的命名约定,我认为它是一个存储过程。如果此对象可用,请检查您的数据库。

请注意以下事项:

  1. 使用" sp _"启动数据库对象的名称可能不是一个好方法。因为系统存储过程遵循该命名约定。

  2. 请包含与您的问题相关的所有代码,例如在帖子中访问数据库的代码