using System;
namespace Web
{
/// msg; 出错信息
/// sqlcommand; 从数据库中取值时的查询字符串
/// messagebox(System.Web.UI.Page page,string msg) ,弹出对话框,应用时page参数用this,msg为弹出信息
/// getcolumndata(con,sqlcommand,table_name, column_name) 进行查询后返回一条记录时取得一个数据库中相应字段的值, 其中con 为已带有连接字符串的参数
///connconnection() 连接数据库
/// getusertype() 判断用户是否通过验证,如果没有就跳转到login.aspx
/// </summary>
/// 公共函数
public class myclass
{ public string msg; //出错信息
public string sqlcommand; //查询字符串 //以下5项为getcolumndata()的参数
public string table_name; //数据库中的表名
public string column_name; //数据表中的列名
public System.Data.SqlClient.SqlConnection con; //数据库的连接
public myclass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//连接数据库的方法
public bool openconnection(System.Data.SqlClient.SqlConnection con )
{
bool i=true ; //测试数据库是否连接上
try
{ con.Open(); }
catch
{
msg="数据库不能连接,请联系管理员";
con.Dispose();
i=false;
}
return i;
}
public void messagebox(System.Web.UI.Page page,string msg) //应用时page参数用this
{
string script="<script> alert('"+ msg +"')</script>";
page.Response.Write(script);
}
/// <summary>
/// //绑定数据,其中参数为connection, datagrid、查询字符串及数据库中的表名
/// </summary>
public void mydatabind(System.Data.SqlClient.SqlConnection con, System.Web.UI.WebControls.DataGrid dg, string sqlstring,string mytable)
{ //绑定数据,其中参数为connection, datagrid、查询字符串及数据库中的表名
if(openconnection(con))
{
System.Data.SqlClient.SqlDataAdapter myadp=new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlCommand mycmd=new System.Data.SqlClient.SqlCommand();
System.Data.DataSet myds =new System.Data.DataSet();
mycmd.CommandTimeout =30;
mycmd.Connection =con;
mycmd.CommandText =sqlstring; //设置参数
myadp.SelectCommand =mycmd;
myadp.Fill(myds,mytable); //填充数据集
dg.DataSource =myds.Tables[mytable].DefaultView; //设置数据源
dg.DataBind(); //数据绑定
myadp.Dispose();
myds.Dispose();
mycmd.Dispose();
}
}
/// <summary>
/// 取得一个数据库中相应字段的值,无参数时调用成员属性为参数
/// </summary>
public string getcolumndata()
{
return getcolumndata( con, sqlcommand, table_name,column_name) ;
}
/// <summary>
/// 取得一个数据库中相应字段的值
/// </summary>
public string getcolumndata(System.Data.SqlClient.SqlConnection con,string sqlcommand,string table_name,string column_name) //取得一个数据库中相应字段的值
{ string colunm_data=""; //返回值 //错误时将返回空值
if(!openconnection(con))
{
msg="数据库不能连接,请联系管理员" ;
return colunm_data ;
}
else
{
System.Data.SqlClient.SqlDataAdapter adp=new System.Data.SqlClient.SqlDataAdapter() ; //创建sqldataadapter对象
System.Data.SqlClient.SqlCommand com =new System.Data.SqlClient.SqlCommand();
com.CommandTimeout =30;
com.Connection =con;
com.CommandText =sqlcommand;
adp.SelectCommand =com;
System.Data.DataSet ds =new System.Data.DataSet() ; //创建一个数据集
try
{ adp.Fill(ds,table_name);}
catch
{
msg="数据库查询错误,请重新设置查询条件";
adp.Dispose();
ds.Dispose();
com.Dispose();
return colunm_data ;
}
if(ds.Tables[table_name].Rows.Count ==0)
{
msg="在数据库中没有相应的记录";
return colunm_data ;
}
colunm_data=ds.Tables[table_name].Rows[0][column_name].ToString() ;//取出数据字段的值
if(colunm_data=="")
{
msg="在数据库中没有相应的记录";
}
else
{
msg="查询成功";
}
con.Close();
return colunm_data ;
}
}
/// <summary>
/// 使用用户名与密码的登陆过程,其中page这个参数给弹出框使用
/// </summary>
public bool login(System.Web.UI.Page mypage, System.Data.SqlClient.SqlConnection myconnection, string myuser, string mypassword)
{
System.Data.SqlClient.SqlCommand com =new System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataReader dr;
com.Connection=myconnection;
com.CommandText="SELECT * FROM admin WHERE users = '" +myuser +"'";
if (myconnection.State.ToString() =="Closed" ) //如果数据库没有连接,就连接
{
try
{
myconnection.Open();
}
catch
{
messagebox(mypage," 数据库不能连接,请联系管理员");
return false;
}
}
dr=com.ExecuteReader();
if (!dr.Read() )
{
messagebox(mypage,"没有这个用户名");
return false;
}
if (mypassword==dr.GetValue(2).ToString() )
{
mypage.Session["usertype"]=dr.GetValue(3).ToString() ;
mypage.Session["user"]=dr.GetValue(2).ToString();
return true;
}
else
{
messagebox(mypage,"密码不正确,请重新输入");
return false;
}
}
}
}
namespace Web
{
/// msg; 出错信息
/// sqlcommand; 从数据库中取值时的查询字符串
/// messagebox(System.Web.UI.Page page,string msg) ,弹出对话框,应用时page参数用this,msg为弹出信息
/// getcolumndata(con,sqlcommand,table_name, column_name) 进行查询后返回一条记录时取得一个数据库中相应字段的值, 其中con 为已带有连接字符串的参数
///connconnection() 连接数据库
/// getusertype() 判断用户是否通过验证,如果没有就跳转到login.aspx
/// </summary>
/// 公共函数
public class myclass
{ public string msg; //出错信息
public string sqlcommand; //查询字符串 //以下5项为getcolumndata()的参数
public string table_name; //数据库中的表名
public string column_name; //数据表中的列名
public System.Data.SqlClient.SqlConnection con; //数据库的连接
public myclass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//连接数据库的方法
public bool openconnection(System.Data.SqlClient.SqlConnection con )
{
bool i=true ; //测试数据库是否连接上
try
{ con.Open(); }
catch
{
msg="数据库不能连接,请联系管理员";
con.Dispose();
i=false;
}
return i;
}
public void messagebox(System.Web.UI.Page page,string msg) //应用时page参数用this
{
string script="<script> alert('"+ msg +"')</script>";
page.Response.Write(script);
}
/// <summary>
/// //绑定数据,其中参数为connection, datagrid、查询字符串及数据库中的表名
/// </summary>
public void mydatabind(System.Data.SqlClient.SqlConnection con, System.Web.UI.WebControls.DataGrid dg, string sqlstring,string mytable)
{ //绑定数据,其中参数为connection, datagrid、查询字符串及数据库中的表名
if(openconnection(con))
{
System.Data.SqlClient.SqlDataAdapter myadp=new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlCommand mycmd=new System.Data.SqlClient.SqlCommand();
System.Data.DataSet myds =new System.Data.DataSet();
mycmd.CommandTimeout =30;
mycmd.Connection =con;
mycmd.CommandText =sqlstring; //设置参数
myadp.SelectCommand =mycmd;
myadp.Fill(myds,mytable); //填充数据集
dg.DataSource =myds.Tables[mytable].DefaultView; //设置数据源
dg.DataBind(); //数据绑定
myadp.Dispose();
myds.Dispose();
mycmd.Dispose();
}
}
/// <summary>
/// 取得一个数据库中相应字段的值,无参数时调用成员属性为参数
/// </summary>
public string getcolumndata()
{
return getcolumndata( con, sqlcommand, table_name,column_name) ;
}
/// <summary>
/// 取得一个数据库中相应字段的值
/// </summary>
public string getcolumndata(System.Data.SqlClient.SqlConnection con,string sqlcommand,string table_name,string column_name) //取得一个数据库中相应字段的值
{ string colunm_data=""; //返回值 //错误时将返回空值
if(!openconnection(con))
{
msg="数据库不能连接,请联系管理员" ;
return colunm_data ;
}
else
{
System.Data.SqlClient.SqlDataAdapter adp=new System.Data.SqlClient.SqlDataAdapter() ; //创建sqldataadapter对象
System.Data.SqlClient.SqlCommand com =new System.Data.SqlClient.SqlCommand();
com.CommandTimeout =30;
com.Connection =con;
com.CommandText =sqlcommand;
adp.SelectCommand =com;
System.Data.DataSet ds =new System.Data.DataSet() ; //创建一个数据集
try
{ adp.Fill(ds,table_name);}
catch
{
msg="数据库查询错误,请重新设置查询条件";
adp.Dispose();
ds.Dispose();
com.Dispose();
return colunm_data ;
}
if(ds.Tables[table_name].Rows.Count ==0)
{
msg="在数据库中没有相应的记录";
return colunm_data ;
}
colunm_data=ds.Tables[table_name].Rows[0][column_name].ToString() ;//取出数据字段的值
if(colunm_data=="")
{
msg="在数据库中没有相应的记录";
}
else
{
msg="查询成功";
}
con.Close();
return colunm_data ;
}
}
/// <summary>
/// 使用用户名与密码的登陆过程,其中page这个参数给弹出框使用
/// </summary>
public bool login(System.Web.UI.Page mypage, System.Data.SqlClient.SqlConnection myconnection, string myuser, string mypassword)
{
System.Data.SqlClient.SqlCommand com =new System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataReader dr;
com.Connection=myconnection;
com.CommandText="SELECT * FROM admin WHERE users = '" +myuser +"'";
if (myconnection.State.ToString() =="Closed" ) //如果数据库没有连接,就连接
{
try
{
myconnection.Open();
}
catch
{
messagebox(mypage," 数据库不能连接,请联系管理员");
return false;
}
}
dr=com.ExecuteReader();
if (!dr.Read() )
{
messagebox(mypage,"没有这个用户名");
return false;
}
if (mypassword==dr.GetValue(2).ToString() )
{
mypage.Session["usertype"]=dr.GetValue(3).ToString() ;
mypage.Session["user"]=dr.GetValue(2).ToString();
return true;
}
else
{
messagebox(mypage,"密码不正确,请重新输入");
return false;
}
}
}
}