到SQL数据库记录错误

时间:2011-03-20 23:26:10

标签: visual-studio-2008

我想从listbox中将以下代码记录到sql数据库中。但是,它给了我error.Here是我的代码。我在下面的代码中显示错误行。

Public Class Form1

Dim baglanti As New SqlClient.SqlConnection("Data Source=localhost\SQLEXPRESS;Initial Catalog=dedektor;Persist 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

谢谢,

1 个答案:

答案 0 :(得分:0)

这是快速回顾。

    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")

此致