小莲莲
学如初出之苗,不见其增日有所长;惰如磨刀之石,不见其损日有所亏!

<add key="SqlConnString" value="server=.;database=OA;uid=sa;pwd=123456" />

 

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
///DBUtil 的摘要说明
/// </summary>
public class DBUtil
{
  String connString = System.Configuration.ConfigurationSettings.AppSettings["SqlConnString"];
        SqlConnection conn = null;

        /// <summary>
        /// 执行sql语句查询,将结果填到DataTable中
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <param name="sql">数据库语句</param>
        public void fillDataTable(DataTable dt, String sql)
        {
            conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql,conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            adapter.Fill(dt);
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }  
        }
        /// <summary>
        /// 执行sql语句查询,将结果填到DataSet中
        /// </summary>
        /// <param name="ds">DataSet</param>
        /// <param name="sql">数据库语句</param>
        public void fillDataSet(DataSet ds, String sql)
        {
            conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            adapter.Fill(ds);
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 执行sql语句,查询count
        /// </summary>
        /// <param name="sql">数据库语句</param>
        /// <returns>count</returns>
        public int fillCount(String sql)
        {
            conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);

            conn.Open();
            int count=int.Parse(cmd.ExecuteScalar().ToString());
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
            return count;
        }
        /// <summary>
        /// 执行sql语句,增、删、改的方法
        /// </summary>
        /// <param name="sql"></param>
        public void nonQuery(String sql)
        {
            conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }
}

posted on 2010-06-01 13:35  江南荷韵  阅读(428)  评论(0编辑  收藏  举报