I want to record from listbox with the following code into a sql database.But,it's gives me error.Here is my code.I show error line the following code.
Public Class Form1
Dim baglanti As New SqlClient.SqlConnection("Data Source=localhost\SQLEXPRESS;Initial Catalog=dedektor;Per开发者_运维百科sist Security Info=True;User ID=test;Password=test1")
Dim adaptor As SqlClient.SqlDataAdapter
Dim kayit As New DataSet
Dim datakayit As DataRow
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
For a = TextBox1.Text To TextBox2.Text
ListBox1.Items.Add(a)
Next
Error line --> adaptor = New SqlClient.SqlDataAdapter("INSERT INTO Table_2([po])VALUES('" + ListBox1.Items.Add(a) + "') ", baglanti)
adaptor.Fill(kayit, "table_2")
End Sub
Thanks,
Here is the quick Recap.
Dim sqlquery As String = ""INSERT INTO Table_2([po])VALUES(@po)" //sql string
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sqlquery, con) //Insert
Dim a As Integer
For a = TextBox1.Text To TextBox2.Text
cmd.Parameters.AddwithValue("@po",ListBox1.Items.Add(a)) //use parameters to avoid sql injection
cmd.ExecuteNonQuery() //execute insert Command
Next
//You must call another query to populate your DataSet
adaptor.Fill(kayit, "table_2")
Regards
精彩评论