2011-11-14 73 views
0

我有一個文本框和一個數據網格。我試圖在文本框中輸入一個姓氏,並將那個姓氏的整個記錄​​從oracle數據庫中返回到datagrid中。下面的代碼當前返回表中的所有記錄,而不是按姓氏搜索。VB.net/Oracle從文本框中選擇語句輸入到datagrid中

Public Class Form1 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    Dim myString As String 
    Dim myArg As String 
    Dim mysql As String 
    Dim myConn As New OleDb.OleDbConnection() 

    ' Create new connection 
    myString = "Provider=msdaora;Data Source=XXXX;User Id=XXXX;Password=XXXX;" 
    myConn.ConnectionString = myString 
    myArg = TextBox1.Text 
    mysql = "select * from table1 where lastname like '%'" 
    Try 
     myConn.Open() 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 

    Dim myDataadapter As New OleDb.OleDbDataAdapter(mysql, myConn) 

    Dim myDs As New DataSet 

    myDataadapter.Fill(myDs, "customer") 

    Dim prmLName As New OleDb.OleDbParameter() 
    prmLName.Direction = ParameterDirection.Input 
    prmLName.Size = "10" 
    prmLName.Value = TextBox1.Text 


    DataGridView1.DataSource = myDs 
    DataGridView1.DataMember = "customer" 
End Sub 
End Class 

回答

0

您沒有爲姓氏傳遞的參數值,所以它以它爲空和返回所有記錄:

mysql = "select * from table1 where lastname like '%" 

在這裏,你會做這樣的: -

mysql = "select * from table1 where lastname like '%" & txtLastName.Text & "%'" 

我也會建議你通過參數SqlCommand.Parameters Property來避免SQL Injection.

+0

感謝您的幫助和建議,在此! – Christopher

+0

歡迎:) – Muse

0

Y你應該寫下你的選擇查詢如下。

mysql = "select * from table1 where lastname like '%" & myArg & "'" 
+0

感謝您的回答以及skk! – Christopher

1
Private Sub Search1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search1.Click 
    Dim connectionstring As String = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=dps; User=root;Password=xxxxx;Option=3;" 
    Dim conn As New OdbcConnection(connectionstring) 
    conn.Open() 

    Dim da As New OdbcDataAdapter("select * from Student where Grade ='" & Label1.Text & "' and lastName like '%" & txtLastName.Text & "%'", conn) 
    Dim ds As New DataSet 
    da.Fill(ds) 
    DataGridView1.DataSource = ds.Tables(0) 
    DataGridView1.Refresh() 

    conn.Close() 
相關問題