zoukankan      html  css  js  c++  java
  • .Net(C#)数据库访问连接方式

    一、MySql

    static string  strConn = "server=.;database=mast;uid=sa;pwd=123456;integrated Security=SSPI;persist Security info=false;";
     /// <summary>
     /// 执行SQL语句,返回影响的记录数
     /// </summary>
     /// <param name="SQLString">SQL语句</param>
     /// <returns>影响的记录数</returns>
    public static int MyExecuteSql(string SQLString)
            {
                using (MySqlConnection connection = new MySqlConnection(strConn))
                {
                    using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
                    {
                        try
                        {
                            connection.Open();
                            int rows = cmd.ExecuteNonQuery();
                            return rows;
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            connection.Close();
                            throw e;
                        }
                    }
                }
            }
            /// <summary>
            /// 执行查询语句,返回DataTable
            /// </summary>
            /// <param name="SQLString">查询语句</param>
            /// <returns>DataSet</returns>
            public static DataTable MyQuery(string SQLString)
            {
                using (MySqlConnection connection = new MySqlConnection(strConn))
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        connection.Open();
                        MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
                        command.Fill(ds, "ds");
                    }
                    catch (System.Data.SqlClient.SqlException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    return ds.Tables[0];
                }
            }
    二、SQLServer
    static string strConn = "Data Source=.;database=mast;User id=root;Password=123456;pooling=false;CharSet=utf8;port=3306;";
    
            /// <summary>
            /// 执行SQL语句,返回影响的记录数
            /// </summary>
            /// <param name="SQLString">SQL语句</param>
            /// <returns>影响的记录数</returns>
            public static int ExecuteSql(string SQLString)
            {
                using (SqlConnection connection = new SqlConnection(strConn))
                {
                    using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                    {
                        try
                        {
                            connection.Open();
                            int rows = cmd.ExecuteNonQuery();
                            return rows;
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            connection.Close();
                            throw e;
                        }
                    }
                }
            }
            /// <summary>
            /// 执行查询语句,返回DataTable
            /// </summary>
            /// <param name="SQLString">查询语句</param>
            /// <returns>DataSet</returns>
            public static DataTable Query(string SQLString)
            {
                using (SqlConnection connection = new SqlConnection(strConn))
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        connection.Open();
                        SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                        command.Fill(ds, "ds");
                    }
                    catch (System.Data.SqlClient.SqlException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    return ds.Tables[0];
                }
            }
    三、Access

    //using System.Data.OleDb;
    public static string OtherPAth = "";
    public static string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
    public static string Sql = String.Empty;
    #region 执行简单SQL语句,使用OleDb查询

    /// <summary>
    /// 执行SQL语句,返回影响的记录数
    /// </summary>
    /// <param name="SQLString">SQL语句</param>
    /// <returns>影响的记录数</returns>
    public static int ExecuteSql(string SQLString)
    {
    using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth ))
    {
    using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
    {
    try
    {
    connection.Open();
    int rows = cmd.ExecuteNonQuery();
    return rows;
    }
    catch (System.Data.SqlClient.SqlException e)
    {
    connection.Close();
    throw e;
    }
    }
    }
    }

    /// <summary>
    /// 执行查询语句,返回DataTable
    /// </summary>
    /// <param name="SQLString">查询语句</param>
    /// <returns>DataSet</returns>
    public static DataTable Query(string SQLString)
    {
    using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
    {
    DataSet ds = new DataSet();
    try
    {
    connection.Open();
    OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
    command.Fill(ds, "ds");
    }
    catch (Exception ex)
    {
    System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    return ds.Tables[0];
    }
    }

    /// <summary>
    /// 执行查询语句,返回DataTable
    /// </summary>
    /// <param name="SQLString">查询语句</param>
    /// <returns>DataSet</returns>
    public static DataTable QueryDataName()
    {
    using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
    {
    DataTable ds = new DataTable();
    try
    {
    connection.Open();
    ds = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    }
    catch (Exception ex)
    {
    System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    return ds;
    }
    }

    #endregion

    四、Sqlite

    public static string strConn = @"Data Source=" + @"F:资料文档20190629CAD625AnPinCadDLLclDataCad_try0626.db";
    public static string Sql = String.Empty;

    /// <summary>
    /// 执行SQL语句,返回影响的记录数
    /// </summary>
    /// <param name="SQLString">SQL语句</param>
    /// <returns>影响的记录数</returns>
    public static int ExecuteSql(string SQLString)
    {
    using (SQLiteConnection connection = new SQLiteConnection(strConn))
    {
    //事务
    using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
    {
    using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
    {
    try
    {
    connection.Open();
    cmd.Transaction = singleTrans;
    int rows = cmd.ExecuteNonQuery();
    singleTrans.Commit();
    return rows;
    }
    catch (System.Data.SqlClient.SqlException e)
    {
    connection.Close();
    singleTrans.Rollback();
    throw e;
    }
    }
    }
    }
    }

    /// <summary>
    /// 执行查询语句,返回DataTable
    /// </summary>
    /// <param name = "SQLString" > 查询语句 </ param >
    /// < returns > DataSet </ returns >
    public static DataTable Query(string SQLString)
    {
    using (SQLiteConnection connection = new SQLiteConnection(strConn))
    {
    DataSet ds = new DataSet();
    try
    {
    connection.Open();
    SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
    command.Fill(ds, "ds");
    return ds.Tables[0];
    }
    catch (Exception ex)
    {
    connection.Close();
    throw ex;
    }
    }
    }

    /// <summary>
    /// 执行存储过程,返回影响的记录数
    /// </summary>
    /// <param name="SQLString">SQL语句</param>
    /// <returns>影响的记录数</returns>
    public static int ExecuteProc(string procName, SQLiteParameter[] coll)
    {
    using (SQLiteConnection connection = new SQLiteConnection(strConn))
    {
    //事务
    using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
    {
    using (SQLiteCommand cmd = new SQLiteCommand(connection))
    {
    try
    {
    connection.Open();
    for (int i = 0; i < coll.Length; i++)
    {
    cmd.Parameters.Add(coll[i]);
    }
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = procName;
    cmd.Transaction = singleTrans;
    int rows = cmd.ExecuteNonQuery();
    singleTrans.Commit();
    return rows;
    }
    catch (System.Data.SqlClient.SqlException e)
    {
    connection.Close();
    singleTrans.Rollback();
    throw e;
    }
    }
    }
    }
    }

    /// <summary>
    /// 执行带参数的SQL语句,返回影响的记录数
    /// </summary>
    /// <param name="SQLString">SQL语句</param>
    /// <returns>影响的记录数</returns>
    public static int ExecuteSqlPar(string sqlPar, SQLiteParameter[] coll)
    {
    using (SQLiteConnection connection = new SQLiteConnection(strConn))
    {
    //事务
    using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
    {
    using (SQLiteCommand cmd = new SQLiteCommand(connection))
    {
    try
    {
    connection.Open();
    for (int i = 0; i < coll.Length; i++)
    {
    cmd.Parameters.Add(coll[i]);
    }
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = sqlPar;
    cmd.Transaction = singleTrans;
    int rows = cmd.ExecuteNonQuery();
    singleTrans.Commit();
    return rows;
    }
    catch (System.Data.SqlClient.SqlException e)
    {
    connection.Close();
    singleTrans.Rollback();
    throw e;
    }
    }
    }
    }
    }

  • 相关阅读:
    SAP UI5 应用 XML 视图的加载逻辑分析
    作为一名 ABAP 资深顾问,下一步可以选择哪一门 SAP 技术作为主攻方向?
    一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
    C++图像的拷贝
    C++图像裁减
    C++opencv图像的缩放
    C++typeid操作符--返回类型、变量、对象的类型名称
    C++opencv-像素操作
    C++虚析构函数
    C++openCV图像的读取、显示、保存、信息
  • 原文地址:https://www.cnblogs.com/shiyige-216/p/14642367.html
Copyright © 2011-2022 走看看