2012-08-17 66 views
1

我使用VB.NETSQL SERVER 2008如何通過對象循環和插入SQL Server 2008中

我有對象的數組,我需要INSERTSQL SERVER 2008這是對象enter image description here

這個對象在Inserted到三個表:

  • 第一個表將有報告信息:[ID,所以reportName,說明,樣式]插入這條信息後,重刑我會用這個的@@IDENTITY作爲FK第二表

  • 第二張表:[ID,id_first_table,評論,完整,成品,名稱,開始,結局]

問題將帶着第三張表 我怎麼可以通過清單Element環和使用第二表ID,直到元月底

喜歡它插入到第三個表:

[id , id_second_table, 101 ,820] 
[id , id_second_table'sameid', 101,831] 

這裏是我曾嘗試:

DECLARE @reportId INT , @fieldId INT 

INSERT INTO  
dbo.tblReport(strReportName,strReportDescription,strReportStyle, 
strScource,dtmCreated) 
VALUES ('caustic' , 'titration caustic', 'simple table' ,'user' , getdate()) 
SELECT @reportId = @@IDENTITY 

INSERT INTO dbo.tblReportFields 
(lngReports,strFieldName,bolComment,bolComplete,bolFinished, 
bolOutCome,bolStarted,bolUser) 
VALUES (@reportId ,'caustic titration' , 1,0,1,0,0,0) 
SELECT @fieldId = @@IDENTITY 

--LOOP AND INSERT ALL THE checklist Element 

    INSERT INTO dbo.tblReportTask (lngReportFields,lngChecklist,lngTask) 
    VALUES (@fieldId, 814 , 1443) 

做這個的也將被視爲

+0

什麼是您的VB.NET代碼插入記錄? – 2012-08-17 14:09:31

+0

我還沒有任何想法,到目前爲止,我發現這樣做的最好方法就是將此對象轉換爲數據表 – 2012-08-17 14:15:42

+0

因此,您在代碼中擁有此對象,但沒有代碼將其置入數據庫中,對嗎? – 2012-08-17 14:29:22

回答

1

好最好的辦法的任何想法,您的信息是一個有點難以用,因爲工作您的對象和SQL語句不完全匹配。但是,基本上你是這麼做的。我希望你能把它和你的問題聯繫起來。

這是插入Report和ReportFields表的方法。我沒有爲ReportFields表包含SQL,因爲它與Report Report類似。

Dim reportID As Integer = 0 
    Dim fieldID As Integer = 0 
    Using tmpCONN As New SqlConnection(tmpConnStr) 
    Dim tmpSQL As New StringBuilder 
    tmpSQL.AppendLine("INSERT INTO dbo.tblReport(strReportName,strReportDescription,strReportStyle,strScource,dtmCreated) ") 
    tmpSQL.AppendLine(" VALUES ") 
    tmpSQL.AppendLine("(@NAME, @DESC, @STYLE, @SOURCE,@CREATED); ") 
    tmpSQL.AppendLine("SELECT SCOPE_IDENTITY() AS ScopeID; ") 
    Using tmpCMD As New SqlCommand(tmpSQL.ToString, tmpCONN) 
     tmpCMD.Parameters.AddWithValue("@NAME", Obj.Name) 
     tmpCMD.Parameters.AddWithValue("@DESC", Obj.Description) 
     tmpCMD.Parameters.AddWithValue("@STYLE", Obj.Stype) 
     tmpCMD.Parameters.AddWithValue("@SOURCE", Obj.Source) 
     tmpCMD.Parameters.AddWithValue("@CREATED", Date.Now) 
     reportID = tmpCMD.ExecuteScalar 
    End Using 

    'Do same type of insert here for the ReportFields table. Instead of reportID,' 
    'it would return fieldID.' 


    'This is the loop to insert to Tasks' 
    For Each chk As chkObj In Obj.Checklist 
     tmpSQL = New StringBuilder 
     tmpSQL.AppendLine("INSERT INTO dbo.tblReportTask (lngReportFields,lngChecklist,lngTask) ") 
     tmpSQL.AppendLine(" VALUES ") 
     tmpSQL.AppendLine("(@FIELD_ID, @CHECK_ID , @TASK) ") 
     Using tmpCMD As New SqlCommand(tmpSQL.ToString, tmpCONN) 
     tmpCMD.Parameters.AddWithValue("@FIELD_ID", fieldID) 
     tmpCMD.Parameters.AddWithValue("@FIELD_ID", chk.cl_id) 
     tmpCMD.Parameters.AddWithValue("@FIELD_ID", chk.ts_id) 
     tmpCMD.ExecuteNonQuery() 
     End Using 
    Next 
    End Using 
+0

看起來很有前途......我會試試這個 – 2012-08-17 16:49:12