using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace MyDB
{
/// <summary>
/// DbControl 因修改要求涉及三层结构改动,设置Web层DB操作类处理。
/// </summary>
public class DbControl
{
private int record_total=0;
protected string DBTYPE,SQL_SELECT;
protected SqlConnection sqlConn;
protected SqlCommand sqlCmd;
protected DataSet ds=new DataSet();
protected SqlConnection objconn;
public DbControl()
{
//
// TODO: 在此处添加构造函数逻辑
//
sqlConn=new SqlConnection(ConfigurationSettings.AppSettings["StrConnection"]);
objconn=new SqlConnection(ConfigurationSettings.AppSettings["StrConnection"]);
}
#region 收集
public SqlConnection objconnopen
{
get
{
objconn.Open();
return objconn;
}
}
public void objconnclose()
{
objconn.Close();
objconn.Dispose();
}
#endregion
#region 数据库各种操作方法
/// <summary>
/// 得到Reader对象
/// </summary>
/// <param name="strQuery"></param>
/// <returns></returns>
public SqlDataReader SqlGetReader(string strQuery)
{
SQL_SELECT=strQuery;
sqlCmd=new SqlCommand(strQuery,sqlConn);
SqlDataReader dr;
try
{
sqlCmd.Connection.Close();
sqlCmd.Connection.Open();
}
catch(Exception e)
{
throw e;
}
dr=sqlCmd.ExecuteReader();
return dr;
}
/// <summary>
/// 执行Command操作
/// </summary>
/// <param name="strQuery"></param>
/// <returns></returns>
public int SqlRunCommand(string strQuery)
{
SQL_SELECT=strQuery;
sqlCmd=new SqlCommand(strQuery,sqlConn);
//sqlCmd.Cancel();
try
{
sqlCmd.Connection.Close();
sqlCmd.Connection.Open();
}
catch(Exception e)
{
throw e;
}
return sqlCmd.ExecuteNonQuery();
}
/// <summary>
/// 得到结果集数
/// </summary>
/// <param name="strQuery"></param>
/// <returns></returns>
public int SqlRunCommandScalar(string strQuery)
{
SQL_SELECT=strQuery;
sqlCmd=new SqlCommand(strQuery,sqlConn);
//sqlCmd.Cancel();
try
{
sqlCmd.Connection.Close();
sqlCmd.Connection.Open();
}
catch(Exception e)
{
throw e;
}
return Convert.ToInt32(sqlCmd.ExecuteScalar());
}
/// <summary>
/// 得到DataTable
/// </summary>
/// <param name="strQuery"></param>
/// <param name="strTable"></param>
/// <returns></returns>
public DataTable SqlGetDataset(string strQuery,string strTable)
{
SQL_SELECT=strQuery;
sqlCmd=new SqlCommand(strQuery,sqlConn);
try
{
sqlCmd.Connection.Close();
sqlCmd.Connection.Open();
}
catch(Exception e)
{
throw e;
}
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=sqlCmd;
da.Fill(ds,strTable);
da.Dispose();
return ds.Tables[strTable];
}
#endregion
#region 返回Script值
public string RetrunScript(string strscript)
{
return "<script>alert('"+strscript+"');history.back(-1);</script>";
}
public string RetrunScript(string strscript,int intflag)
{
if (intflag==1)
{
return "<script>alert('"+strscript+"');window.close();</script>";
}
else
{
return "<script>alert('"+strscript+"');</script>";
}
//return "<script>alert('"+strscript+"');window.close();</script>";
}
public string RetrunScript(string strscript,string returnurl)
{
return "<script>alert('"+strscript+"');this.location='"+returnurl+"';</script>";
}
#endregion
#region 手动代码填充区
public int rizhi(string mk,string rrbh)
{
//SQL_SELECT=strQuery;
sqlCmd=new SqlCommand("insert into rizhi (rrbh,mk,riqi)values('"+rrbh+"','"+mk+"','"+DateTime.Now.ToString()+"')",sqlConn);
//sqlCmd.Cancel();
try
{
sqlCmd.Connection.Close();
sqlCmd.Connection.Open();
}
catch(Exception e)
{
throw e;
}
return sqlCmd.ExecuteNonQuery();
}
/// <summary>
/// 自动产生编号
/// </summary>
/// <param name="tbName"></param>
/// <param name="idName"></param>
/// <param name="idHeader"></param>
/// <returns></returns>
public string outPutMaxID(string tbName,string idName,string idHeader)
{
string tmpID = idHeader + "000000" + "1" ;
string iMaxID = "";
int iVal = 0;
SqlDataReader dr;
//dr = this.SqlGetReader("Select Max("+idName+") From "+tbName+"");
dr = this.SqlGetReader("Select Top 1 * From "+tbName+" Order By "+idName+" DESC");
if (dr.HasRows)
{
dr.Read();
iMaxID = dr[idName].ToString();
iVal = Convert.ToInt32(iMaxID.Substring(3,7));
iVal += 1;
iVal=iVal+1000000000;
iMaxID = iVal.ToString().Substring(3,7);
iMaxID =idHeader + iMaxID;
dr.Close();
}
else{
iMaxID = tmpID;
}
return iMaxID;
}
/// <summary>
/// 产生显示编码使用
/// </summary>
/// <param name="tbName">表名</param>
/// <param name="idName">字段名</param>
/// <param name="idHeader">编码头</param>
/// <param name="fdName">标识字段名</param>
/// <param name="fdValue">标识字段值</param>
/// <returns>编号</returns>
public string outPutMaxID(string tbName,string idName,string idHeader,string fd