vb.net连接sql数据库
这是我的代码:ImportsSystem.DataImportsSystem.Data.OleDbPublicClassForm1PrivateSubButton1_Cl...
这是我的代码:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objconnection As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;″&″DataSource=localhost;Initial Catalog=mysql;″&″UserID=root;Password=root;")
objconnection.Open()
End Sub
End Class
可是调试的时候会出现异常:
在 System.Data.OleDb.OleDbException 中第一次偶然出现的“System.Data.dll”类型的异常
详细信息是:{"无效的授权说明连接字符串属性无效"}
请问如何解决啊?困在这个问题上好久了。
我没有分了。。抱歉 展开
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objconnection As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;″&″DataSource=localhost;Initial Catalog=mysql;″&″UserID=root;Password=root;")
objconnection.Open()
End Sub
End Class
可是调试的时候会出现异常:
在 System.Data.OleDb.OleDbException 中第一次偶然出现的“System.Data.dll”类型的异常
详细信息是:{"无效的授权说明连接字符串属性无效"}
请问如何解决啊?困在这个问题上好久了。
我没有分了。。抱歉 展开
1个回答
展开全部
您好:给你个Sql Servers 2000的链接方法参考一下。
Public Shared connectionString As String = “Data Source=SERVERS;Initial Catalog=HL;User ID=sa;Password=pw" providerName="System.Data.SqlClient”
'<summary>
'执行SQL语句,返回影响的记录数
'</summary>
'<param name="SQLString">SQL语句</param>
'<returns>影响的记录数</returns>
Public Shared Function ExecuteSql(ByVal SQLString As String) As Integer
Using connection As New SqlConnection(connectionString)
Using cmd As New SqlCommand(SQLString, connection)
Try
connection.Open()
Dim rows As Integer = cmd.ExecuteNonQuery()
Return rows
Catch e As System.Data.SqlClient.SqlException
connection.Close()
Throw e
End Try
End Using
End Using
End Function
希望对您有用!
Public Shared connectionString As String = “Data Source=SERVERS;Initial Catalog=HL;User ID=sa;Password=pw" providerName="System.Data.SqlClient”
'<summary>
'执行SQL语句,返回影响的记录数
'</summary>
'<param name="SQLString">SQL语句</param>
'<returns>影响的记录数</returns>
Public Shared Function ExecuteSql(ByVal SQLString As String) As Integer
Using connection As New SqlConnection(connectionString)
Using cmd As New SqlCommand(SQLString, connection)
Try
connection.Open()
Dim rows As Integer = cmd.ExecuteNonQuery()
Return rows
Catch e As System.Data.SqlClient.SqlException
connection.Close()
Throw e
End Try
End Using
End Using
End Function
希望对您有用!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询