Home > other >  VB.net type SqlConnection is not defined
VB.net type SqlConnection is not defined

Time:11-29

I am starting my first VB project and need to connect to SQL Server. The type SqlConnection is unknown even though the namespace System.Data.SqlClient is present.

I'm unable to move beyond this.

I have stripped the code back to essential lines that demonstrate.

I have downloaded/updated the System.Data.SqlClient from Nuget in tools.

Out of ideas and just chewing up good time

Hope you can help

Code:

Imports System.Data.SqlClient

Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Private myConn As SqlConnection       'SqlConnection shows up as a red squiggly underline
    Private myCmd As SqlCommand           'SqlCommand     ditto  
    Private myReader As SqlDataReader     'SqlDataReader  ditto   
    Private results As String

End Sub

CodePudding user response:

You need to declare private outside of the sub, or if you wanted to declare it inside a sub try to use Dim instead

Dim myConn As SqlConnection

CodePudding user response:

It should work if the Nuget is referenced from the project you're working on. First try deleting your bin/obj folders and then doing a full rebuild in case it's something wonky in the editor that's not updating (also pay attention to the build window to see if there are any clues there as to what might be causing it to fail).

As an aside, you might try the Microsoft.Data.SqlClient NuGet package as Microsoft has recommended migrating to it going forward (it mostly should be a drop in replacement and that has been my experience, you'll just change the namespace from Imports System.Data.SqlClient to Microsoft.Data.SqlClient). This is the NuGet that is going to get updated going forward. It looks like it supports > net6.0, > netstandard 2.0, > net462.

  • Related