Results 1 to 9 of 9

Thread: Accessing Data

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342

    Accessing Data

    When I used VB6 I would explicitly access SQL servers in that I did not use the data environment, rather I would use code like:

    Code:
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    With cn
        .Provider = "SQLOLEDB"
        .ConnectionString = "Data Source=SQLServer;Initial Catalog=MyCat"
        .Open
    End With
    
    rs.Open "SELECT * FROM Table1"
    
    Text1.Text = rs!Field
    I am just wondering if this is the best way to do this in ADO.NET.

    Thanks!!

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You should use the classes found in the System.Data.ADO namespace.

  3. #3
    New Member
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    14
    could you post some code as an example?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The code are the same as in VB6 it's just that you have to set a reference to the System.Data and you should also include the namespace System.Data.ADO

    Best regards

  5. #5
    New Member
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    14
    hmmm I don't mean to sound rude but I don't think thats the case.

    I've tried using vb6 ado in .Net and it does'nt work. (which worked perfectly in vb6)

    out of all the things changed in .Net, Database Access is one of the biggest.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Of course it's a big change in how .Net handles ADO internally.
    But the method names and properties are practically the same.
    If you have a look in the .Net framework about the System.Data.ADO namespace you'll see that the changes you have to make is minor.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I'm sorry!
    Now that I've had a closer look I see that I was looking at some code I made in beta1.
    You could use the ADODB interoperability assembly.
    Simply set a reference to that library and try the code. It works perfectly for me.
    VB Code:
    1. Imports ADODB
    2. Public Class Form1
    3.     Inherits System.Windows.Forms.Form
    4.     Private cn As New Connection()
    5.     Private rs As New Recordset()
    6.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    7.  
    8.     'auto generated code removed in this example
    9.  
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.         With cn
    12.             .Provider = "SQLOLEDB"
    13.             .ConnectionString = "Data Source=SomeSQLServer;Initial Catalog=Anything"
    14.             .Open()
    15.         End With
    16.         rs.Open("Select * from tblFirst", cn)
    17.         TextBox1.Text = rs("MyField").Value
    18.     End Sub
    19. End Class
    Best regards

  8. #8
    New Member
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    14
    ok thanks

    I have another question about ado.net.

    how do you use variables in sql query's ?

    for example i used this code in a username/password protected program i did in vb6

    Code:
    strUser = txtUser.Text
    strPass = txtPass.Text
    
    sqlUser = "SELECT * FROM tblUserPass WHERE tblUserPass.Username = '" & strUser & "'" & " AND tblUserPass.Password = '" & strPass & "'"
    
    Set rsUserPass = dbUserPass.Execute(sqlUser)
    
    If rsUserPass.BOF = True And rsUserPass.EOF = True Then
    Denied()
    Else
    Granted()
    End If
    how would I do that in .Net ?

    Thankyou.

  9. #9
    New Member
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    14
    don't worry i got it =)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width