我已经陷入了这个问题一段时间了。我已经在谷歌搜索过了,但他们都使用了javascript。如何将多个值保存到数据库中的多行?
这是我的代码:
<SCRIPT language="javascript">
function addRow(data) {
var table = document.getElementById(data);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch (newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
</SCRIPT>
<table style="width: 100%;"><tr><td><h3 style="text-decoration: underline"></h3></td></tr></table>
<table style="width: 100%;" rules="all">
<tr>
<td style="width: 100px">Stokereta</td>
<td style="width: 80px">Jenis Roda</td>
<td style="width: 100px">No Siri</td>
<td style="width: 80px">Saiz Roda (mm)</td>
<td style="width: 50px">Flange (mm)</td>
<td style="width: 50px">Hollow (mm)</td>
<td style="width: 100px">Tindakan</td>
<td style="width: 100px">Skidded</td>
<td style="width: 350px">Catatan</td>
</tr>
</table>
<table style="width: 100%;" id="data" rules="all">
<tr>
<td >
<asp:TextBox ID="stokereta" runat="server" Width="100" BorderStyle="None" ></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="ddl_jenisroda" runat="server" Width="80" >
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="nosiri" runat="server" Width="100" BorderStyle="None" ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="saizroda" runat="server" Width="80" BorderStyle="None" ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="flange" runat="server" Width="50" BorderStyle="None" ></asp:TextBox>
</td>
<td>
<asp:TextBox ID="hollow" runat="server" Width="50" BorderStyle="None" ></asp:TextBox>
</td>
<td>
<asp:DropDownList ID="ddl_tindakan" runat="server" Width="100" >
<asp:ListItem>Pilih..</asp:ListItem>
<asp:ListItem>Reprofiling</asp:ListItem>
<asp:ListItem>Rediscing</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddl_skid" runat="server" Width="100" >
<asp:ListItem>Pilih..</asp:ListItem>
<asp:ListItem>Ya</asp:ListItem>
<asp:ListItem>Tidak</asp:ListItem>
</asp:DropDownList>
</td>
</table>
<br />
<input id="Button2" type="button" value="Tambah" onclick="addRow('data')" style="border: thin ridge #000000" />
<asp:Label ID="Label3" runat="server" Text="Proses" Visible="False"></asp:Label>
<br />
<br />
<asp:Button ID="send" runat="server" Text="Hantar" BorderColor="Black" Width="60px" OnClick="Send_Click" />
<asp:Button ID="clear" runat="server" Text="Semula" BorderColor="Black" Width="60px" OnClick="Semula_Click"/><br />
代码背后的代码:
Protected Sub Save()
Dim conn As New SqlConnection(connectionString)
conn.Open()
Dim n As Integer = Convert.ToInt32(kuantiti.Text)
For i As Integer = 0 To n - 1 Step +1
If i = 0 Then
Dim s As String
s = "Insert into butiran_hantar ([depoh],[siriSA],[kuantiti],[date],[stokereta],[jenisroda], " +
"[nosiri],[saizroda],[flange],[hollow],[tindakan],[skid],[catatan],[status]) " +
"select (@depoh,@siriSA,@kuantiti,@date,@stokereta,@jenisroda, " +
"@nosiri,@saizroda,@flange,@hollow,@tindakan,@skid,@catatan,@status) "
Dim sqlcomm As New SqlCommand(s, conn)
sqlcomm.Parameters.Add("@depoh", SqlDbType.VarChar).Value = depohlist.SelectedValue
sqlcomm.Parameters.Add("@siriSA", SqlDbType.BigInt).Value = Convert.ToInt32(lblsiri.Text.Trim())
sqlcomm.Parameters.Add("@kuantiti", SqlDbType.Int).Value = Convert.ToInt32(kuantiti.Text.Trim())
sqlcomm.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Parse(tarikh.Text)
sqlcomm.Parameters.Add("@stokereta", SqlDbType.VarChar).Value = stokereta.Text
sqlcomm.Parameters.Add("@jenisroda", SqlDbType.VarChar).Value = ddl_jenisroda.SelectedItem.Text
sqlcomm.Parameters.Add("@nosiri", SqlDbType.BigInt).Value = Convert.ToInt32(nosiri.Text)
sqlcomm.Parameters.Add("@saizroda", SqlDbType.Int).Value = Convert.ToInt32(saizroda.Text)
sqlcomm.Parameters.Add("@flange", SqlDbType.Int).Value = Convert.ToInt32(flange.Text)
sqlcomm.Parameters.Add("@hollow", SqlDbType.Int).Value = Convert.ToInt32(hollow.Text)
sqlcomm.Parameters.Add("@tindakan", SqlDbType.VarChar).Value = ddl_tindakan.SelectedValue
sqlcomm.Parameters.Add("@skid", SqlDbType.NChar).Value = ddl_skid.SelectedValue
sqlcomm.Parameters.Add("@catatan", SqlDbType.VarChar).Value = catatan.Text
sqlcomm.Parameters.Add("@status", SqlDbType.VarChar).Value = Label3.Text
'sqlcomm.Parameters.AddWithValue("@ulasandm", ulasandm.Text)
Try
sqlcomm.ExecuteNonQuery()
Label2.Text = "Data disimpan."
Catch ex As Exception
Label2.Text = ex.Message.ToString()
End Try
Else
Dim sqlcomm As New SqlCommand("Insert into butiran_hantar ([depoh],[siriSA],[kuantiti],[date],[stokereta],[jenisroda], " +
"[nosiri],[saizroda],[flange],[hollow],[tindakan],[skid],[catatan],[status]) " +
"values (@depoh,@siriSA,@kuantiti,@date,@stokereta,@jenisroda, " +
"@nosiri,@saizroda,@flange,@hollow,@tindakan,@skid,@catatan,@status)", conn)
sqlcomm.Parameters.Add("@depoh", SqlDbType.VarChar).Value = depohlist.SelectedValue
sqlcomm.Parameters.Add("@siriSA", SqlDbType.BigInt).Value = Convert.ToInt32(lblsiri.Text.Trim())
sqlcomm.Parameters.Add("@kuantiti", SqlDbType.Int).Value = Convert.ToInt32(kuantiti.Text.Trim())
sqlcomm.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Parse(tarikh.Text)
sqlcomm.Parameters.Add("@stokereta", SqlDbType.VarChar).Value = stokereta.Text
sqlcomm.Parameters.Add("@jenisroda", SqlDbType.VarChar).Value = ddl_jenisroda.SelectedItem.Text
sqlcomm.Parameters.Add("@nosiri", SqlDbType.BigInt).Value = Convert.ToInt32(nosiri.Text.Trim())
sqlcomm.Parameters.Add("@saizroda", SqlDbType.Int).Value = Convert.ToInt32(saizroda.Text.Trim())
sqlcomm.Parameters.Add("@flange", SqlDbType.Int).Value = Convert.ToInt32(flange.Text.Trim())
sqlcomm.Parameters.Add("@hollow", SqlDbType.Int).Value = Convert.ToInt32(hollow.Text.Trim())
sqlcomm.Parameters.Add("@tindakan", SqlDbType.VarChar).Value = ddl_tindakan.SelectedValue
sqlcomm.Parameters.Add("@skid", SqlDbType.NChar).Value = ddl_skid.SelectedValue
sqlcomm.Parameters.Add("@catatan", SqlDbType.VarChar).Value = catatan.Text
sqlcomm.Parameters.Add("@status", SqlDbType.VarChar).Value = Label3.Text
'sqlcomm.Parameters.AddWithValue("@ulasandm", ulasandm.Text)
Try
sqlcomm.ExecuteNonQuery()
Label2.Text = "Data disimpan."
Catch ex As Exception
Label2.Text = ex.Message.ToString()
End Try
End If
Next
End Sub