从存储过程中捕获错误

时间:2018-11-02 20:23:36

标签: c# sql-server

我正在尝试使用SQL Server存储过程将新用户添加到表中,但是没有添加它,它也没有显示任何错误。

C#代码:

public void ADD_USER(string firstname, string lastname, string username, string password, string birthdate, string shiftstart, string shiftend, string ssn, string address, int salary, byte[] id_image, byte[] avatar)
{
    DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
    DAL.Open();

    SqlParameter[] param = new SqlParameter[12];
    param[0] = new SqlParameter("@USERNAME", SqlDbType.VarChar,100);
    param[0].Value = username;
    param[1] = new SqlParameter("@PASSWORD", SqlDbType.VarChar, 100);
    param[1].Value = password;
    param[2] = new SqlParameter("@FIRSTNAME", SqlDbType.VarChar, 100);
    param[2].Value = firstname;
    param[3] = new SqlParameter("@LASTNAME", SqlDbType.VarChar, 100);
    param[3].Value = lastname ;
    param[4] = new SqlParameter("@BIRTHDATE", SqlDbType.Date);
    param[4].Value = birthdate;
    param[5] = new SqlParameter("@ADDRESS", SqlDbType.VarChar, 100);
    param[5].Value = address;
    param[6] = new SqlParameter("@SSN", SqlDbType.Int);
    param[6].Value = ssn;
    param[7] = new SqlParameter("@SHIFTSTART", SqlDbType.Time);
    param[7].Value = shiftstart;
    param[8] = new SqlParameter("@SHIFTEND", SqlDbType.Time);
    param[8].Value = shiftend;
    param[9] = new SqlParameter("@SALARY", SqlDbType.Int);
    param[9].Value = salary;
    param[10] = new SqlParameter("@ID_IMAGE", SqlDbType.Image);
    param[10].Value = id_image;
    param[11] = new SqlParameter("@AVATAR", SqlDbType.Image);
    param[11].Value = avatar;

    try 
    {
        DAL.executeCommand("ADD_USER", param);
    }
    catch (SqlException ex)
    {
        Console.Write(ex.Message);
    }

    DAL.close();
}

存储过程代码:

CREATE PROC ADD_USER
    @USERNAME VARCHAR(100),
    @PASSWORD VARCHAR(100),
    @FIRSTNAME VARCHAR(100),
    @LASTNAME VARCHAR(100),
    @BIRTHDATE VARCHAR(100),
    @ADDRESS VARCHAR(100),
    @SSN INT,
    @ID_IMAGE IMAGE,
    @AVATAR IMAGE,
    @SHIFTSTART VARCHAR(100),
    @SHIFTEND VARCHAR(100),
    @SALARY INT
AS
    INSERT INTO [CAFE].[dbo].[users] ([username], [password],
                                      [firstname], [lastname], [SSN],
                                      [id_image], [avatar],
                                      [start_working], [shift_start], [shift_end],
                                      [salary], [birth_date], [address])
    VALUES (@USERNAME, @PASSWORD,
            @FIRSTNAME, @LASTNAME, @SSN,
            @ID_IMAGE, @AVATAR,
            CONVERT(DATE, GETDATE()), @SHIFTSTART, @SHIFTEND,
            @SALARY, @BIRTHDATE, @ADDRESS)

存储过程可以在SQL Server本身上正常工作,但不会从C#代码添加任何记录

我试图跟踪此问题,但没有得到结果。

我正在使用SQL Server 2008 R2和Visual Studio 2015

0 个答案:

没有答案