📄 config.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
namespace AspxCnMob
{
/// <summary>
//============.net类Wap版1.0=================
//
// '''
// (0 0)
// +-----oOO----(_)------------+
// | |
// | 作者:高处不胜寒 |
// | QQ:28767360 |
// | AspXCn QQ群: |
// | 类型:Wap版 |
// | 适用数据库:Sql Sever |
// | 编写时间:2005-07-20 |
// | |
// +------------------oOO------+
// |__|__|
// || ||
// ooO Ooo
//
//===========================================
/// </summary>
public class Config :System.Web.UI.MobileControls.MobilePage
{
public SqlConnection cn;
private SqlDataAdapter ada;
private DataSet ds;
public DataRow dr;
public SqlTransaction tran;
//**********************************>> 基本数据库操作篇 <<**********************************//
/// <summary>
/// 打开数据库
/// </summary>
public void Open()
{
cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["AspxCnMob"]);
if (cn.State == ConnectionState.Closed)
cn.Open();
}
/// <summary>
/// 关闭数据库
/// </summary>
public void Close()
{
if (cn.State == ConnectionState.Open)
cn.Close();
}
/// <summary>
/// 返回一个SqlParameter实例
/// </summary>
/// <param name="ParamName"></param>
/// <param name="Value"></param>
/// <returns></returns>
public SqlParameter MakeParam(string ParamName,object Value)
{
return new SqlParameter(ParamName, Value);
}
/// <summary>
/// 调用存储过程创建一个SqlCommand对象
/// </summary>
/// <param name="procName">存储过程</param>
/// <param name="prams">给存储过程传递传输SqlParameter对象</param>
/// <returns></returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
Open();
SqlCommand cmd = new SqlCommand(procName, cn);
cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">给存储过程传递传输SqlParameter对象</param>
/// <returns></returns>
private void RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">给存储过程传递传输SqlParameter对象</param>
/// <param name="dataReader">输出一个DataReader对象</param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// 获得DataSet对象
/// </summary>
/// <param name="str_Sql">SQL语句</param>
/// <returns></returns>
public DataSet GetDs(string str_Sql)
{
Open();
SqlDataAdapter Ada = new SqlDataAdapter(str_Sql,cn);
DataSet ds = new DataSet();
Ada.Fill(ds);
cn.Close();
return ds;
}
/// <summary>
/// 获得DataSet对象
/// </summary>
/// <param name="tablename">内存表ID</param>
/// <param name="str_Sql">SQL语句</param>
/// <returns></returns>
public DataSet GetDs(string tablename,string str_Sql)
{
Open();
SqlDataAdapter Ada = new SqlDataAdapter(str_Sql,cn);
DataSet ds = new DataSet();
Ada.Fill(ds,tablename);
cn.Close();
return ds;
}
/// <summary>
/// 获得DataTable对象
/// </summary>
/// <param name="str_Sql">SQL语句</param>
/// <returns></returns>
public DataTable GetTable(string str_Sql)
{
return GetDs(str_Sql).Tables[0];
}
/// <summary>
/// 获得DataTable对象
/// </summary>
/// <param name="tablename">内存表ID</param>
/// <param name="str_Sql">SQL语句</param>
/// <returns></returns>
public DataTable GetTable(string tablename,string str_Sql)
{
return GetDs(str_Sql).Tables[tablename];
}
public SqlDataReader GetDataReader(string str_Sql)
{
Open();
SqlCommand cmd = new SqlCommand(str_Sql,cn);
SqlDataReader dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return dataReader;
}
/// <summary>
/// 执行Sql语句
/// </summary>
/// <param name="str_Sql">Sql语句</param>
public void RunSql(string str_Sql)
{
Open();
SqlCommand cmd = new SqlCommand(str_Sql,cn);
cmd.ExecuteNonQuery();
cn.Close();
}
//**********************************>> 控件绑定篇 <<**********************************//
public void BindCtrl(string str_Sql,string str_val,string str_txt,Control ctl_Listctl)
{
try
{
SqlDataReader dr=GetDataReader(str_Sql);
if (ctl_Listctl is System.Web.UI.MobileControls.ObjectList)
{
((System.Web.UI.MobileControls.ObjectList)ctl_Listctl).DataSource=dr;
((System.Web.UI.MobileControls.ObjectList)ctl_Listctl).DataBind();
}
if (ctl_Listctl is System.Web.UI.MobileControls.List)
{
((System.Web.UI.MobileControls.List)ctl_Listctl).DataSource=dr;
((System.Web.UI.MobileControls.List)ctl_Listctl).DataValueField=str_val;
((System.Web.UI.MobileControls.List)ctl_Listctl).DataTextField=str_txt;
((System.Web.UI.MobileControls.List)ctl_Listctl).DataBind();
}
if (ctl_Listctl is System.Web.UI.MobileControls.SelectionList)
{
((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).DataSource=dr;
((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).DataValueField=str_val;
((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).DataTextField=str_txt;
((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).DataBind();
}
cn.Close();
}
catch
{
cn.Close();
}
}
public string GetCtrlVal(Control ctl_Listctl)
{
string val="";
if (ctl_Listctl is System.Web.UI.MobileControls.List)
{
}
if (ctl_Listctl is System.Web.UI.MobileControls.SelectionList)
{
bool flag=true;
for (int i=0;i<((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).Items.Count;i++)
{
if (((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).Items[i].Selected==true)
{
if (flag==true)
{
val=((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).Items[i].Text;
flag=false;
}
else
{
val=val+","+((System.Web.UI.MobileControls.SelectionList)ctl_Listctl).Items[i].Text;
}
}
}
}
return val;
}
//**********************************>> 数据库操作篇(包括添加Add,修改Edit,删除Del,详细Detail) <<**********************************//
/// <summary>
/// 读出详细信息赋给预先定义好的变量数组
/// </summary>
/// <param name="ref_fileds">字段数组,比如:string[] ref_fileds={"name","age"}</param>
/// <param name="ref_Variables">变量数组,string[] ref_Variables={"",""}</param>
/// <param name="str_Sql">SQL语句</param>
public void GetDetail(string [] ref_Fileds,ref string [] ref_Ctrls, string str_Sql)
{
SqlDataReader dr=GetDataReader(str_Sql);
while (dr.Read())
{
for (int j=0;j<ref_Fileds.Length;j++)
{
ref_Ctrls[j]=dr[ref_Fileds[j]].ToString();
}
break;
}
dr.Close();
}
/// <summary>
/// 获得表记录数
/// </summary>
/// <param name="table_name">表名或者表名+条件,GetRsCount("t_user where id="+Request["id"])</param>
/// <returns></returns>
public int GetRsCount(string table_name)
{
string strSql;
int intCount;
Open();
strSql="select count(*) from "+table_name;
SqlCommand cmd=new SqlCommand(strSql,cn);
intCount=(int)cmd.ExecuteScalar();
cn.Close();
return intCount;
}
/// <summary>
/// 获得单个int类型字段总和
/// </summary>
/// <param name="field">字段</param>
/// <param name="table_name">表名或者表名+条件,GetFiledSum("id","t_user where id="+Request["id"])</param>
/// <returns></returns>
public string GetFiledSum(string field,string table_name)
{
string SumValue;
Open();
SqlCommand cmd=new SqlCommand("select sum("+field+") as s from "+table_name,cn);
SumValue=cmd.ExecuteScalar().ToString();
cn.Close();
return SumValue;
}
public string GetFiledSum(string field)
{
string SumValue;
Open();
SqlCommand cmd=new SqlCommand(field,cn);
SumValue=cmd.ExecuteScalar().ToString();
cn.Close();
return SumValue;
}
public string GetFiledStr(string field,string table_name)
{
string val="";
DataTable dt=GetDs("Select "+field+" from "+table_name).Tables[0];
for (int i=0;i<dt.Rows.Count;i++)
{
if (i==0)
{
val=dt.Rows[i][field].ToString();
}
else
{
val=val+","+dt.Rows[i][field].ToString();
}
}
return val;
}
/// <summary>
/// 获得单个字段值
/// </summary>
/// <param name="str_Sql">Sql语句</param>
/// <returns></returns>
public string GetFiledValue(string str_Sql)
{
string str;
Open();
SqlCommand cmd=new SqlCommand(str_Sql,cn);
str=cmd.ExecuteScalar().ToString();
cn.Close();
return str;
}
/// <summary>
/// 获得表记录数
/// </summary>
/// <param name="table_name">表名或者表名+条件,GetRsCount("t_user where id="+Request["id"])</param>
/// <returns></returns>
public int GetMaxId(string filed,string table_name)
{
string strSql;
int intCount;
Open();
strSql="select max("+filed+") from "+table_name;
SqlCommand cmd=new SqlCommand(strSql,cn);
object obj=cmd.ExecuteScalar();
if (obj==System.DBNull.Value)
{
intCount=1;
}
else
{
intCount = Convert.ToInt32(cmd.ExecuteScalar())+1;
}
cn.Close();
return intCount;
}
/// <summary>
/// 通过SqlCommandBuilder对象增加数据库记录
/// </summary>
/// <param name="sql">Select-SQL语句</param>
public void Builder(string str_Sql)
{
Open();
ada=new SqlDataAdapter(str_Sql,cn);
SqlCommandBuilder myCommandBuilder=new SqlCommandBuilder(ada);
ds=new DataSet();
ada.Fill(ds);
dr=ds.Tables[0].NewRow();
}
/// <summary>
/// 关闭SqlCommandBuilder对象
/// </summary>
public void BuilderClose()
{
ds.Tables[0].Rows.Add(dr);
ada.Update(ds); // 更新数据库
cn.Close(); // 关闭数据库
ds.Clear(); // 清空DataSet对象
}
/// <summary>
/// 通过SqlCommandBuilder对象修改数据库记录
/// </summary>
/// <param name="sql">Select-SQL语句</param>
public void BuilderEdit(string str_Sql)
{
Open();
ada=new SqlDataAdapter(str_Sql,cn);
SqlCommandBuilder myCommandBuilder=new SqlCommandBuilder(ada);
ds=new DataSet();
ada.Fill(ds);
dr=ds.Tables[0].Rows[0];
}
/// <summary>
/// 关闭SqlCommandBuilder对象
/// </summary>
public void BuilderEditClose()
{
ada.Update(ds); // 更新数据库
cn.Close(); // 关闭数据库
ds.Clear(); // 清空DataSet对象
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -