PDA

View Full Version : مشکل در کد جستجو



soniarose
جمعه 10 مهر 1388, 11:57 صبح
سلام من میخوام کدی برای جستجو بنویسم که بعد از پیدا کردن رکورد مورد نظر اون ستونها رو در datagridview نشون بده من این کد رو مینویسم لطفا شما کاملش کنید



Dim strcon1 As String
' Dim com1 As SqlCommand
Dim da As SqlDataAdapter
Dim strsql1 As String
Dim con1 As SqlConnection
Dim ds1 As New DataSet
Dim dt As New DataTable

ds1.Clear()

strsql1 = "select * from table1 where "
strcon1 = "Data Source=(local);Initial Catalog=tel;Integrated Security=True"
If TextBox1.Text <> "" Then strsql1 += "id like '%" + TextBox1.Text + "' and "

If TextBox2.Text <> "" Then strsql1 += "name='" + TextBox2.Text + "' and "
If TextBox3.Text <> "" Then strsql1 += "famil='" + TextBox3.Text + "' and "


If strsql1 = "select * from table1 where " Then
strsql1 = "select * from table1"
Else
strsql1 = strsql1.Remove(strsql1.Length - 4, 4)
End If


con1 = New SqlConnection(strcon1)
con1.Open()
da = New SqlDataAdapter(strsql1, con1)
da.Fill(ds1, "table1")
con1.Close()
ds1.Dispose()
da.Dispose()
MsgBox(" ستون پیدا شد" + dt.Rows.Count.ToString)

saadi2
جمعه 10 مهر 1388, 13:58 عصر
Public Class Form1
Dim strcon1 As String
Dim strsql1 As String
Dim dt As New DataTable
Dim DA As New OleDb.OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strcon1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = test.mdb"
strsql1 = "select * from table1 where "
If TextBox1.Text <> "" Then strsql1 += "id like '%" + TextBox1.Text + "' and "
If TextBox2.Text <> "" Then strsql1 += "name='" + TextBox2.Text + "' and "
If TextBox3.Text <> "" Then strsql1 += "tel='" + TextBox3.Text + "' and "
If strsql1 = "select * from table1 where " Then
strsql1 = "select * from table1"
Else
strsql1 = strsql1.Remove(strsql1.Length - 4, 4)
End If
DA = New OleDb.OleDbDataAdapter(strsql1, strcon1)
DA.Fill(dt)
Dim dgv As New DataGridView
dgv.DataSource = dt
dgv.Dock = DockStyle.Fill
Me.Controls.Add(dgv)
DA.Dispose()
MsgBox(" ستون پیدا شد " + dt.Rows.Count.ToString)
End Sub
End Class