私はVB.NETを使用していて、SQLデータベースに接続するために使用していました。
は今、私は私のデータベースにデータを挿入したい、まだ私は私のコードを完了した後に、このエラーが出る:VB.NETコードで例外が発生する
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
それは意味ないと私は私のコードで間違って何をしますか?ここで
は私のコードです:
Imports System.Data.SqlClient
Public Class AddNew
Public con As New SqlConnection("Data Source=localhost;Initial Catalog=Tuition_Information;Integrated Security=True")
Public cmd As New Data.SqlClient.SqlCommand
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "Insert Into Student_Info Value ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox1.SelectedValue & "','" & ComboBox2.SelectedValue & "','" & TextBox7.Text & "','" & TextBox8.Text & "')"
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
MsgBox("New Student Added", MsgBoxStyle.Information, "add")
Catch ex As Exception
MessageBox.Show("Error occured, please try again.")
End Try
End Sub
私のフォームは次のようになります。あなたがつもり
パラメータを使用しないと、SQLインジェクションの問題が発生します。 SelectedValueがnullでないと仮定すると、問題もあります。 VALUEではなくVALUESです。値リストと一致するようにフィールドリストを指定する方が常に安全です。 – LarsTech
[学生登録](http://stackoverflow.com/q/332365/11683)、それは... – GSerg
@LarsTechああ作品!どうもありがとうございます !!! –