2013-01-20 65 views
0

我有一個視圖,其中包含八個Textboxes用於輸入用戶提問的選項文本以提交DataBase表。 這些值將進入名爲Question_Options的表格,它具有相同的Question_Id(FK),我將其保存在隱藏字段中,現在我必須提交DataBase中的所有TextBoxes值,我想通過循環來完成....請任何想法.. ..我只是一個初學者。如何將不同文本框中的多個記錄插入單個表中?

我的HTML代碼是

<table> 
     <tr> 
      <td>Option 1 
       <asp:HiddenField ID="HF_QidOpt" runat="server" /> 
      </td> 
      <td> 
       <asp:TextBox ID="txt_OptText" runat="server" Height="54px" TextMode="MultiLine" Width="310px"></asp:TextBox> 
      </td> 
      <td>Option 1 Description</td> 
      <td> 
       <asp:TextBox ID="txt_OptDesc" runat="server" TextMode="MultiLine" Height="54px" Width="310px"></asp:TextBox> 
      </td> 
     </tr> 
     <tr> 
      <td>Option 2</td> 
      <td> 
       <asp:TextBox ID="txt_OptText2" runat="server" Height="54px" TextMode="MultiLine" Width="310px"></asp:TextBox> 
      </td> 
      <td>Option 2 Description</td> 
      <td> 
       <asp:TextBox ID="txt_OptDesc2" runat="server" TextMode="MultiLine" Height="54px" Width="310px"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td>Option 3</td> 
      <td> 
       <asp:TextBox ID="txt_OptText3" runat="server" Height="54px" TextMode="MultiLine" Width="310px"></asp:TextBox></td> 
      <td>Option 3 Description</td> 
      <td> 
       <asp:TextBox ID="txt_OptDesc3" runat="server" TextMode="MultiLine" Height="54px" Width="310px"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td>Option 4</td> 
      <td> 
       <asp:TextBox ID="txt_OptText4" runat="server" Height="54px" TextMode="MultiLine" Width="310px"></asp:TextBox></td> 
      <td>Option 4 Description</td> 
      <td> 
       <asp:TextBox ID="txt_OptDesc4" runat="server" TextMode="MultiLine" Height="54px" Width="310px"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td> 
       <asp:Button ID="btn_Create_Option" runat="server" Text="Create Option" /></td> 
     </tr> 
     </table> 

,我已經試過這一個,但無法獲取值

Try 
      Dim DB As New SQLDBDataContext 
      Dim NGUID As New System.Guid(HF_QidOpt.Value.ToString) 
      Dim boxes As TextBox() = New TextBox() {txt_OptText, txt_OptText2, txt_OptText3, txt_OptText4, txt_OptDesc, txt_OptDesc2, txt_OptDesc3, txt_OptDesc4} 
      For Each t As TextBox In boxes 
       If t IsNot Nothing Then 
        Dim CO As New Question_Option 
        With CO 
         .UID = System.Guid.NewGuid 
         .Question_ID = NGUID 
         .Option_Text = 'How to get value of txt_OptText, txt_OptText2, txt_OptText3, txt_OptText4' 
         .Option_Description = 'How to get value of txt_OptDesc, txt_OptDesc2, txt_OptDesc3, txt_OptDesc4' 
        End With 
        DB.Question_Options.InsertOnSubmit(CO) 
       End If 
      Next 
        DB.SubmitChanges() 
        MV_Default.SetActiveView(V_Success) 

     Catch ex As Exception 
      lbl_Failure.Text = ex.Message 
      MV_Default.SetActiveView(V_Failure) 

結束Try

回答

1

你必須使用UNION承滴盤在SQL Server中插入單個語句中的SQL腳本中有多行。

INSERT INTO Table 
    (Name, Location) 
    SELECT 'Name1', 'Location1' 
    UNION ALL 
    SELECT 'Name2', 'Location2' 
    UNION ALL 
    SELECT 'Name3', 'Location3' 

裁判thisthis

+0

但我使用LINQ到SQL,可以使用LINQ你給我提供例如... – Sheery

相關問題