1

我正在创建一个网页来存储从 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。

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

我应该在存储过程中更改什么吗?

4

0 回答 0