gul khan
gul khan

Reputation: 33

Search Another Type Text SQL Server VB.Net

I have a problem with search: how do I search our language text from SQL query and which type data I select

Dim cmd As New SqlCommand
cmd.Connection = cn
cmd.CommandText = "SELECT * FROM Table_12 WHERE m='" & "دولت خان" & "'"

Dim adapter As New SqlDataAdapter(cmd)
Dim table As New DataTable()
adapter.Fill(table)

If table.Rows.Count() > 0 Then
        TextBox2.Text = table.Rows(0)(1).ToString()
        MessageBox.Show("Record found!")
Else
        MessageBox.Show("Record not found!")

Upvotes: 0

Views: 78

Answers (2)

Mary
Mary

Reputation: 15091

Keep your database objects local so you can control that they are closed and disposed. A single Using...End Using block handles this for you.

You can pass your connection string directly to the constructor of the connection and pass the command text and connection directly to the constructor of the command.

Always use parameters to avoid Sql Injection. The value concatenated to an sql command string is potentially executable. Parameter values are not.

Private Sub OpCode()
    Dim dt As New DataTable
    Using cn As New SqlConnection("Your connection string"),
            cmd As New SqlCommand("SELECT * FROM Table_12 WHERE m= @m;", cn)
        cmd.Parameters.Add("@m", SqlDbType.NVarChar, 50).Value = "دولت خان"
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    If dt.Rows.Count() > 0 Then
        TextBox2.Text = dt.Rows(0)(1).ToString()
        MessageBox.Show("Record found!")
    Else
        MessageBox.Show("Record not found!")
    End If
End Sub

Upvotes: 1

XAMT
XAMT

Reputation: 1627

Search with like clause for unicode in column m and show column o:

cmd.CommandText = "SELECT o FROM Table_12 WHERE m like N'%" & "دولت خان" & "%'" 

Upvotes: 0

Related Questions