2011-03-20 55 views
2

我想从列表框中记录下列代码到一个sql数据库中。但是,它给了我错误。这里是我的代码。我显示错误行下面的代码。要SQL数据库记录错误

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

1.什么错误? 2.使用参数化查询。 3.在任何情况下,连接'ListBox1.Items.Add(a)'在语义上绝对看起来不正确。这似乎返回一个'int'[根据其集合中的索引](http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.add.aspx) – 2011-03-20 23:39:15

回答

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

问候