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

namespace dal
{
    public class sqlhelper
    {
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataReader sdr = null;
        private SqlDataAdapter sda = null;

        public sqlhelper()
        {
            string connstr = ConfigurationManager.ConnectionStrings

["connstr"].ConnectionString;
            conn = new SqlConnection(connstr);
        }

        private SqlConnection getconn()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }

        /// <summary>
        /// 执行不带参数的增删改sql语句或者存储过程
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>

        //public int executenonquery(string cmdtext, CommandType ct)
        //{
        //    int res;
        //    try
        //    {
        //        cmd = new SqlCommand(cmdtext, getconn());
        //        cmd.CommandType = ct;
        //        res = cmd.ExecuteNonQuery();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        if (conn.State == ConnectionState.Open)
        //        {
        //            conn.Close();
        //        }
        //    }
        //    return res;
        //}

        /// <summary>
        /// 执行不带参数的增删改语句或者存储过程
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public int executenonquery(string cmdtext, CommandType ct)
        {
            int res;
            using (cmd = new SqlCommand(cmdtext, getconn()))
            {
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }

        /// <summary>
        /// 执行带参数的增删改语句
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程</param>
        /// <param name="paras">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public int executenonquery(string cmdtext, SqlParameter[] paras,

CommandType ct)
        {
            int res;
            using (cmd = new SqlCommand(cmdtext, getconn()))
            {
                cmd.CommandType = ct;
                cmd.Parameters.AddRange(paras);
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }

        /// <summary>
        /// 该方法执行传入的查询sql或者存储过程
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public DataTable executequery(string cmdtext, CommandType ct)
        {

            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdtext, getconn());
            cmd.CommandType = ct;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }
        /// <summary>
        /// 执行带参数的SQL或者存储过程查询
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程</param>
        /// <param name="paras">命令集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public DataTable executequery(string cmdtext, SqlParameter[] paras, CommandType ct)
        {

            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdtext, getconn());
            cmd.CommandType = ct;
            cmd.Parameters.AddRange(paras);
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }
        /// <summary>
        /// 不带参数sql或者存储过程查询过程
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程名称</param>
        /// <param name="ct">命令的类型</param>
        /// <returns>返回一个dataset数据库</returns>
        public DataSet datasetquery(string cmdtext, CommandType ct)
        {
            DataSet ds = new DataSet();
            cmd = new SqlCommand(cmdtext, getconn());
            cmd.CommandType = ct;
            using (sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(ds);
            }
            return ds;
        }
        /// <summary>
        /// 带参数sql或者存储过程查询过程
        /// </summary>
        /// <param name="cmdtext">sql语句或者存储过程名称</param>
        /// <param name="paras">参数</param>
        /// <param name="ct">命令的类型</param>
        /// <returns>返回dataset数据库</returns>
        public DataSet datasetquerypara(string cmdtext, SqlParameter[] paras, CommandType ct)
        {
            DataSet ds = new DataSet();
            cmd = new SqlCommand(cmdtext, getconn());
            cmd.CommandType = ct;
            cmd.Parameters.AddRange(paras);
            using (sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(ds);
            }
            return ds;

        }

 

        /// <summary>
        /// 测试程序
        /// </summary>
        /// <param name="proc_name">存储过程名称</param>
        /// <returns></returns>
        public DataTable test(string proc_name)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(proc_name, getconn());
            cmd.CommandType = CommandType.StoredProcedure;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }

    }
}

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

namespace dal
{
    public class newsdao
    {
        private sqlhelper sqlhelperex;
        public newsdao()
        {
            sqlhelperex = new sqlhelper();
        }

        /// <summary>
        /// 取出所有记录
        /// </summary>
        /// <returns></returns>
        public DataTable selectallnews()
        {
            return sqlhelperex.executequery("proc_select_allnews", CommandType.StoredProcedure);
        }

        //        /// <summary>
        //        ///取出最新10条新闻
        //        /// </summary>
        //        /// <returns></returns>
        //        public DataTable selectnewnews()
        //        {
        //            return sqlhelperex.executequery("proc_select_newnews",

        //CommandType.StoredProcedure);
        //        }
        //        /// <summary>
        //        /// 取出最新10调热点新闻
        //        /// </summary>
        //        /// <returns></returns>
        //        public DataTable selecttennewnews()
        //        {
        //            return sqlhelperex.executequery("proc_select_ten_news",

        //CommandType.StoredProcedure);
        //        }
        //        /// <summary>
        //        /// 根据新闻类别取出新闻
        //        /// </summary>
        //        /// <param name="caid">新闻类别</param>
        //        /// <returns></returns>
        //        public DataTable selectbycaid(string caid)
        //        {
        //            DataTable dt = new DataTable();
        //            SqlParameter[] paras = new SqlParameter[] {
        //            new SqlParameter("@caid",caid)          
        //            };
        //            dt = sqlhelperex.executequery("proc_select_bycaid", paras,

        //CommandType.StoredProcedure);
        //            return dt;
        //        }
        //        /// <summary>
        //        /// 根据新闻id取出新闻
        //        /// </summary>
        //        /// <param name="id">新闻id</param>
        //        /// <returns></returns>
        //        public DataTable selectbynewsid(string id)
        //        {

        //            DataTable dt = new DataTable();
        //            SqlParameter[] paras = new SqlParameter[] {
        //            new SqlParameter("@id",id)          
        //            };
        //            dt = sqlhelperex.executequery("proc_select_bynewsid", paras,

        //CommandType.StoredProcedure);
        //            return dt;
        //        }
        //        /// <summary>
        //        /// 根据新闻标题搜索新闻
        //        /// </summary>
        //        /// <param name="title">新闻标题</param>
        //        /// <returns></returns>
        //        public DataTable selectbytitle(string title)
        //        {

        //            DataTable dt = new DataTable();
        //            SqlParameter[] paras = new SqlParameter[] {
        //            new SqlParameter("@title",title)          
        //            };
        //            dt = sqlhelperex.executequery("proc_select_bynewstitle", paras,

        //CommandType.StoredProcedure);
        //            return dt;
        //        }

        /// <summary>
        /// 根据c1字段内容搜索
        /// </summary>
        /// <param name="c1">c1字段内容</param>
        /// <returns></returns>
        public DataTable selectbychar(string c1)
        {

            DataTable dt = new DataTable();
            SqlParameter[] paras = new SqlParameter[] {
                    new SqlParameter("@c1",c1)          
                    };
            dt = sqlhelperex.executequery("proc_select_bynewstitle", paras, CommandType.StoredProcedure);
            return dt;
        }

        //        /// <summary>
        //        /// 根据新闻内容搜索新闻
        //        /// </summary>
        //        /// <param name="content">新闻内容</param>
        //        /// <returns></returns>
        //        public DataTable selectbycontent(string content)
        //        {

        //            DataTable dt = new DataTable();
        //            SqlParameter[] paras = new SqlParameter[] {
        //            new SqlParameter("@content",content)          
        //            };
        //            dt = sqlhelperex.executequery("proc_select_bynewscontent", paras,

        //CommandType.StoredProcedure);
        //            return dt;
        //        }

        //        /// <summary>
        //        /// 增加新闻
        //        /// </summary>
        //        /// <param name="title">新闻标题</param>
        //        /// <param name="content">新闻内容</param>
        //        /// <param name="caid">新闻类别id</param>
        //        /// <returns></returns>
        //        public bool insertnews(string title, string content, string caid)
        //        {
        //            bool flag = false;
        //            SqlParameter[] paras = new SqlParameter[] {
        //            new SqlParameter("@title",title),
        //                new SqlParameter("@content",content),
        //                new SqlParameter("@caid",caid)
        //            };
        //            int res = sqlhelperex.executenonquery("proc_insert_news", paras,

        //CommandType.StoredProcedure);
        //            if (res > 0)
        //            {
        //                flag = true;
        //            }
        //            return flag;
        //        }

        /// <summary>
        /// 增加记录
        /// </summary>
        /// <param name="c1">字段1</param>
        /// <param name="c2">字段2</param>
        /// <returns>逻辑值</returns>
        public bool insertchar(string c1, string c2)
        {
            bool flag = false;
            SqlParameter[] paras = new SqlParameter[] {
                    new SqlParameter("@c1",c1),
                        new SqlParameter("@c2",c2)
                    };
            int res = sqlhelperex.executenonquery("proc_insert_news", paras, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }

 

        //        /// <summary>
        //        /// 修改新闻
        //        /// </summary>
        //        /// <param name="id">新闻id</param>
        //        /// <param name="title">新闻标题</param>
        //        /// <param name="content">新闻内容</param>
        //        /// <param name="caid">新闻类别id</param>
        //        /// <returns></returns>
        //        public bool updatenews(string id, string title, string content,

        //string caid)
        //        {
        //            bool flag = false;
        //            SqlParameter[] paras = new SqlParameter[] {
        //            new SqlParameter("@id",id),
        //            new SqlParameter("@title",title),
        //            new SqlParameter("@content",content),
        //            new SqlParameter("@caid",caid)
        //            };
        //            int res = sqlhelperex.executenonquery("porc_update_news", paras,

        //CommandType.StoredProcedure);
        //            if (res > 0)
        //            {
        //                flag = true;
        //            }
        //            return flag;

        //        }

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="c1">字段1</param>
        /// <param name="c2">字段2</param>
        /// <returns></returns>
        public bool updatechar(string id, string c1, string c2)
        {
            bool flag = false;
            SqlParameter[] paras = new SqlParameter[] {
                    new SqlParameter("@id",id),
                    new SqlParameter("@c1",c1),
                    new SqlParameter("@c2",c2)                  
                    };
            int res = sqlhelperex.executenonquery("proc_update_news", paras, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;

        }

        //        /// <summary>
        //        /// 删除新闻
        //        /// </summary>
        //        /// <param name="id">新闻id</param>
        //        /// <returns></returns>
        //        public bool deletenews(string id)
        //        {
        //            bool flag = false;
        //            SqlParameter[] paras = new SqlParameter[] {            
        //                new SqlParameter("@id",id)
        //            };
        //            int res = sqlhelperex.executenonquery("proc_delete_news", paras,

        //CommandType.StoredProcedure);
        //            if (res > 0)
        //            {
        //                flag = true;
        //            }
        //            return flag;
        //        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool deletechar(string id)
        {
            bool flag = false;
            SqlParameter[] paras = new SqlParameter[] {            
                        new SqlParameter("@id",id)
                    };
            int res = sqlhelperex.executenonquery("proc_delete_news", paras, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 删除所有数据库中所有数据,请谨慎使用
        /// </summary>
        /// <returns></returns>
        public bool delecharall()
        {
            bool flag = false;
            int res = sqlhelperex.executenonquery("delete  donkey", CommandType.Text);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        ///dataset 选择所有数据
        /// </summary>
        /// <returns>返回dataset数据表</returns>
        public DataSet selectallchar()
        {
            return sqlhelperex.datasetquery("select * from donkey", CommandType.Text);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataSet selectbyid(string id)
        {
            SqlParameter[] paras = new SqlParameter[] {
            new SqlParameter("@id",id)
            };
            return sqlhelperex.datasetquerypara("select * from donkey where id=@id",paras,CommandType.Text);
        }

 

        /// <summary>
        /// 按照id选择数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回一个数据表格</returns>
        public DataTable datasetselectbyid(string id)
        {
            DataTable dt = new DataTable();
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@id",id)           
            };
            dt = sqlhelperex.executequery("proc_selectbyid", paras, CommandType.StoredProcedure);
            return dt;
        }

    }
}

sqlhelper-sql数据库的更多相关文章

  1. Android学习笔记——保存数据到SQL数据库中(Saving Data in SQL Databases)

    知识点: 1.使用SQL Helper创建数据库 2.数据的增删查改(PRDU:Put.Read.Delete.Update) 背景知识: 上篇文章学习了保存文件,今天学习的是保存数据到SQL数据库中 ...

  2. C# SQL数据库的访问类

    /// <summary> /// SQL数据库的访问类 /// </summary> public class SQLHelper { /// <summary> ...

  3. KTV项目 SQL数据库的应用 结合C#应用窗体

    五道口北大青鸟校区 KTV项目 指导老师:袁玉明 歌曲播放原理 SQL数据库关系图 C#解决方案类图 第一步:创建数据库连接方法和打开方法和关闭方法! public class DBHelper { ...

  4. jquery autocomplete实现读取sql数据库自动补全TextBox

    转自我本良人 原文 jquery autocomplete实现读取sql数据库自动补全TextBox 项目需要这样子一个功能,其他部门提的意见,只好去实现了哦,搞了好久才弄出来,分享一下. 1.前台页 ...

  5. SQL数据库

    SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言.在使用它时,只需要发出“做什么”的命令,“怎么做” ...

  6. 基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0

    作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/ 我的第二款软件:CET四六级单词背诵软件.基于QT5.5.0.sql数 ...

  7. matlab连接sql数据库

    最近项目还涉及到matlab连接数据库,下面我就记录如何进行配置使得matlab能够连接sql数据库.由于最近工程做的多一些,所以分享的都在工程配置上,当初为了这些配置可是反复卸载与重装,算法其实也有 ...

  8. SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件

    SQL SERVER 2008配置Database Mail –用SQL  数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...

  9. Eclipse连接到My sql数据库之前操作

    Eclipse连接到My sql数据库之前操作 1:首先是安装My sql数据库(为了减少你的麻烦,按照下面的连接,下载即可)百度云链接:http://pan.baidu.com/s/1mitWmbm ...

  10. Eclipse连接到My sql数据库的操作总结/配置数据库驱动

    Eclipse连接到MYSQL数据库的操作 (自己亲测,开始学习Eclipse(我的Eclipse版本是4.5.2,Jdbc驱动器的jar包版本是5.1.7,亲测可以使用)连接到数据库的时候,发现网上 ...

随机推荐

  1. 雾里看花终隔一层——探析package和import

    package是什么 package好比java用来组织文件的一种虚拟文件系统.package把源代码.java文件,.class文件和其他文件有条理的进行一个组织,以供java来使用. 源代码的要求 ...

  2. Magento white screen or how XML can break your site?

    Magento white screen or how XML can break your site? by SANDO on 02. OCT, 2012 in MAGENTO, SMALL TIP ...

  3. 在css中定义滚动条样式

    1,Overflow内容溢出时的设置 overflow 水平及垂直方向内容溢出时的设置 overflow-x 水平方向内容溢出时的设置 overflow-y 垂直方向内容溢出时的设置 以上三个属性设置 ...

  4. Java学习-009-文件名称及路径获取实例及源代码

    此文源码主要为应用 Java 获取文件名称及文件目录的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-3 00:02:27,请知悉. Java获取文件名称的 ...

  5. No mapping found for HTTP request with URI [] in DispatcherServlet with name 'appServlet'

    项目是使用SpringMVC (1)在浏览器中访问,后台总报错: No mapping found for HTTP request with URI [] in DispatcherServlet ...

  6. C语言语句分类:大致可分为六大类

    #include <stdio.h> void main() { int sign, i, n; float sum; printf("please input an integ ...

  7. Android --Fragment中异步HTTP请求

    1.自定义异步Task /** * 异步获取实时数据 */ public class TimerDataTask extends AsyncTask<Void, Void, Boolean> ...

  8. random模块使用

    import random #print random.random() #0.522622274753 #print random.randint(,) 生成1-5之间的一个随机整数 #print ...

  9. Android中颜色的设置

    方案一:新建xml文件,然后在java中用代码访问(xml文件可以直接访问) 1.在res->values文件夹下新建color.xml(这个文件中定义的代码是#RRGGBB) 2.在java类 ...

  10. 异步调用webservice

    一.异步调用 asynchronous call(异步调用):一个可以无需等待被调用函数的返回值就让操作继续进行的方法 举例: 异步调用就是你 喊 你朋友吃饭 ,你朋友说知道了 ,待会忙完去找你 ,你 ...