操作Sql数据库帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DateBase
{
    public class Helper
    {
        /// <summary>
        /// 定义SqlConnection
        /// </summary>
        public static SqlConnection conn = null;

        /// <summary>
        /// 数据库连接 public static string str = ConfigurationManager.AppSettings["MySqlConncetion"];
        /// </summary>
        public static string str = "Data Source=.;Initial Catalog=MyOffice;User ID=www_jy;password=*****************";

        /// <summary>
        /// 事物回滚
        /// </summary>
        public static SqlTransaction trans = null;

        /// <summary>
        /// 打开数据库连接
        /// </summary>
        public static SqlConnection Connection
        {
            get
            {
                try
                {
                    if (conn == null)
                    {
                        conn = new SqlConnection(str);
                        conn.Open();
                    }
                    else if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    else if (conn.State == ConnectionState.Broken)
                    {
                        conn.Close();
                        conn.Open();
                    }
                }
                catch (Exception e)
                {
                    //抛出错误
                }

                return conn;
            }
        }

        /// <summary>
        /// 打开事物
        /// </summary>
        public static void OpenTrans()
        {
            try
            {
                trans = Connection.BeginTransaction();
            }
            catch (Exception e)
            {
                //抛出异常
            }
        }

        /// <summary>
        /// 事物提交
        /// </summary>
        public static void CommitTrans()
        {
            trans.Commit();
        }

        /// <summary>
        /// 事物回滚
        /// </summary>
        public static void RollBackTrans()
        {
            trans.Rollback();
        }

        /// <summary>
        /// 执行增、删、改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int GetExecute(string sql)
        {
            int num = 0;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Transaction = trans;
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                //抛出异常
                RollBackTrans();
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
            return num;
        }

        /// <summary>
        /// 有参数执行增、删、改操作
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pare"></param>
        /// <returns></returns>
        public static int GetExecute(string sql, SqlParameter[] pare)
        {
            int num = 0;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(pare);
                cmd.Transaction = trans;
                num = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                //抛出异常
                RollBackTrans();
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
            return num;
        }

        /// <summary>
        /// 查询条数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int GetExecuteScalar(string sql)
        {
            int num = 0;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                num = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception e)
            {
                //抛出异常
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
            return num;
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pare"></param>
        /// <returns></returns>
        public static int GetExecuteScalar(string sql, SqlParameter[] pare)
        {
            int num = 0;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(pare);
                num = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception e)
            {
                //抛出异常
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
            return num;
        }

        /// <summary>
        /// 查找数据库
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataSet GetDataSet(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sql, Connection);
                adapter.Fill(ds);
            }
            catch (Exception e)
            {
                //抛出异常
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
            return ds;
        }

        /// <summary>
        /// 有参查找数据库
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pare"></param>
        /// <returns></returns>
        public static DataSet GetDataSet(string sql, SqlParameter[] pare)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(pare);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
            }
            catch (Exception e)
            {
                //抛出异常
            }
            finally
            {
                //关闭数据库连接
                conn.Close();
            }
            return ds;
        }
    }
}
时间: 2024-10-01 02:28:38

操作Sql数据库帮助类的相关文章

C# SQL数据库助手类2.0(自用)

1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Text; 7 8 namespace YcTools 9 { 10 /// <summary>C# SQL数据库助手类2.0</summary> 11 public class YSql

C#学习之旅---C#操作SQL数据库

C#操作SQL数据库 Connection(连接)对象 连接字符串: 形式1.”server=;uid=;pwd=;database=” 形式2.”server=;Intergrated Security=true/SSPI” 创建连接对象: SqlConnection con=new SqlConnection(连接字符串) 打开数据库连接:con.Open() 关闭数据库连接:con.Close() 查看连接状态:con.state =ConnectionState.Open/Closed

小胖学PHP总结5-----PHP操作SQL数据库(Deprecated: mysql_connect(): The mysql extension is deprecated and will)

第一:打开数据库 要操作SQL数据库,首先必须于MySQL服务器建立连接,连接MySQL服务器的语句: mysql_connect('hostname','username','password') 该函数的返回值用于表示这个数据库连接成功,如果连接成功,则会返回一个资源,为以后执行SQL指令做准备. <?php $link = mysql_connect("localhost:3306","root","123456") or die(&

141019●用C#操作SQL数据库

数据库操作:ADO.NET 一.命名空间: using System.Data; using System.Data.SqlClient;     //针对SqlServer进行优化了的数据访问类的空间 System.Data.OleDB;System.Data.ODBC;    //命名空间通用数据访问类的空间. ODBC——Open DataBase Connection开放式数据互联 二.步骤: 1.建立链接对象——建一个通向数据库的通道. 2.打开通道 3.操作数据库 4.关闭通道 三.

通过泛型数据,操作SQL数据库

本人在操作数据库的时候,采用将对象的形式操作传入数据库,经过大量百度和朋友帮助,总结出如下两个接口.用于插入数据,提取数据. 要这样操作的前提条件,就是类名和表名一致,类字段和表字段一致就可以了 /// <summary> /// 插入一个数据 /// </summary> /// <param name="pole">需要插入的数据</param> /// <returns></returns> public i

C#连接操作MySQL数据库 帮助类

最近准备写一个仓库管理的项目 客户要求使用C#编写MySQL存储数据 为了方便,整理了数据库操作的工具类 using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; /// <summary> /// @author: 房上的猫

C#操作SQLite数据库帮助类——SQLiteHelper

using System; using System.Collections.Generic; using System.Data.SQLite; using System.Data; namespace Com.ZCWL.Rock.Helper { public class SQLiteHelper { private static string connectionString = string.Empty; /// <summary> /// 根据数据源.密码.版本号设置连接字符串. /

sql数据库通用类

1 /** 2 * 3 */ 4 package com.hotel.util; 5 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.sql.ResultSetMetaData; 11 import java.util.ArrayList; 12 import

java操作ORACLE数据库工具类(JDBC)

package com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * * 2015-3-5下午2:45:56 * *MusicWeb.util.DBUtil *连接数据库 类 */ public class DBUtil