在数据库中插入表时出错?

时间:2012-11-06 13:28:23

标签: asp.net sql-server-2008 datatable insertion

我使用表数据类型在数据库中插入表,代码如下:

CREATE TYPE BackUpDoctorLocationAreaRoom AS TABLE (
[RoomId] bigint,
[AreaId] bigint,
[LocationId] bigint
);

Alter proc proc_tblBackUpDoctorInsert 
(
@Id uniqueidentifier='',
@BackUpDoctorId uniqueidentifier='1323e1f4-7a93-4b45-9a9b-3840c32fd6d8',  
@StartDate datetime='11/08/2012',  
@EndDate datetime='11/09/2012',  
@StartTime datetime='22:22:22',  
@EndTime datetime='01:11:11',  
@CreatedBy uniqueidentifier='acf7961c-4111-49ad-a66a-ce7f9ce131bd',  
@ModifiedBy uniqueidentifier='acf7961c-4111-49ad-a66a-ce7f9ce131bd',  
@createdDate datetime='11/6/12 3:09:58 AM',  
@ModifiedDate datetime='11/6/12 3:09:58 AM',
@tblBackUpDoctorsForRooms BackUpDoctorLocationAreaRoom READONLY
)    
as    
set xact_abort on
declare @newId uniqueidentifier;    
set @newId = newid(); 
insert into tblBackUpDoctor (Id,BackUpDoctorId,StartDate,EndDate,StartTime,EndTime,CreatedBy,ModifiedBy,
createdDate,ModifiedDate,IsActive,isdeleted) values 
(@newId,    
@BackUpDoctorId,    
@StartDate,    
@EndDate,    
@StartTime,    
@EndTime,    
@CreatedBy,    
@ModifiedBy,    
@createdDate,    
@ModifiedDate,    
1,0)    
declare @IdFortblBackUpDoctorsForRooms uniqueidentifier;    
set @IdFortblBackUpDoctorsForRooms = newid();    
delete from tblBackUpDoctorsForRooms where BackUpRecordId=@id and Roomid in (Select roomid from @tblBackUpDoctorsForRooms)
delete from tblbackupdoctor where id=@id
insert into tblBackUpDoctorsForRooms (BackUpRecordId,Roomid,Araeid,locationid)    
Select  @newId,roomid,areaid,locationid from @tblBackUpDoctorsForRooms
select @newId

这是我正在使用该表的sp。

我的班级文件的代码是:

public string InsertBackUpDoctor(ClsBackUpDoctorProp objProp, DataTable dtLocAreaRoom)
{
    String ConnectionString = CCMMUtility.GetCacheForWholeApplication();
    String backUpRecordId = "";
    SqlParameter[] param = new SqlParameter[12];
    param[0] = new SqlParameter("@Id", objProp.Id);
    param[1] = new SqlParameter("@BackUpDoctorId", objProp.BackUpDoctorId);
    param[2] = new SqlParameter("@StartDate", objProp.StartDate);
    param[3] = new SqlParameter("@EndDate", objProp.EndDate);
    param[4] = new SqlParameter("@StartTime", objProp.StartTime);
    param[5] = new SqlParameter("@EndTime", objProp.EndTime);
    param[6] = new SqlParameter("@CreatedBy", objProp.CreatedBy);
    param[7] = new SqlParameter("@ModifiedBy", objProp.ModifiedBy);
    param[8] = new SqlParameter("@createdDate", CCMMUtility.GetCurrentDateTimeByTimeZone("US Mountain Standard Time"));
    param[9] = new SqlParameter("@ModifiedDate", CCMMUtility.GetCurrentDateTimeByTimeZone("US Mountain Standard Time"));
    param[10] = new SqlParameter("@CurrentDate", objProp.CurrentDate);
    param[11] = new SqlParameter("@tblBackUpDoctorsForRooms ", dtLocAreaRoom);


    backUpRecordId = SqlHelper.ExecuteScalar(ConnectionString, "proc_tblbackupdoctorInsertBackUpDoctors", param).ToString();
    return backUpRecordId;
}

这是我尝试插入时出现的错误:

传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确。表值参数12(“@tblBackUpDoctorsForRooms”),第0行,第0列:数据类型0xF3(用户定义的表类型)具有指定的非零长度数据库名称。表值参数不允许使用数据库名称,只有模式名称和类型名称有效。 我不知道为什么会这样来帮助我..

1 个答案:

答案 0 :(得分:2)

我相信您必须更改传递自定义参数的方式:

不仅仅是

param[11] = new SqlParameter("@tblBackUpDoctorsForRooms ", dtLocAreaRoom);

,而是像

SqlParameter parameter = new SqlParameter();           

parameter.ParameterName = "@tblBackUpDoctorsForRooms";               
parameter.SqlDbType = System.Data.SqlDbType.Structured;
parameter.TypeName = "BackUpDoctorLocationAreaRoom";                
parameter.Value = dtLocAreaRoom;                                  

param[11] = parameter;