using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;
namespace page.user_class
{
/// <summary>
/// database 的摘要说明。
/// </summary>
public class database
{
public static SqlConnection conn;
public static SqlCommand sql_command;
public database()
{
//
// TODO: 在此处添加构造函数逻辑
//
string dbstr = System.Configuration.ConfigurationSettings.AppSettings["DBConnStr"];
conn = new SqlConnection(dbstr);
sql_command = new SqlCommand();
sql_command.Connection = conn;
}
/// <summary>
/// 执行SQL语句,返回是否执行成功
/// </summary>
/// <param name="sql_cmd"></param>
/// <returns></returns>
public static bool ExecuteNonQuery(string sql_cmd)
{
database mydata = new database();
sql_command.CommandText = sql_cmd;
conn.Open();
if (sql_command.ExecuteNonQuery() > 0)
return true;
else
return false;
}
/// <summary>
///作为字符串返回第一行第一列的数据
/// </summary>
/// <param name="sql_cmd"></param>
/// <returns></returns>
public static string s_ExecuteScalar(string sql_cmd)
{
database mydata = new database();
sql_command.CommandText = sql_cmd;
conn.Open();
string rtn = sql_command.ExecuteScalar().ToString();
conn.Close();
return rtn;
}
/// <summary>
/// 作为整型数据返回第一行第一列的数据
/// </summary>
/// <param name="sql_cmd"></param>
/// <returns></returns>
public static int int_ExecuteScalar(string sql_cmd)
{
database mydata = new database();
sql_command.CommandText = sql_cmd;
conn.Open();
int rtn = Convert.ToInt32(sql_command.ExecuteScalar().ToString());
conn.Close();
return rtn;
}
/// <summary>
/// 返回数据阅读器datareader
/// </summary>
/// <param name="sql_cmd"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql_cmd)
{
database mydata = new database();
sql_command.CommandText = sql_cmd;
conn.Open();
SqlDataReader myreader = sql_command.ExecuteReader();
return myreader;
}
/// <summary>
/// 生成Dataset
/// </summary>
/// <param name="sql_cmd"></param>
/// <returns></returns>
public static DataSet ExecuteDataSet(string sql_cmd)
{
database mydata = new database();
sql_command.CommandText = sql_cmd;
DataSet mydataset = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(sql_command);
myadapter.Fill(mydataset);
return mydataset;
}
/// <summary>
/// 分页显示DATAGRID的内容
/// </summary>
/// <param name="mygrid">需要绑定的DataGrid控件</param>
/// <param name="sql_cmd">设定相关的SQL语句</param>
/// <param name="pager">需要绑定的分页控件</param>
/// <param name="tbname">绑定的目标表名</param>
public static void show(DataGrid mygrid,string sql_cmd,Wuqi.Webdiyer.AspNetPager pager,string tbname)
{
database mydata = new database();
sql_command.CommandText = sql_cmd;
SqlDataAdapter myadapter = new SqlDataAdapter(sql_command);
DataSet mydataset = new DataSet();
myadapter.Fill(mydataset,pager.PageSize*(pager.CurrentPageIndex-1),pager.PageSize,tbname);
mygrid.DataSource = mydataset.Tables[tbname];
mygrid.DataBind();
}
/// <summary>
/// 生成分页效果字符串
/// </summary>
/// <param name="mygrid"></param>
/// <returns></returns>
public static StringBuilder getsb(DataGrid mygrid)
{
System.Text.StringBuilder sb=new StringBuilder("<script Language=/"Javascript/"><!--/n");
sb.Append("var el=document.all;");
sb.Append(mygrid.ClientID);
sb.Append(".scrollIntoView(true);");
sb.Append("<");
sb.Append("/");
sb.Append("script>");
return sb;
}
~database()
{
conn.Close();
conn.Dispose();
sql_command.Dispose();
}
}
}