欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ADO.net 工具类

程序员文章站 2024-01-14 21:03:22
...
    public class sqlHelper
    {
        public static readonly object locker = new object();
        private static sqlHelper instance = null;
        private OleDbConnection conn = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=172.11.11.11;Initial Catalog=aaa;Password=aaa;Persist Security Info=True;User ID=sa");
        private sqlHelper()
        {
        }

        public static sqlHelper getInstance()
        {
            lock (locker)
            {
                if (instance == null)
                {
                    instance = new sqlHelper();
                    instance.conn.Open();
                }
                else if (instance.conn.State != ConnectionState.Open)
                {
                    instance.conn.Open();
                }
                return instance;
            }
        }

        public string QuerySingle(string sql)
        {
            string single = null;
            using (OleDbCommand command = new OleDbCommand(sql, conn))
            {
                object obj = command.ExecuteScalar();
                if (obj != null)
                {
                    single = command.ExecuteScalar().ToString();
                }
            }
            return single;
        }

        public DataTable Query(string sql)
        {
            DataTable table = null;
            using (OleDbCommand command = new OleDbCommand(sql, conn))
            {
                using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                {
                    DataSet data = new DataSet();
                    adapter.Fill(data);
                    table = data.Tables[0];
                }
            }
            return table;
        }

        public int Execute(string sql)
        {
            int result = 0;
            using (OleDbCommand command = new OleDbCommand(sql, conn))
            {
                result = command.ExecuteNonQuery();
            }
            return result;
        }

        /// <summary>
        /// 传参防sql注入
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public DataTable Query(string sql, params object[] args)
        {
            DataTable table = null;
            using (OleDbCommand command = new OleDbCommand(sql, conn))
            {

                foreach (var arg in args)
                {
                    if (arg == null)
                        command.Parameters.AddWithValue("?", DBNull.Value);
                    else
                        command.Parameters.AddWithValue("?", arg);
                }

                using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                {
                    DataSet data = new DataSet();
                    adapter.Fill(data);
                    table = data.Tables[0];
                }
            }
            return table;
        }
    }

 

相关标签: ADO.net