<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();
}
}
}