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

C#的SQL操作类实例

程序员文章站 2023-12-16 16:58:58
本文实例讲述了c#的sql操作类,分享给大家供大家参考。具体方法如下: 复制代码 代码如下:using system; using system.collections...

本文实例讲述了c#的sql操作类,分享给大家供大家参考。具体方法如下:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.text;
using system.data;
using system.data.sqlclient;
using system.windows.forms;
namespace yjgl
{
    /// <summary>   
    /// 数据访问基础类(sql)   
    /// </summary>
    class baseoperate
    {
        #region  建立数据库连接
        /// <summary>
        /// 建立数据库连接.
        /// </summary>
        /// <returns>返回sqlconnection对象</returns>
        public sqlconnection getcon()
        {
            string m_str_sqlcon = "data source=pc-200201010009 ;database=db_yjzl;user id=sa;pwd=123";//连接字符串
            sqlconnection mycon = new sqlconnection(m_str_sqlcon);      //创建连接对象
            return mycon;
        }
        #endregion
        #region  执行sqlcommand命令
        /// <summary>
        /// 执行sqlcommand
        /// </summary>
        /// <param name="m_str_sqlstr">sql语句</param>
        public void getcom(string m_str_sqlstr)
        {
            sqlconnection sqlcon = this.getcon();   //获取连接字符串
            sqlcon.open();       //打开连接
            try
            {
                sqlcommand sqlcom = new sqlcommand(m_str_sqlstr, sqlcon);  //通过sql语句和连接对象创建命令对象
                sqlcom.executenonquery();     //对sqlcon执行sql语句并返回受影响的行数
                sqlcom.dispose();           //释放sqlcom'
            }
            catch (sqlexception e)
            {
                messagebox.show(e.tostring(), "信息提示", messageboxbuttons.ok, messageboxicon.information);
            }
            finally
            {
                sqlcon.close();
                sqlcon.dispose();
            }
        }
        #endregion
        #region  创建dataset对象
        /// <summary>
        /// 创建一个dataset对象
        /// </summary>
        /// <param name="m_str_sqlstr">sql语句</param>
        /// <param name="m_str_table">表名</param>
        /// <returns>返回dataset对象</returns>
        public dataset getds(string m_str_sqlstr, string m_str_table)  //查询
        {
            sqlconnection sqlcon = this.getcon();
            sqldataadapter sqlda = new sqldataadapter(m_str_sqlstr, sqlcon);
            dataset myds = new dataset();
            sqlda.fill(myds, m_str_table);
            return myds;
         }
        #endregion
         #region  创建sqldatareader对象
         /// <summary>
         /// 创建一个sqldatareader对象
         /// </summary>
         /// <param name="m_str_sqlstr">sql语句</param>
         /// <returns>返回sqldatareader对象</returns>
        public sqldatareader getread(string m_str_sqlstr)
        {
            sqlconnection sqlcon = this.getcon();
            sqlcommand sqlcom = new sqlcommand(m_str_sqlstr, sqlcon);
            sqlcon.open();
            sqldatareader sqlread = sqlcom.executereader(commandbehavior.closeconnection);
            return sqlread;
           
         }
        #endregion
    }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: