2017-08-03 61 views
-1

我正在创建通用的Web应用程序,它将从DB生成接口字段,我可以从这些字段获取并传递值到数据库,因为字段是动态的我将这些值传递给DB,由逗号,如下所示分离的串,如何将动态数据添加到SQL Server 2008中的多个表

'MemberID:M001,FirstName:Kasun,LastName:Harshana,CompanyName:cmp' 

它包含字段id与激磁

例如,用户插入值:MEMBERID = M001

但我的问题是如何从动态字段的值,并将这些到多个表 如:成员Id和名字应插入成员表和名字,姓氏和公司名称应插入person表

这些表格依赖输入字符串的过程中,该字符串可以有一个以上的表,这取决于我的看法('MemberID:M001,FirstName:Kasun,LastName:Harshana,CompanyName:cmp') : 2 table

更多细节的领域,我在这里附上这个样本表结构,this is the table where I get fields this the table where contains field belong table detail

我需要创建一个存储在SQL服务器程序添加到这些特定的表 任何方法来实现,这将是热烈欢迎,

感谢

+1

你期望什么样的答案?当你卡住时,你应该写你自己的代码并提出问题。我建议不要将这种类型的csv文件发送到存储过程,而是在您的C#代码中进行处理。 – Peter

+0

您在编写Web API或存储过程时遇到困难吗?通过web API具有这样的通用接口是安全问题。 –

回答

0

当事件的数据录入,您应该调用数据库上的两个存储过程运行: 创建PROC usp_MemberTable @pMemberID, @pLastName 作为 插入件插入membertable 值 (@ pMemberID,@ pLastName)

创建PROC usp_PesronTable - 作为列的参数 as - 带有参数的sql语句。

对于人员表,我会将成员ID添加为外键。 可以从c#中表单的数据输入对象传递参数。

0
ALTER proc [dbo].[usp_CreateMemberByString](@Input 
nvarchar(max),@KeyUser bigint,@KeySession int = 0 , @KeyClient bigint 
=0,@retVal INT OUTPUT,@retMsg varchar(500) output) 
AS 
    BEGIN 
    DECLARE @KeyMember bigint,@keyHousehold bigint,@MemberID 
    varchar(30),@FirstName varchar(50),@LastName varchar(50),@MiddleInitial 
    varchar(1),@NamePrefix varchar(20),@NameSuffix varchar(20) 
     ,@Title int,@CompanyName varchar(50),@Status bit,@EnrollDate 
    datetime,@IsMainMember bit,@HomePhone varchar(20),@MobilePhone 
    varchar(20),@WorkPhone varchar(20),@DateofBirth datetime 
     ,@Gender varchar(10),@EmailAddress varchar(100),@Notes 
    varchar(2000),@Source varchar(20) 
     ,@Address1 varchar(45),@Address2 varchar(45),@City 
    varchar(30),@StateProvince varchar(50),@CountryCode 
    varchar(10),@StreetNumber varchar(10),@PostalCode varchar(10) 
     ,@SendEmail bit,@SendMail bit,@SendText bit 
     ,@EnteredBy varchar(20) 
     ,@imgPath varchar(500) = '' 

DECLARE @StartIndex INT, @EndIndex INT,@Character char = ',' 
SET @StartIndex = 1 

IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character 
BEGIN 
     SET @Input = @Input + @Character 
END 

WHILE CHARINDEX(@Character, @Input) > 0 
BEGIN 
     SET @EndIndex = CHARINDEX(@Character, @Input) 

     --Start sperate item name and vakue 
     DECLARE @SubStartIndex INT, @SubEndIndex INT,@SubCharacter char = ':',@SubInput NVARCHAR(250) 
     SET @SubStartIndex = 1 
     SET @SubInput = SUBSTRING(@Input, @StartIndex, @EndIndex - 1) 

     SET @SubEndIndex = CHARINDEX(@SubCharacter, @SubInput) 

     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'MemberID') 
     Begin 
     set @MemberID = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'FirstName') 
     Begin 
     set @FirstName = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'LastName') 
     Begin 
     set @LastName = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'MiddleInitial') 
     Begin 
     set @MiddleInitial = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'NamePrefix') 
     Begin 
     set @NamePrefix = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'NameSuffix') 
     Begin 
     set @NameSuffix = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Title') 
     Begin 
     set @Title = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'CompanyName') 
     Begin 
     set @CompanyName = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Status') 
     Begin 
     set @Status = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'EnrollDate') 
     Begin 
     set @EnrollDate = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'IsMainMember') 
     Begin 
     set @IsMainMember = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'HomePhone') 
     Begin 
     set @HomePhone = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'MobilePhone') 
     Begin 
     set @MobilePhone = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'WorkPhone') 
     Begin 
     set @WorkPhone = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'DateofBirth') 
     Begin 
     set @DateofBirth = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Gender') 
     Begin 
     set @Gender = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'EmailAddress') 
     Begin 
     set @EmailAddress = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Notes') 
     Begin 
     set @Notes = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Source') 
     Begin 
     set @Source = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Address1') 
     Begin 
     set @Address1 = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'Address2') 
     Begin 
     set @Address2 = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'City') 
     Begin 
     set @City = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'StateProvince') 
     Begin 
     set @StateProvince = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'CountryCode') 
     Begin 
     set @CountryCode = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'StreetNumber') 
     Begin 
     set @StreetNumber = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'PostalCode') 
     Begin 
     set @PostalCode = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'SendEmail') 
     Begin 
     set @SendEmail = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'SendMail') 
     Begin 
     set @SendMail = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'SendText') 
     Begin 
     set @SendText = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
     if(SUBSTRING(@SubInput, @SubStartIndex, @SubEndIndex - 1) = 'KeyMember') 
     Begin 
     set @KeyMember = SUBSTRING(@SubInput, @SubEndIndex + 1, LEN(@SubInput)); 
     end 
    --End sperate item name and vakue 

    SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input)) 
END 
if(@KeyMember != 0) 
begin 
    exec usp_UpdateMember 0,@KeyMember,0,@MemberID,@FirstName,@LastName,@MiddleInitial,@NamePrefix,@NameSuffix,@Title,@CompanyName,@Status 
     ,@EnrollDate,@IsMainMember,@HomePhone,@MobilePhone,@WorkPhone,@DateofBirth,@Gender,@EmailAddress,@Notes,@Source,@EnteredBy 
     ,@Address1,@Address2,@City,@StateProvince,@CountryCode,@StreetNumber,@PostalCode,@SendEmail,@SendMail,@SendText,@EnteredBy,''          
     ,@retVal output,@retMsg output; 
end 

else 
begin 
    exec usp_CreateMember @KeyUser,@keyHousehold,@MemberID,@FirstName,@LastName,@MiddleInitial,@NamePrefix,@NameSuffix,@Title,@CompanyName,@Status,@EnrollDate,@IsMainMember 
     ,@HomePhone,@MobilePhone,@WorkPhone,@DateofBirth,@Gender,@EmailAddress,@Notes,@Source,@Address1,@Address2,@City,@StateProvince,@CountryCode,@StreetNumber,@PostalCode 
     ,@SendEmail,@SendMail,@SendText,@EnteredBy,@imgPath,@KeySession,@KeyClient,@retVal output,@retMsg output; 
end 

END

这是我创造的,但这里只添加到静态表我要动态地做到这一点的程序,

+0

EXEC usp_CreateMemberByString“MEMBERID:M001,姓:Kasun,名字:Harshana,公司名称:CMP,EnrollDate:1970/01/01 MobilePhone:0112236987,出生日期:1970/01/01性别:男,EmailAddress的:电子邮件@测试。 COM,地址1:Horana,KEYMEMBER:104367' ,1,1,1,1, '味精' –

相关问题