自動インクリメントされた一意の ID を使用して、vb.net フォーム フィールドから SQL テーブルにデータをコミットすることに成功しました。私が抱えている問題は、同じフィールド名を持つ複数のフォーム フィールドがあることです。たとえば、データベース内のフィールドで、フォームの TextBox1、TextBox2、TextBox3、または TextBox4 からのデータを受け入れるようにします。これは可能ですか?
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using message As New MailMessage()
Dim iReturn As Boolean
Using con As New SqlConnection("Server=SGI-NOT-TTID01;Database=Commission;uid=****;pwd=******")
Using com As New SqlCommand()
With com
.CommandText = "INSERT INTO InputTable VALUES (@UniqueRequestNumber, @BusinessUnit, @ReasonCode, @Originator, @OnBehalfofName, @OnBehalfofEmail, @SalesTeam_CS, @SalesTeam_MISDBM, @DateEffectiveFrom, @DateEffectiveTo, @CustomerClientName, @CustomerAccNum, @MovingTo, @JustificationComments, @NewBusOrRenewal, @OthersImpacted, @ReasonCode2, @Target, @TargetPeriod, @ProposedScheme, @NewOTC, @ExistingRole, @Amount, @CustomerImapcted, @CurrentOTC, @PoundsInPocketEst, @NewRole ) "
.Connection = con
.CommandType = CommandType.StoredProcedure
.CommandText = "AddUniqueRequestNumberWithOutput"
.Parameters.AddWithValue("@UniqueRequestNumber", ParameterDirection.Output)
.Parameters.AddWithValue("@BusinessUnit", ComboBox1.Text)
.Parameters.AddWithValue("@ReasonCode", ComboBox4.Text)
.Parameters.AddWithValue("@Originator", TextBox1.Text)
.Parameters.AddWithValue("@OnBehalfofName", TextBox2.Text)
.Parameters.AddWithValue("@OnBehalfofEmail", TextBox3.Text)
.Parameters.AddWithValue("@SalesTeam_CS", ComboBox2.Text)
.Parameters.AddWithValue("@SalesTeam_MISDBM", ComboBox3.Text)
.Parameters.AddWithValue("@DateEffectiveFrom", DateTimePicker1.Text)
.Parameters.AddWithValue("@DateEffectiveTo", DateTimePicker2.Text)
.Parameters.AddWithValue("@CustomerClientName", TextBox4.Text)
.Parameters.AddWithValue("@CustomerAccNum", TextBox5.Text)
.Parameters.AddWithValue("@MovingTo", TextBox6.Text)
.Parameters.AddWithValue("@JustificationComments", TextBox7.Text)
.Parameters.AddWithValue("@NewBusOrRenewal", ComboBox6.Text)
.Parameters.AddWithValue("@OthersImpacted", TextBox13.Text)
.Parameters.AddWithValue("@ReasonCode2", ComboBox5.Text)
.Parameters.AddWithValue("@Target", TextBox5.Text)
.Parameters.AddWithValue("@TargetPeriod", ComboBox7.Text)
.Parameters.AddWithValue("@ProposedScheme", TextBox24.Text)
.Parameters.AddWithValue("@NewOTC", TextBox17.Text)
.Parameters.AddWithValue("@ExistingRole", TextBox19.Text)
.Parameters.AddWithValue("@Amount", TextBox8.Text)
.Parameters.AddWithValue("@CustomerImpacted", TextBox12.Text)
.Parameters.AddWithValue("@CurrentOTC", TextBox16.Text)
.Parameters.AddWithValue("@PoundsInPocketEst", TextBox10.Text)
.Parameters.AddWithValue("@NewRole", TextBox20.Text)
End With
Try
con.Open()
com.ExecuteNonQuery()
iReturn = True
Catch ex As SqlException
MsgBox(ex.Message.ToString)
iReturn = False
Finally
con.Close()
End Try
'set to the from, to and subject fields
message.From = (New MailAddress(TextBox3.Text.ToString()))
message.[To].Add(New MailAddress("benjamin.boothe@experian.com"))
message.Subject = "Request Number"
If Not attachmentList Is Nothing Then
For Each attachment As System.Net.Mail.Attachment In attachmentList
message.Attachments.Add(attachment)
Next
End If
'display submitted box
MessageBox.Show("Your request has been submitted!", "Congratulations!")
'close form
Me.Close()
Dim client As New SmtpClient()
client.Host = "mailhost"
client.Send(message)
End Using
End Using
End Using
Return
End Sub
したがって、この関数の背後にあるロジックは次のとおりです。
- SQL Server のテーブルに値をコミットします。
- 電子メールを送信します (オプションで添付ファイル付き)。