存储过程不更新数据库asp.net c#

时间:2016-10-06 12:56:55

标签: c# asp.net sql-server stored-procedures

以下是数据库中的存储过程。

CREATE PROCEDURE Taskedb_Insert
(

    @nNewID int output,
    @sFromEmail varchar(200)  = NULL,
    @sToEmail varchar(200)  = NULL,
    @sEmailSubject varchar(400)  = NULL,
    @sEmailBody varchar(MAX)  = NULL,
    @dEmailDate datetime  = NULL
)   
AS
 BEGIN

INSERT INTO [Taskedb]
(

    FromEmail, 
    ToEmail, 
    EmailSubject, 
    EmailBody, 
    EmailDate
) VALUES (

    @sFromEmail,
    @sToEmail,
    @sEmailSubject,
    @sEmailBody,
    @dEmailDate
)
SELECT @nNewID = @@IDENTITY
END

这是ASP.NET C#中的代码

string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=tbd_email_tasks;Integrated Security=True";

SqlConnection connection = new SqlConnection(connectionString);

connection.Open();

SqlCommand cmd = new SqlCommand("Taskedb_Insert", connection);
cmd.CommandType = System.Data.CommandType.StoredProcedure;

SqlParameter p1 = cmd.Parameters.Add("@nNewID", System.Data.SqlDbType.Int);
p1.Direction = System.Data.ParameterDirection.Output;

SqlParameter p2 = cmd.Parameters.Add("@sEmailSubject", System.Data.SqlDbType.VarChar);
p2.Value = subject;

SqlParameter p3 = cmd.Parameters.Add("@sEmailBody", System.Data.SqlDbType.VarChar);
p3.Value = body;

SqlParameter p4 = cmd.Parameters.Add("@sToEmail", System.Data.SqlDbType.VarChar);
p4.Value = to;

SqlParameter p5 = cmd.Parameters.Add("@sFromEmail", System.Data.SqlDbType.VarChar);
p5.Value = from;

SqlParameter p6 = cmd.Parameters.Add("@dEmailDate", System.Data.SqlDbType.DateTime);
p6.Value = date;

//connection.Open();
cmd.ExecuteNonQuery();

它没有更新数据库。请帮帮我。

2 个答案:

答案 0 :(得分:1)

感谢大家的帮助

我收到此错误是因为我的表Taskedb有

为nvarchar(50)

但是我注入了67个字符强度,导致了这个错误。将其更改为

为nvarchar(MAX)

问题已修复

答案 1 :(得分:0)

您可以检查它是否可能在某个交易范围内。当然,我将使用block包装你的数据库连接和命令。