首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

Sql数据库改成Access数据库后,连接字符串和打开数据库、查询数据库如何修改

2012-04-03 
Sql数据库改成Access数据库后,连接字符串和打开数据库、查询数据库怎么修改VB.NET codeImports System.Coll

Sql数据库改成Access数据库后,连接字符串和打开数据库、查询数据库怎么修改

VB.NET code
Imports System.Collections.GenericImports System.TextImports System.DataImports System.Data.SqlClientImports System.Windows.FormsNamespace SoftSystem.Common    Class DATA#Region "全局变量"        Public Shared Login_Num As String        Public Shared Login_Name As String        Public Shared MyData_con As SqlConnection        Public Shared MyData_Mydb As String = "Data Source=MyDateBase;Database=softSystem;User=sa;PWD=as"        Public Shared msg As String() = New String(4) {"添加", "删除", "修改", "借阅", "归还"}#End Region        Public Shared Function GetConnection() As SqlConnection            MyData_con = New SqlConnection(MyData_Mydb)            MyData_con.Open()            Return MyData_con        End Function        Public Shared Sub CloseConnection()            If MyData_con.State = ConnectionState.Open Then                MyData_con.Close()                MyData_con.Dispose()            End If        End Sub        Public Shared Function GetCommand(ByVal str_SQL As String) As SqlDataReader            GetConnection()            Dim MyData_com As SqlCommand = MyData_con.CreateCommand()            MyData_com.CommandText = str_SQL            Dim MyData_read As SqlDataReader = MyData_com.ExecuteReader()            Return MyData_read        End Function        Public Shared Sub GetSQLCommand(ByVal str_SQL As String, ByVal OperCode As Integer, ByVal showmsg As Boolean)            GetConnection()            Dim MyData_SQLcom As New SqlCommand(str_SQL, MyData_con)            Try                If MyData_SQLcom.ExecuteNonQuery() > 0 Then                    If showmsg Then                        MessageBox.Show(msg(OperCode) & "成功!", "提示信息")                    End If                Else                    If showmsg Then                        MessageBox.Show(msg(OperCode) & "失败!", "提示信息")                    End If                End If                MyData_SQLcom.Dispose()                CloseConnection()                Return            Catch e As Exception                MessageBox.Show(msg(OperCode) & "失败!" & vbLf & "出错原因:" & e.Message, "提示信息")            End Try            MyData_SQLcom.Dispose()            CloseConnection()        End Sub        Public Shared Sub GetSQLCommand(ByVal str_SQL As String(), ByVal OperCode As Integer, ByVal showmsg As Boolean)            GetConnection()            Dim MyData_SQLcom As New SqlCommand()            Dim MyData_tran As SqlTransaction            MyData_tran = MyData_con.BeginTransaction()            MyData_SQLcom.Connection = MyData_con            MyData_SQLcom.Transaction = MyData_tran            Try                For i As Integer = 0 To str_SQL.Length - 1                    If str_SQL(i) = "" Then                        Continue For                    End If                    MyData_SQLcom.CommandText = str_SQL(i)                    MyData_SQLcom.ExecuteNonQuery()                Next                MyData_tran.Commit()                If showmsg Then                    MessageBox.Show(msg(OperCode) & "成功!", "提示信息")                End If            Catch e As Exception                MyData_tran.Rollback()                If showmsg Then                    MessageBox.Show(msg(OperCode) & "失败!" & vbLf & "出错原因:" & e.Message, "提示信息")                End If            End Try            MyData_SQLcom.Dispose()            CloseConnection()        End Sub        Public Shared Sub DataGVBind(ByVal gv As DataGridView, ByVal str_SQL As String)            MyData_con = GetConnection()            Dim sda As New SqlDataAdapter(str_SQL, MyData_con)            Dim ds As New DataSet()            sda.Fill(ds)            gv.DataSource = ds.Tables(0)        End Sub    End ClassEnd Namespace 



[解决办法]
dim connectionString as string = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=e:\\d.mdb "
将有Sql 改为OleDB 如SqlDataAdapter 改为OleDBDataAdapter
[解决办法]
只有连接字符串和对象换换。看看例子。
例如
http://developer.51cto.com/art/200910/159375.htm
http://tech.sina.com.cn/s/2008-04-18/14412147676.shtml
http://www.cnblogs.com/jhtchina/archive/2007/12/19/898956.html

热点排行