+ -
当前位置:首页 → 问答吧 → Sql数据库改成Access数据库后,连接字符串和打开数据库、查询数据库怎么修改

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

时间:2011-12-10

来源:互联网

VB.NET code


Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms

Namespace 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 Class
End Namespace



作者: lina791211   发布时间: 2011-12-10

详细解释下。
这个代码是我用VB.net操作SQL数据库的一个操作助手代码(也有C#版本的)
但是对于Access的操作还是不懂,没有实现出来
哪位大神能帮忙解决解决。
非常感谢啊!

作者: lina791211   发布时间: 2011-12-10

这个是同样的 C#版本的。
有详细的注释。


C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace App_Code.Common.DATA
{
    class DATA
    {
        #region 全局变量
        public static string Login_Num;                     //登录ID
        public static string Login_Name;                    //登录名
        public static SqlConnection MyData_con;             //数据库连接字符串
        public static string MyData_Mydb = "Data Source=TEARDREAM-PC;Database=SoftDatabase;User=sa;PWD=as";
        public static string[] msg = new string[5] { "新增", "删除", "修改", "查询", "统计" };  //系统常用msg
        #endregion

        /// <summary>
        /// [全局]--打开数据库,传递参数无;返回参数:SqlConnection MyData_Mydb
        /// </summary>
        /// <returns></returns>
        public static SqlConnection GetConnection()
        {
            MyData_con = new SqlConnection(MyData_Mydb);    
            MyData_con.Open();
            return MyData_con;
        }

        /// <summary>
        /// [全局]--关闭数据库连接:当数据库连接打开是可以关闭;无返回值
        /// </summary>
        public static void CloseConnection()
        {
            if (MyData_con.State == ConnectionState.Open)
            {
                MyData_con.Close();
                MyData_con.Dispose();
            }
        }

        /// <summary>
        /// [全局]--从数据库中按行读取 str_SQL 指令下所匹配的数据流
        /// </summary>
        /// <param name="str_SQL"></param>
        /// <returns></returns>
        public static SqlDataReader GetCommand(string str_SQL)
        {
            GetConnection();
            SqlCommand MyData_com = MyData_con.CreateCommand();
            MyData_com.CommandText = str_SQL;
            SqlDataReader MyData_read = MyData_com.ExecuteReader();
            return MyData_read;
        }

        /// <summary>
        /// [全局]--系统数据库单指令操作正确性判断:参数[查询语句,操作类型,是否提示]
        /// </summary>
        /// <param name="str_SQL"></param>
        /// <param name="OperCode"></param>
        /// <param name="showmsg"></param>
        public static void GetSQLCommand(string str_SQL, int OperCode, bool showmsg)
        {
            GetConnection();
            SqlCommand MyData_SQLcom = new SqlCommand(str_SQL, MyData_con);
            try
            {
                if (MyData_SQLcom.ExecuteNonQuery() > 0)
                {
                    if (showmsg)
                        MessageBox.Show(msg[OperCode] + "成功!", "提示信息");
                }
                else
                {
                    if (showmsg)
                        MessageBox.Show(msg[OperCode] + "失败!", "提示信息");
                }
                MyData_SQLcom.Dispose();
                CloseConnection();
                return;
            }
            catch (Exception e)
            {
                MessageBox.Show(msg[OperCode] + "失败!\n出错原因:" + e.Message, "提示信息");
            }
            MyData_SQLcom.Dispose();
            CloseConnection();
        }

        /// <summary>
        /// [全局][重载+1]--系统数据库指令集操作正确性判断:参数[批量查询语句,操作类型,是否提示]
        /// </summary>
        /// <param name="str_SQL"></param>
        /// <param name="OperCode"></param>
        /// <param name="showmsg"></param>
        public static void GetSQLCommand(string[] str_SQL, int OperCode, bool showmsg)
        {
            GetConnection();
            SqlCommand MyData_SQLcom = new SqlCommand();
            SqlTransaction MyData_tran;
            MyData_tran = MyData_con.BeginTransaction();
            MyData_SQLcom.Connection = MyData_con;
            MyData_SQLcom.Transaction = MyData_tran;
            try
            {
                for (int i = 0; i < str_SQL.Length; i++)
                {
                    if (str_SQL[i] == "") continue;
                    MyData_SQLcom.CommandText = str_SQL[i];
                    MyData_SQLcom.ExecuteNonQuery();
                }
                MyData_tran.Commit();
                if (showmsg)
                    MessageBox.Show(msg[OperCode] + "成功!", "提示信息");
            }
            catch (Exception e)
            {
                MyData_tran.Rollback();
                if (showmsg)
                    MessageBox.Show(msg[OperCode] + "失败!\n出错原因:" + e.Message, "提示信息");
            }
            MyData_SQLcom.Dispose();
            CloseConnection();
        }

        /// <summary>
        ///[全局]--DataGridView填充
        /// </summary>
        /// <param name="gv"></param>
        /// <param name="str_SQL"></param>
        public static void DataGVBind(DataGridView gv, string str_SQL)
        {
            MyData_con = GetConnection();
            SqlDataAdapter sda = new SqlDataAdapter(str_SQL, MyData_con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            gv.DataSource = ds.Tables[0];
        }

    }
}

作者: lina791211   发布时间: 2011-12-10

dim connectionString as string = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=e:\\d.mdb "
将有Sql 改为OleDB 如SqlDataAdapter 改为OleDBDataAdapter

作者: DENQH   发布时间: 2011-12-11

相关阅读 更多