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 fdName,string fdValue)
{
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+" Where "+fdName+" = '"+fdValue+"' 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="fdName"></param>
/// <param name="fdValue"></param>
/// <returns></returns>
public int outPutvalIsValid(string tbName,string fdName,string fdValue)
{
int isValid;
SqlDataReader dr;
dr = this.SqlGetReader("Select * From "+tbName+" Where "+fdName+" = '"+fdValue+"'");
if (dr.HasRows)
{
isValid = 1;//失败
}
else
{
isValid = 0;//成功
}
return isValid;
}
/// <summary>
///
/// </summary>
/// <param name="tbName"></param>
/// <param name="fdName1"></param>
/// <param name="fdValue1"></param>
/// <param name="fdName2"></param>
/// <param name="fdValue2"></param>
/// <returns></returns>
public int outPutvalIsValid(string tbName,string fdName1,string fdValue1,string fdName2,string fdValue2)
{
int isValid;
SqlDataReader dr;
dr = this.SqlGetReader("Select * From "+tbName+" Where "+fdName1+" = '"+fdValue1+"' And "+fdName2+" = '"+fdValue2+"'");
if (dr.HasRows)
{
isValid = 1;//失败
}
else
{
isValid = 0;//成功
}
return isValid;
}
/// <summary>
/// 检验字段
/// </summary>
/// <param name="tbName"></param>
/// <param name="fdName1"></param>
/// <param name="fdValue1"></param>
/// <param name="fdName2"></param>
/// <param name="fdValue2"></param>
/// <param name="fdName3"></param>
/// <param name="fdValue3"></param>
/// <returns></returns>
public int outPutvalIsValid(string tbName,string fdName1,string fdValue1,string fdName2,string fdValue2,string fdName3,string fdValue3)
{
int isValid;
SqlDataReader dr;
dr = this.SqlGetReader("Select * From "+tbName+" Where "+fdName1+" = '"+fdValue1+"' And "+fdName2+" = '"+fdValue2+"' And " + fdName3 +" = '"+fdValue3+"'");
if (dr.HasRows)
{
isValid = 1;//失败
}
else
{
isValid = 0;//成功
}
return isValid;
}
/// <summary>
/// 取出表中另一个字段内容!
/// </summary>
/// <param name="tbName"></param>
/// <param name="fdName"></param>
/// <param name="fdValue"></param>
/// <param name="valName"></param>
/// <returns></returns>
public string outPutOtherFD(string tbName,string fdName,string fdValue,string valName)
{
string strOutValue="";
SqlDataReader dr=this.SqlGetReader("Select "+valName+" From "+tbName+" Where "+fdName+"='"+fdValue+"'");
if (dr.HasRows)
{
dr.Read();
strOutValue=dr[valName].ToString();
dr.Close();
}
else
{
strOutValue="Error!";
}
return strOutValue;
}
public void Close()
{
sqlCmd.Cancel();
sqlCmd.Dispose();
sqlConn.Close();
sqlConn.Dispose();
ds.Clear();
ds.Dispose();
}
#endregion
#region 返回记录数值
/// <summary>
/// 返回记录数值
/// </summary>
public int RecordTotal
{
get
{
sqlCmd.Cancel();
sqlCmd.CommandText=SQL_SELECT;
SqlDataReader sqlDr;
sqlDr=sqlCmd.ExecuteReader();
while (sqlDr.Read())
{
record_total++;
}
sqlDr.Close();
return record_total;
}
}
#endregion
}
}
来源:博远电子(软件定制),如涉及版权问题请与我们联系。
TAG
软件定制,软件开发,瀚森HANSEN,辽宁,沈阳,抚顺