我有一個Visual Basic窗體,其輸入字段將生成單個零件的序列號並將其寫回到SQL數據庫。如何根據vb表單中的數量字段生成多個連續數據?SQL插入基於VB中的值的多個記錄表單
SQL DB:
create table serialnumbers (
serial int IDENTITY(10000,1),
workorder varchar(50),
partnumber varchar(50),
employeeid int,
[day] varchar(50)
)
VB:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
Public Sub ClearTextBoxes(frm As Form)
For Each Control In frm.Controls
If TypeOf Control Is TextBox Then
Control.Text = "" 'Clear all text'
End If
Next Control
End Sub
Private Sub BTNSUBMIT_Click(sender As Object, e As EventArgs) Handles BTNSUBMIT.Click
datetime.Text = Date.Now.ToString
If workorder.Text = "" Or partnumber.Text = "" Or employeeid.Text = "" Or quantity.Text = "" Or datetime.Text = "" Then
MsgBox("Please Enter All Required Fields")
Else
Try
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "Insert Into famem1 Values ('" & workorder.Text & "', '" & partnumber.Text & "', '" & employeeid.Text & "', '" & datetime.Text & "') "
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
MsgBox("Successfully Added", MsgBoxStyle.Information, "add")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Call ClearTextBoxes(Me)
End If
End Sub
End Class
在你做任何事情之前,你需要閱讀,理解並開始使用參數化查詢,然後才能訪問bobby表。 http://bobby-tables.com/您還應該將連接和命令對象包裝在USING語句中,以便您可以將連接釋放回連接池。 –
至於手頭的問題,我不完全確定你想要什麼。您是否想根據用戶輸入創建一定數量的相同行? –
是的。希望用戶可以在表單上輸入數量,並將該行數寫入SQL。 – dustinw