Problems connecting to Database using ODBC and VB Project

0

EDIT 3 I have used the provided links to change the connection, which works :) now I have this message for every record pulled from my database Error Screen EDIT 2: No reason for ODBC, just it was the first result, code is below I am running VS Studio 2017 Pro with SQL Server ODBC Driver 17 x64. On running the application I run into this problem, is there a step I'm missing

OS: Windows 10 Pro x64 (Build 2004)

System.InvalidOperationException
  HResult=0x80131509
  Message=The 'Odbc Driver 17 For SQL Serverserver=tcp:<servername>.database.windows.net, 1433' provider is not registered on the local machine.
  Source=System.Data
  StackTrace:
   at System.Data.OleDb.OleDbServicesWrapper.GetDataSource(OleDbConnectionString constr, DataSourceWrapper& datasrcWrapper)
   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OleDb.OleDbConnection.Open()
   at MMYOB.CoA.CoA_Load(Object sender, EventArgs e) in C:\MMYOB\MMYOB\CoA.vb:line 11
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    'Create a connection object
    Dim ConnectionString As String = "provider=Odbc Driver 17 For SQL Server" + "server=tcp:<servername>.database.windows.net, 1433;Database=mmyob;Uid=sqldba;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30"
    Dim SQL As String = "SELECT * FROM COA"
    Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString)

    ' open the connection 
    conn.Open()

    ' Create an OleDbDataAdapter object
    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
    adapter.SelectCommand = New OleDbCommand(SQL, conn)

    ' Create Data Set object
    Dim ds As DataSet = New DataSet("COA")
    ' Call DataAdapter's Fill method to fill data from the
    ' DataAdapter to the DataSet 
    adapter.Fill(ds)

    ' Bind dataset to a DataGrid control
    dataGrid1.DataSource = ds.DefaultViewManager
vb.net
odbc
azure-sql-database
asked on Stack Overflow Nov 1, 2020 by matthewandichabod • edited Nov 1, 2020 by matthewandichabod

1 Answer

0

Since you didn't specify what kind of application this is I am assuming WinForms. The data access part of the code is the same it is only the binding to the grid that may vary.

The pattern for Azure Sql Server connection string would look something like this.

Server=tcp:myserver.database.windows.net,1433;Database=myDataBase;User ID=mylogin@myserver;Password=myPassword;Trusted_Connection=False;Encrypt=True;

See https://www.connectionstrings.com/azure-sql-database/ Your connection string looks a bit different.

Connections and commands need to be disposed. Using...End Using blocks take care of this for us. You can pass the CommandText and the Connection directly to the constructor of the Command.

Private Sub FillDataGridView()
    Dim ConnectionString As String = "server=tcp:<servername>.database.windows.net, 1433;Database=mmyob;Uid=sqldba;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30"
    Dim dt As New DataTable
    Using conn As New SqlConnection(ConnectionString),
            cmd As New SqlCommand("SELECT * FROM COA", conn)
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    DataGridView1.DataSource = dt
End Sub
answered on Stack Overflow Nov 2, 2020 by Mary

User contributions licensed under CC BY-SA 3.0