使用存储过程将数据插入SQL Server

时间:2014-09-13 20:59:55

标签: c# sql sql-server stored-procedures

将记录插入SQL Server时出现问题。

C#中的字符串不会出现在SQL Server中,因为我插入sql只是插入第一个字符

示例:如果我在数据库中插入22222只插入前两个

注意我第一次使用存储过程。

这是我的代码:

public void insert_workshop(DateTime Pdate, string PTime, string PDesc, byte[] Img)
{
        SqlCommand cmd = new SqlCommand("[InsertWorkShops]", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@WorkshopsDate", SqlDbType.Date).Value = Pdate;
        cmd.Parameters.Add("@Time", SqlDbType.NVarChar).Value = PTime;
        cmd.Parameters.Add("@WorkshopsDescription", SqlDbType.NVarChar).Value = PDesc;
        cmd.Parameters.Add("@WorkshopsImage", SqlDbType.Image).Value = Img;
        cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = 1;

        try
        {
            cmd.ExecuteNonQuery();
            Msg = "Add Done ";
        }
        catch
        {
            Msg = "Error While Adding";
        }

        WorkShopTransactions Ws = new WorkShopTransactions();
        Ws.insert_workshop(WorkShopDT.Value, txtWorshopTime.Text.ToString(),
                          txtWorkshopDescriptions.Text.ToString(), img);

T-SQL:

ALTER PROCEDURE [dbo].[InsertWorkShops] 
    @WorkshopsDate date,
    @Time nvarchar, 
    @WorkshopsDescription nvarchar,
    @WorkshopsImage image,
    @CreatedBy int
AS
BEGIN
    SET NOCOUNT ON;

    insert into Workshops 
    values(@WorkshopsDate, @Time, @WorkshopsDescription, @WorkshopsImage, @CreatedBy)
END

1 个答案:

答案 0 :(得分:4)

在存储过程中,您已声明nvarchar参数没有长度。它们默认为nvarchar(1)