connect VB with SQL Server !!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mse07
    New Member
    • Jan 2007
    • 36

    connect VB with SQL Server !!

    hi for every one
    i have SQL Server database and i want to connect with visual basic forms
    because i want to query about specific data from some tables
    and i want to use the forms to do that


    1- how can i connect vb with SQL Server?
    2- how can i show records for some tables?
    for exampl:
    i use Data component when i connecting with access and i can see any records from any table in database .
    what componenet can i use with SQL Server

    thank you .
  • pureenhanoi
    New Member
    • Mar 2007
    • 175

    #2
    Originally posted by mse07
    hi for every one
    i have SQL Server database and i want to connect with visual basic forms
    because i want to query about specific data from some tables
    and i want to use the forms to do that


    1- how can i connect vb with SQL Server?
    2- how can i show records for some tables?
    for exampl:
    i use Data component when i connecting with access and i can see any records from any table in database .
    what componenet can i use with SQL Server

    thank you .
    the easiest way to connect to database is using ODBC data-source.
    open the Control Panel -> Administrative Tools -> Data Source (ODBC)
    Click on the System DSN tab and click Add.
    If u already have SQL server on ur machine, scroll the list at the bottom and choose SQL Server, then click Finish
    + define the name of the connection (example : mse07)
    + let the server = (local) or the name of the SQL Server
    Click Next twice
    Choose "Change the default database to" checkbox and specify the database that u would like to connect.
    Click Next, and then Finish
    Now, goto Visual Basic and :
    Reference the project to Microsoft ActiveX Data Object 2.0 (or higher)
    use following code to connect to database
    Code:
    Dim con As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim sql As String
    con.Open "mse70"                          'the name of DSN that u defined above
    sql = "SELECT * FROM tableName"
    Set rs = con.Excute(sql)
    While Not rs.EOF
            Debug.Print rs.Fields(1)               ' print the field to debug screen or some where u need
            rs.MoveNext
    Wend

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Thanks for that pureenhanoi. Do you think you could write up a quick little article that I can post in the VB Articles section (or you can post it yourself, of course)? This sort of basic "how do I connect to a database" question seems to come up quite frequently, and it would be nice to have a reference to which we could direct people.

      Comment

      • mse07
        New Member
        • Jan 2007
        • 36

        #4
        thank you , but i use Data Grid to show a view from database

        i need to query about some fields

        how can i do that ?

        thank you .

        Comment

        • pureenhanoi
          New Member
          • Mar 2007
          • 175

          #5
          Originally posted by mse07
          thank you , but i use Data Grid to show a view from database

          i need to query about some fields

          how can i do that ?

          thank you .
          no problem. If u already known how to connect to SQL server, so continue with following section. If u did not, see above replies or simply, study from the begining. Now, how to use Data Grid to show view from database.
          Code:
          sql = "SELECT field1,field2,field3 FROM tableName"
          Set Rs = Con.Excute(sql)
          Set DataGrid1.RecordSource = Rs.RecordSource
          Data Grid have a nice view. But it is dificult to use, specially when u need edit cells' value. I'm not a good programer so i'm scared about difficult things. If u need a view only, datagrid is a good idea. But if u need edit cells' value, u should use FlexGrid, instead.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by dere
            show me how to connect visual basic with sequential query language server.
            dere, please don't add your question onto the end of an existing thread. I'm going to split it off to a new thread.

            Comment

            Working...