VB.NET 中的“ConnectionString 属性尚未初始化”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15585299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
“The ConnectionString property has not been initialized” error in VB.NET
提问by user2201924
Every time I tried to connect to the database it give me this error "The ConnectionString property has not been initialized"
每次我尝试连接到数据库时,它都会给我这个 error "The ConnectionString property has not been initialized"
How can i avoid this error and make it work?
How can i avoid this error and make it work?
Here are my codes:
这是我的代码:
Imports System.Data.OleDb
Public Class frmLoginPage
Dim con As New OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("Select*from tblPassword", con)
da.Fill(dt)
Dim newRow As DataRow = dt.NewRow
With newRow
If .Item("Username") = txtUsername.Text And .Item("Password") = txtPassword.Text Then frmMainMenu.ShowDialog() Else MsgBox("The username or password you entered was incorrect, please try again!", MsgBoxStyle.Critical, "Information")
End With
End Sub
回答by DeanOC
You have instantiated an OleDbConnectionobject, but you haven't set the connectionstringproperty so it doesn't know where it should connect to. You also haven't opened it. Your code should look something like:
您已经实例化了一个OleDbConnection对象,但是您还没有设置connectionstring属性,因此它不知道它应该连接到哪里。你也没有打开。您的代码应该类似于:
Dim myConnection As OleDbConnection = New OleDbConnection()
myConnection.ConnectionString = myConnectionString
myConnection.Open()
' execute queries, etc
myConnection.Close()
回答by SMHasnain
This Problem Occurs when you donot consider making a new connection The solution is very simple, all you have to do is to make
当您不考虑建立新连接时会出现此问题 解决方案很简单,您要做的就是建立
Dim conString as string = "Your connection string here"
Dim con As new OleDbConnection
con.connectionSting= ConSting
con.open()
'now simply write the query you want to be executed
'At the end write
con.close()
this will solve your problem. if it doesn't solve your problem post reply I will try to solve it.
这将解决您的问题。如果它不能解决您的问题后回复我会尝试解决它。
回答by Panda1122
You never assigned your connection string to the connection object, just like the error is saying.
您从未将连接字符串分配给连接对象,就像错误所说的那样。
Insert a line setting the connection string before con.open.
在 con.open 之前插入一行设置连接字符串。
Con.connectionstring = connection Con.Open() Or better yet, change your using statement as follows
Con.connectionstring = connection Con.Open() 或者更好的是,更改您的 using 语句如下
Dim Connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\VB Project\Library Catalog System\Library Catalog System\library.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim Connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\VB Project\Library Catalog System\Library Catalog System\library.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Using Con As New SqlConnection(connection)
使用 Con 作为新的 SqlConnection(connection)

