“ConnectionString属性尚未初始化” VB.NET中的错误 [英] "The ConnectionString property has not been initialized" error in VB.NET
本文介绍了“ConnectionString属性尚未初始化” VB.NET中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每次我试图连接到数据库,它给我这个错误ConnectionString属性尚未初始化
我能做什么来解决这个问题? / p>
这里是我的代码
模块Module1
函数GetInfoForStudent (ByRef QueryName As String,ByVal UserName As String,ByVal Password As String)as DataTable
使用Con As新的SqlConnection
尝试
使用OleCon作为新的SqlConnection
Dim Connection As String = Data Source = .\SQLEXPRESS; AttachDbFilename = G:\VB Project \Library
目录System \Library Catalog System \library.mdf;集成
Security = True; Connect Timeout = 30 ; User Instance = True
Con.Open()
Dim Cmd As SqlCommand = Con.CreateCommand()
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandText = QueryName
Cmd.Parameters.AddWithValue(@ user,UserName)
Cmd.Parameters.AddWithValue(@ pass,密码)
Dim da As新的SqlDataAdapter(Cmd)
Dim ds as New DataTable()
da.Fill(ds)
返回ds
结束使用
Catch ex As Exception
抛出新的异常消息)
结束尝试
结束使用
结束函数
结束模块
Sub ShowStudentInfo()
Dim dt As DataTable = GetInfoForStudent(MyStoredProcName,@user,@pass)
'由于(大概)只返回一个
使用dt.Rows(0)
'分配您的文本框
StudentIDTextBox.Text = .Item(StudentID)
LoginIDTextBox.Text = .Item(LoginID)
Student_NameTextBox.Text = .Item
Student_addressTextBox.Text = .Item(Student address)
结束于
结束Sub
解决方案
您从未将连接字符串分配给连接对象,就像错误所说。
在con.open之前插入一行设置连接字符串。
Con.connectionstring = connection
Con.Open()
使用语句如下
Dim Connection As String =Data Source = .\SQLEXPRESS; AttachDbFilename = G:\VB Project \ Library Catalog System \Library Catalog System \library.mdf;集成
Security = True; Connect Timeout = 30;用户实例= True
使用Con As新的SqlConnection )
Every time I tried to connect to the database it give me this error "The ConnectionString property has not been initialized"
what can I do to solve this?
here are my codes
Module Module1
Function GetInfoForStudent(ByRef QueryName As String, ByVal UserName As String, ByVal Password As String) As DataTable
Using Con As New SqlConnection
Try
Using OleCon As New SqlConnection
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"
Con.Open()
Dim Cmd As SqlCommand = Con.CreateCommand()
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandText = QueryName
Cmd.Parameters.AddWithValue("@user", UserName)
Cmd.Parameters.AddWithValue("@pass", Password)
Dim da As New SqlDataAdapter(Cmd)
Dim ds As New DataTable()
da.Fill(ds)
Return ds
End Using
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Using
End Function
End Module
Sub ShowStudentInfo()
Dim dt As DataTable = GetInfoForStudent("MyStoredProcName", "@user", "@pass")
' Since (presumably) only one is returned
With dt.Rows(0)
' Assign your text boxes
StudentIDTextBox.Text = .Item("StudentID")
LoginIDTextBox.Text = .Item("LoginID")
Student_NameTextBox.Text = .Item("Student Name")
Student_addressTextBox.Text = .Item("Student address")
End With
End Sub
解决方案
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.connectionstring = connection
Con.Open()
Or better yet, change your using statement as follows
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)
这篇关于“ConnectionString属性尚未初始化” VB.NET中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文