向数据库添加一行而不是多行?

时间:2015-12-27 01:42:28

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

所以目前我的代码动态地将Visual Studio Repeater中的数据添加到数据库中。我的代码除了一个主要问题外,效果很好。在我的foreach循环期间,它为每个Repeater文本框创建一个新行。相反,我希望它一共创建一行。

例如,我的Repeater可以将FirstName和LastName作为列。在我的Repeater中,它可以收集这些数据,例如John表示FirstName,Smith表示LastName。它应该将John Smith输入到表的一行中,但它实际上将John添加为第1行,将Smith添加为第2行。

我该如何解决这个问题?

这是我的代码,它动态地将数据添加到数据库中:

protected void BtnSubmit_Click(object sender, EventArgs e)
{
    using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Events2"].ConnectionString))
    {
        sqlConn.Open();

        using (SqlCommand sqlCmd = new SqlCommand())
        {

            foreach (RepeaterItem rpItem in RepeaterForm.Items)
            {

                Label lblDisplayName = rpItem.FindControl("lblDisplayName") as Label;
                Label lblColumnName = rpItem.FindControl("lblColumnName") as Label;
                TextBox txtColumnValue = rpItem.FindControl("txtColumnValue") as TextBox;

                if (txtColumnValue != null)
                {
                    sqlCmd.Connection = sqlConn;
                    sqlCmd.CommandType = CommandType.StoredProcedure;
                    sqlCmd.CommandText = "spInsFormRegistrant";
                    sqlCmd.Parameters.Clear();

                    sqlCmd.Parameters.Add("@EventId", SqlDbType.Int).Value = eventId;
                    sqlCmd.Parameters.Add("@FormId", SqlDbType.Int).Value = formId;
                    sqlCmd.Parameters.Add("@ColumnName", SqlDbType.NVarChar).Value = lblColumnName.Text;
                    sqlCmd.Parameters.Add("@ColumnValue", SqlDbType.NVarChar).Value = txtColumnValue.Text;

                    sqlCmd.ExecuteNonQuery();
                }
            }
        }
    }
    PnlNone.Visible = false;
    PnlExist.Visible = false;
    PnlSuccess.Visible = true;
    PnlFail.Visible = false;
}

存储过程代码:

ALTER PROCEDURE [dbo].[spInsFormRegistrant]
    -- Add the parameters for the stored procedure here
     @EventId int,
     @FormId int,
     @ColumnName varchar(100),
     @ColumnValue varchar(100)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

 -- Insert statements for procedure here
declare @Query nvarchar(4000)
declare @ParmDefinition nvarchar(500);

set @Query = 'INSERT into Registrant(DateCreated,EventId,FormId,'+ 
             (@ColumnName) +') values (CURRENT_TIMESTAMP, @EventId, @FormId, @ColumnValue)'
set @ParmDefinition = N'@ColumnValue varchar(100), @EventID int, @FormID int'
exec sp_executesql @Query, @ParmDefinition, 
                   @ColumnValue = @ColumnValue,
                   @EventID = @EventID,
                   @FormID = @FormID

END

修改

ALTER PROCEDURE [dbo].[spInsFormRegistrant]
    -- Add the parameters for the stored procedure here
     @EventId int,
     @FormId int,
     @ColumnName varchar(100),
     @ColumnValue varchar(100)
AS

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    declare @searchID int

 -- Insert statements for procedure here

IF  @searchID IS null
 BEGIN

declare @Query nvarchar(4000)
declare @ParmDefinition nvarchar(500)

    set @Query = 'INSERT into Registrant(DateCreated,EventId,FormId,'+ 
             (@ColumnName) +') values (CURRENT_TIMESTAMP, @EventId, @FormId, @ColumnValue)
                set @searchID = SCOPE_IDENTITY()'

    set @ParmDefinition = N'@ColumnValue varchar(100), @EventID int, @FormID int, @searchID INT OUTPUT'
    exec sp_executesql @Query, @ParmDefinition, 
                   @ColumnValue = @ColumnValue,
                   @EventID = @EventID,
                   @FormID = @FormID,
                   @searchID = @searchID OUTPUT;
END
IF  @searchID IS NOT null
BEGIN
declare @Query2 nvarchar(4000)
declare @ParmDefinition2 nvarchar(500)

    set @Query2 = 'UPDATE Registrant SET DateCreated = CURRENT_TIMESTAMP, EventId = @EventId, FormId = @FormId, ' +quotename(@ColumnName)+ ' =  @ColumnValue WHERE RegistrantId = @searchID'
    set @ParmDefinition2 = N'@ColumnValue varchar(100), @EventID int, @FormID int, @searchID int'
    exec sp_executesql @Query2, @ParmDefinition2, 
                   @ColumnValue = @ColumnValue,
                   @EventID = @EventID,
                   @FormID = @FormID,
                   @searchID = @searchID
END
 END

2 个答案:

答案 0 :(得分:0)

因为你为每个字段分隔插入。你这样做的方法必须先将第一个作为插入,另一个作为更新。

请注意:

'INSERT into Registrant(DateCreated,EventId,FormId,'+ 
        (@ColumnName) +') values (CURRENT_TIMESTAMP, @EventId, @FormId, @ColumnValue)

@ColumnName是可变的,每次只插入一个字段!

可能你从某个例子中得到了那些代码,并且不适合你的程序。您需要重新编写代码以执行您尝试执行的操作,方法是在所有字段中插入所有字段,或者如果行已准备就绪,则运行UPDATE命令。

答案 1 :(得分:0)

这是因为您正在为转发器中的每个项目执行存储过程。