Retrieving data from a SQL database to VB.NET

13,389

You cannot use a OleDbConnection whith a SqlCommand(SQL-Server). Which rdbms are you using?

This works for SQL-Server:

Using con = New SqlConnection(connectionString)
    Using da = New SqlDataAdapter("SELECT ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con)
        Dim table = New DataTable()
        da.Fill(table)
        ViewDeliverys.DataGridView1.DataSource = table
    End Using
End Using

Edit: Here's the OleDb version:

Using con = New OleDbConnection(connectionString)
    Using da = New OleDbDataAdapter("SELECT ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con)
        Dim table = New DataTable()
        da.Fill(table)
        ViewDeliverys.DataGridView1.DataSource = table
    End Using
End Using
Share:
13,389
112221
Author by

112221

Updated on June 04, 2022

Comments

  • 112221
    112221 almost 2 years

    Can anyone please tell me what i am doing wrong here? Very basic level of Visual Basic experience.

    Im trying to retrieve the ORDERID, ORDERDATE and CUSTOMERID from the database provided and show them in a dataGridView?

    Dim con1 As New OleDbConnection
    con1.ConnectionString = _
    "Provider=msdaora;Data Source=orabis;User Id=112221800;Password=112221800;"
    
    'Dim con1 As New SqlConnection("Provider=msdaora;Data Source=orabis;User Id=112221800;Password=112221800;")
    con1.Open()
    Dim cmd1 As New SqlCommand("select ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con1)
    
    Dim ada1 As New SqlDataAdapter(cmd1)
    Dim ds1 As New DataSet
    ada1.Fill(ds1)
    ViewDeliverys.DataGridView1.DataSource = ds1.Tables(0)
    
    con1.Close()
    
  • 112221
    112221 about 11 years
    Using An Oracle SQL Developer? So will need the oleDbConnection?
  • 112221
    112221 about 11 years
    Dim con1 As New SqlConnection con1.ConnectionString = _ "Provider=msdaora;Data Source=orabis;User Id=112221800;Password=112221800;" con1.Open() Dim da As New SqlDataAdapter(cmd1) Dim ds1 As New DataSet Using con = New OleDbConnection(connectionString:=) Using da = New OleDbDataAdapter("SELECT ORDERID, ORDERDATE AND CUSTOMERID from CUSORDER", con) Dim table = New DataTable() da.Fill(table) ViewDeliverys.DataGridView1.DataSource = table End Using End Using
  • 112221
    112221 about 11 years
    So this is the whole thing? Sorry im at a real beginner stage. Appreciate your help.
  • Tim Schmelter
    Tim Schmelter about 11 years
    @112221: I've shown the complete code to get the data and use it as DataSource for a DataGridView. Note that it's not necessary to open/close a connection with DataAdapter.Fill.
  • 112221
    112221 about 11 years
    AHHHHH!!!!! Still getting the provider error.. Thanks for your help, I'll try figure it out some how.
  • Tim Schmelter
    Tim Schmelter about 11 years
    @112221: Then it's a problem with your connection-string. Have a look here: connectionstrings.com/…
  • 112221
    112221 about 11 years
    Fixed the connection string but now getting a different error.. Private Function OleDbConnectionStringBuilder() As String Throw New NotImplementedException End Function
  • 112221
    112221 about 11 years
    The method or operation is not implemented.
  • Tim Schmelter
    Tim Schmelter about 11 years
    @112221: Seems to be a different question. I don't see where you use a OleDbConnectionStringBuilder. You can use one to create ypou connection string, e.g.: Dim builder = New OleDbConnectionStringBuilder() ' set all properties here '. Then use builder.ToString to get the connection-string.