存储过程不在SQL Server中使用游标数据插入

时间:2013-09-21 11:48:52

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

我正在创建一个网页来存储从Microsoft Excel工作表中读取的数据。

我将数据传递给SQL Server 2008中的存储过程。

这是我的C#代码:

SqlConnection conn = new SqlConnection(AppDB);
conn.Open();

SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "sp_ins_TaskDetails_from_Excel";

command.Parameters.AddWithValue("@TaskDetails", dtTaskDetailsFromExcel);

string sReturnValue = command.ExecuteNonQuery().ToString();

存储过程有一个用户定义的表数据类型作为参数。

这是我的存储过程:

ALTER PROCEDURE dbo.sp_ins_TaskDetails_from_Excel
(
@TaskDetails TypeInsertTaskFromExcel11 readonly
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE 
    @ProjectID int,
    @ProjectTeamID int,
    @TeamLeaderUserID int,
    @TaskCategoryName varchar(max),
    @TaskDescription varchar(max),
    @TeamMemberUserID int,
    @TaskPriorityName varchar(10),
    @PlanDuration float,
    @PlanStartDate datetime,
    @PlanEndDate datetime,
    @TaskTypeName varchar(30),
    @TaskStatusName varchar(30),
    @TaskAllotName varchar(10),
    @CreatedBy varchar(30),
    @CreatedDate datetime,
    @ISMailSend bit,
    @Isvisible bit,
    @UniqueID int

DECLARE TMSTaskDetailFromExcelCursor CURSOR FOR SELECT

    ProjectID,
    ProjectTeamID,
    TeamLeaderUserID,
    TaskCategoryName,
    TaskDescription,
    TeamMemberUserID,
    TaskPriorityName,
    PlanDuration,
    PlanStartDate,
    PlanEndDate,
    TaskTypeName,
    TaskStatusName,
    TaskAllotName,
    CreatedBy,
    CreatedDate,
    ISMailSend,
    Isvisible,
    UniqueID

FROM @TaskDetails
OPEN TMSTaskDetailFromExcelCursor
FETCH NEXT FROM TMSTaskDetailFromExcelCursor INTO

    @ProjectID,
    @ProjectTeamID,
    @TeamLeaderUserID,
    @TaskCategoryName,
    @TaskDescription,
    @TeamMemberUserID,
    @TaskPriorityName,
    @PlanDuration,
    @PlanStartDate,
    @PlanEndDate,
    @TaskTypeName,
    @TaskStatusName,
    @TaskAllotName,
    @CreatedBy,
    @CreatedDate,
    @ISMailSend,
    @Isvisible,
    @UniqueID;
WHILE @@FETCH_STATUS=0 BEGIN

-- Insert statements for procedure here
INSERT INTO ManageTasks (ProjectID, ProjectTeamID, TeamLeaderUserID, 
    TaskCategoryName,TaskDescription, TeamMemberUserID, TaskPriorityName, 
    PlanDuration, PlanStartDate, PlanEndDate, TaskTypeName, 
    TaskStatusName, TaskAllotName, CreatedBy, CreatedDate, ISMailSend, Isvisible,UniqueID)
VALUES (@ProjectID, @ProjectTeamID, @TeamLeaderUserID,  @TaskCategoryName,
    @TaskDescription, @TeamMemberUserID, @TaskPriorityName, @PlanDuration,
    @PlanStartDate, @PlanEndDate, @TaskTypeName, @TaskStatusName,
    @TaskAllotName, @CreatedBy, @CreatedDate, @ISMailSend,@Isvisible,@UniqueID);

FETCH NEXT FROM TMSTaskDetailFromExcelCursor INTO
        @ProjectID,
    @ProjectTeamID,
    @TeamLeaderUserID,

    @TaskCategoryName,
    @TaskDescription,
    @TeamMemberUserID,

    @TaskPriorityName,
    @PlanDuration,
    @PlanStartDate,

    @PlanEndDate,
    @TaskTypeName,
    @TaskStatusName,

    @TaskAllotName,
    @CreatedBy,
    @CreatedDate,

    @ISMailSend,
    @Isvisible,
    @UniqueID;
END;

CLOSE TMSTaskDetailFromExcelCursor;
DEALLOCATE TMSTaskDetailFromExcelCursor;
END

它接收数据并在同一存储过程中使用游标,在同一过程中使用Insert语句插入每一行。

执行时,Excel中的所有数据都会传递给存储过程,如用户定义的表类型中所述。

但是,值不存储在数据库中,存储过程返回-1。

所有值的格式和顺序都正确。我不知道出了什么问题。

我是否应该在存储过程中更改某些内容?

0 个答案:

没有答案