SQL Query In VB Express 2005

Hi I m a good VB 6 user. I used SQL Query in the codes of VB 6 in the recordsource property of the database.

Now Where and how should I write sql query in code window in VB 2005 Express since there is no recordsource property.

An example to query datagrid by using a sql query in button code will be greatly appreciated.

Example

When Button_Clicked

something........= "select * from mytable where myquery"

End Sub

Hence my datagrid refresh to show my query




Answer this question

SQL Query In VB Express 2005

  • Naldo Alcalde Huamán

    i'm a bit of a newb so i'm just explaining my recent experience writing a search query, though i didn't use sql queries - i expect the method to be similiar though, and this maybe worth a try.. write your sql query, within the buttonclick sub, to incorporate a string variable that gets its contents from the textbox contents!

    dim myQuery As String

    myQuery = textbox1.text

    sql search for myQuery

    something like that; sorry i aint clued up on sql yet



  • ImDaFrEaK

    Hi!!!

    You must learn more about ADO .NET

    You can start with a single example:

    Dim stringConnection As String = "Your connection string"

    Dim stringQuery As String = "SELECT * FROM mytable WHERE myquery"

    Dim cnn As New OleDbConnection(stringConnection)

    Dim da As New OleDbDataAdapter(stringQuery)

    Dim ds As New DataSet

    da.Fill(ds)

    DataGrid1.DataSource = ds; 'Set up the datagrid databinding

    You can use SQL in several kind of objects, properties, methods.

    Good luck!!!

    =)



  • vanWeyden

    Also you can take a look at Lesson 8 and 9 on this page http://msdn.microsoft.com/vstudio/express/vb/learning/default.aspx.

    And further reading is at http://msdn2.microsoft.com/en-us/wzabh8c4(VS.80).aspx.

    Best regards,



  • sunya2005

    Hi,

    My code is based on Lesson 9 I mentioned above. Assuming you have the project for Lesson 9, this code will work for a Form with a TextBox, Button and DataGridView.

    This is the Button's click event handler.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim connectionString As String =
    "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyCompany.mdf;Integrated Security=True;User Instance=True"
    Dim connection As New
    SqlClient.SqlConnection(connectionString)
    Dim dataAdapter As New SqlClient.SqlDataAdapter(Me
    .TextBox1.Text, connection)
    Dim dataSet As New
    DataSet()
    dataAdapter.Fill(dataSet)
    Me
    .DataGridView1.DataSource = dataSet.Tables(0)
    End Sub

    As you can see, data access has changed with ADO .NET. It has a learning curve but on the other hand it provides many benefits. The code above is the base line code you can use to get data. To get the full benefit of ADO .NET, you can take a look at http://msdn2.microsoft.com/en-us/wzabh8c4(VS.80).aspx.

    Best regards,



  • ssbeginner

    Hi Thanks for your reply but I have already tried this kind of example and What I dont get is the query in this statement is in form load that fills my datagrid in form load or something.

    I am interstest in writing a query in text box and press command button to update my gridview.

    Please help if you know something about that

    thanks



  • SQL Query In VB Express 2005