开发者

sqlconnection is not defined

I am trying to update some code. I have a vb file that begins with this...

Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Data.SqlTypes
Imports System.Configuration

<script runat="server">
开发者_如何转开发

...and it is failing here...

Using oConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

The error it returns is...

"Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'SqlConnection' is not defined."

Am I missing some system class?

EDIT: I updated the code to this...

Using oConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

...and it accepts it. Why do I need to explicitly write out System.Data.SqlClient every time I use an object from that class???


EDIT: I updated the code to this...

Using oConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

...and it accepts it. Why do I need to explicitly write out System.Data.SqlClient every time I use an object from that class???

My best guess is that there's another class out there called SqlConnection and .NET doesn't know which type to use until you specify the System.Data.SqlClient.SqlConnection one explicitly.


So, it turns out this line was the issue...

Using oConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("tps_write").ConnectionString())

...instead of using "tps_write," I changed the permission to "tpsWrite." Apparently the tps_write is an outdated permission no longer used here at work. I wish the error messages were more clear. ;)

Thanks for everyone's help!


I came across this thread when searching for help with a similar issue of not being able to create an SQL connection in a vb.net .NET5 Windows Desktop Application. I was also getting the error of "sqlconnection is not defined".

My solution was to add a NuGet package called "System.Data.SqlClient" by Microsoft. You can open the NuGet Package manager by going to Tools > NuGet Package Manager... > Manage NuGet Packages for Solution...

Hopefully, this will help you or others looking for a solution.


If your project is a web application, you might be missing a reference to System.Data. Right click the project in Solution Explorer, and go to Add References. On the .NET tab, select System.Data.dll and click OK.


Make sure you have specified your connection string in the web.config. You should see a tag in there like this

<connectionStrings>
<--! Below is your connection string>
<add name="ConnName" connectionString="Data Source=PROGRAMMER2\SQLServer;Initial Catalog=PPSSecurity;Integrated Security=true;" />
</connectionStrings>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜