using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebSqlHelper
{
///
/// 泛型实现单例模式
///
/// 需要实现单例的类
public class Singleton where T : new()
{
///
/// 返回类的实例
///
public static T Instance
{
get { return SingletonCreator.instance; }
}
class SingletonCreator
{
internal static readonly T instance = new T();
}
}
public class BaseClass
{
public static SqlDbHelper db = new SqlDbHelper("DongShengDB");
#region
//public static System.Data.DataTable ExecutePager(string tableName, string fieldName, string where, string orderby, int pagesize, int pageindex, ref int recordcount)
//{
// int PageLowerBound = (pageindex - 1) * pagesize;
// int PageUpperBound = PageLowerBound + pagesize;
//}
#endregion
[Obsolete]
public static System.Data.DataTable ExecutePager(string table, string field, string key, string where, string orderby, int pagesize, int pageindex, ref int recordcount, params System.Data.Common.DbParameter[] param)
{
int PageLowerBound = (pageindex - 1) * pagesize;
int PageUpperBound = PageLowerBound + pagesize;
string k = key.Substring(0, key.IndexOf(' '));// (string.IsNullOrEmpty(key) ? field : key);
string t = key.Substring(key.IndexOf(' '));
string s = @"declare @indextable table(index_id__ bigint identity(1,1),index_" + k + t + @")
insert into @indextable(index_" + k + ") select " + k + " from " + table + (string.IsNullOrEmpty(where) ? "" : " where " + where) + (string.IsNullOrEmpty(orderby) ? "" : " order by " + orderby) + @"
select count(*) from @indextable
select " + field + " from " + table + " left join @indextable on index_" + k + "=" + k + " where index_id__>" + PageLowerBound.ToString() + " and index_id__<=" + PageUpperBound.ToString() + " order by index_id__";
System.Data.DataSet ds = db.GetSqlStrDataSet(s, param);
recordcount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
return ds.Tables[1];
}
[Obsolete]
public static System.Data.DataTable ExecutePager2(string table, string field, string key, string where, string orderby, int pagesize, int pageindex, ref int recordcount, params System.Data.Common.DbParameter[] param)
{
int PageLowerBound = (pageindex - 1) * pagesize;
int PageUpperBound = PageLowerBound + pagesize;
string k = key.Substring(0, key.IndexOf(' '));// (string.IsNullOrEmpty(key) ? field : key);
string t = key.Substring(key.IndexOf(' '));
string s = @"declare @indextable table(index_id__ bigint identity(1,1),index_" + k + t + @")
insert into @indextable(index_" + k + ") select " + k + " from " + table + (string.IsNullOrEmpty(where) ? "" : " where " + where) + (string.IsNullOrEmpty(orderby) ? "" : " order by " + orderby) + @"
select count(*) from @indextable
select a__.nid, a__.gcmc, a__.jzdz, a__.jsdw, a__.jzmj, a__.yjsbhs, a__.reportcodes, a__.com,a__.stf,a__.spd,a__.jcf,max(index_id__) from " + table + " left join @indextable on index_" + k + "=" + k + " where index_id__>" + PageLowerBound.ToString() + " and index_id__<=" + PageUpperBound.ToString() + " group by a__.nid, a__.gcmc, a__.jzdz, a__.jsdw, a__.jzmj, a__.yjsbhs, a__.reportcodes, a__.com,a__.stf,a__.spd,a__.jcf,a__.jfrq order by max(index_id__)";
System.Data.DataSet ds = db.GetSqlStrDataSet(s, param);
recordcount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
return ds.Tables[1];
}
[Obsolete]
public static System.Data.DataTable ExecutePager(string table, string field, string key1, string key2, string where, string orderby, int pagesize, int pageindex, ref int recordcount, params System.Data.Common.DbParameter[] param)
{
int PageLowerBound = (pageindex - 1) * pagesize;
int PageUpperBound = PageLowerBound + pagesize;
string k = key1.Substring(0, key1.IndexOf(' '));// (string.IsNullOrEmpty(key) ? field : key);
string t = key1.Substring(key1.IndexOf(' '));
string k2 = key2.Substring(0, key2.IndexOf(' '));// (string.IsNullOrEmpty(key) ? field : key);
string t2 = key2.Substring(key2.IndexOf(' '));
string s = @"declare @indextable table(index_id__ bigint identity(1,1),index_" + k + t + ",index_k2" + t2 + @")
insert into @indextable(index_" + k + ",index_k2) select " + k + "," + k2 + " from " + table + (string.IsNullOrEmpty(where) ? "" : " where " + where) + (string.IsNullOrEmpty(orderby) ? "" : " order by " + orderby) + @"
select count(*) from @indextable
select " + field + " from " + table + " left join @indextable on index_" + k + "=" + k + " and index_k2=" + k2 + " where index_id__>" + PageLowerBound.ToString() + " and index_id__<=" + PageUpperBound.ToString() + " order by index_id__";
System.Data.DataSet ds = db.GetSqlStrDataSet(s, param);
recordcount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
return ds.Tables[1];
}
public static DataSet GetDataSet(SqlMapCollection sql)
{
if (sql != null)
{
string str = "";
for (int i = 0; i < sql.Count; i++)
{
str += sql[i].Sql + " ";
}
return db.LoadDataSet(sql.Name, str, sql.Param);
}
return null;
}
public static DataTable GetStruct(string table)
{
return db.GetSqlStrTable("select * from " + table + " where 1>2");
}
static bool ContainColumn(DataTable dt, string n)
{
foreach (DataColumn c in dt.Columns)
if (c.ColumnName.ToLower() == n.ToLower())
return true;
return false;
}
public static int GetNext(string table, string field, string where)
{
object c = db.GetSqlStrScalar("select max(case when ISNUMERIC(" + field + ")=1 then " + field + " else 0 end) from " + table + (string.IsNullOrEmpty(where) ? "" : " where " + where));
if (c == null || c is DBNull)
return 1;
return int.Parse(c.ToString()) + 1;
}
#region InitValue
///
/// 禁用服务器控件 Control ID
///
///
public static void DisabledControls(Control beginP, Control endP)
{
ControlCollection cc = beginP.Parent.Controls;
if (endP == null)
return;
else
{
foreach (Control c in cc)
{
if (c is Label)
((Label)c).Enabled = false;
if (c is TextBox)
{
((TextBox)c).Enabled = false;
((TextBox)c).ReadOnly = true;
}
if (c is CheckBox)
((CheckBox)c).Enabled = false;
if (c is CheckBoxList)
{
((CheckBoxList)c).Enabled = false;
}
if (c is DropDownList)
((DropDownList)c).Enabled = false;
if (c is RadioButtonList)
((RadioButtonList)c).Enabled = false;
if (c is LinkButton)
{
(c as LinkButton).Enabled = false;
}
if (c is HyperLink)
{
(c as HyperLink).Enabled = false;
}
if (c.ID == endP.ID)
{
return;
}
}
}
}
public static void InitValue(Control p, DataRow dr)
{
foreach (Control c in p.Parent.Controls)
{
if (!string.IsNullOrEmpty(c.ID) && ContainColumn(dr.Table, c.ID))
{
if (c is Literal)
((Literal)c).Text = dr[c.ID].ToString();
if (c is Label)
((Label)c).Text = dr[c.ID].ToString();
if (c is TextBox)
((TextBox)c).Text = dr[c.ID].ToString();
if (c is CheckBox)
((CheckBox)c).Checked = dr[c.ID].ToString() == "1";
if (c is CheckBoxList)
{
string[] sr = dr[c.ID].ToString().Split(',');
((CheckBoxList)c).ClearSelection();
foreach (string s in sr)
{
if (((CheckBoxList)c).Items.FindByValue(s) != null)
((CheckBoxList)c).Items.FindByValue(s).Selected = true;
}
}
if (c is DropDownList)
HttpHelper.SelectComBox((DropDownList)c, dr[c.ID].ToString());
if (c is RadioButtonList)
((RadioButtonList)c).SelectedValue = dr[c.ID].ToString();
if (c is HiddenField)
{
((HiddenField)c).Value = dr[c.ID].ToString();
}
}
}
}
public static void CreateSql(Control p, DataTable table, ref string s1, ref string s2, ref string s3, ref List param)
{
foreach (Control c in p.Parent.Controls)
{
if (!string.IsNullOrEmpty(c.ID) && ContainColumn(table, c.ID))
{
s1 += c.ID + ",";
s2 += "@" + c.ID + ",";
s3 += c.ID + "=@" + c.ID + ",";
if ((c is DropDownList || c is CheckBoxList) && c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
s1 += c.ID.TrimEnd("id".ToCharArray()) + ",";
s2 += "@" + c.ID.TrimEnd("id".ToCharArray()) + ",";
s3 += c.ID.TrimEnd("id".ToCharArray()) + "=@" + c.ID.TrimEnd("id".ToCharArray()) + ",";
}
string o = "";
if (c is TextBox)
{
if (((TextBox)c).TextMode == TextBoxMode.Password && ((TextBox)c).Text == "")
o = ((TextBox)p.Parent.FindControl(c.ID + "_hid")).Text;
else
//o=p.Page.get
o = string.IsNullOrEmpty(((TextBox)c).Text.Trim()) ? null : ((TextBox)c).Text.Trim();
}
if (c is CheckBox) o = ((CheckBox)c).Checked ? "1" : "0";
if (c is Label)
{
o = string.IsNullOrEmpty(((Label)c).Text.Trim()) ? null : ((Label)c).Text.Trim();
}
if (c is DropDownList)
{
if (c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
param.Add(db.GetParameter(c.ID.TrimEnd("id".ToCharArray()), !string.IsNullOrEmpty(((DropDownList)c).SelectedValue) ? ((DropDownList)c).SelectedItem.Text : ""));
param.Add(db.GetParameter(c.ID, ((DropDownList)c).SelectedItem.Value));
continue;
}
else
o = ((DropDownList)c).SelectedValue;
}
if (c is CheckBoxList)
{
string s = "", ss = "";
foreach (ListItem li in ((CheckBoxList)c).Items)
{
if (li.Selected)
{
s += li.Value + ",";
ss += li.Text + ",";
}
}
if (c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
param.Add(db.GetParameter(c.ID.TrimEnd("id".ToCharArray()), !string.IsNullOrEmpty(s.TrimEnd(',')) ? ss.TrimEnd(',') : ""));
param.Add(db.GetParameter(c.ID, s.TrimEnd(',')));
continue;
}
o = s.TrimEnd(',');
}
if (c is HiddenField)
{
o = ((HiddenField)c).Value;
}
if (!(c is Literal))
param.Add(db.GetParameter(c.ID, o));
}
}
}
public static void CreateSql(DataRow p, DataTable table, ref string s1, ref string s2, ref string s3, ref List param)
{
foreach (DataColumn c in p.Table.Columns)
{
if (p[c.ColumnName].ToString() != "")
{
if (!string.IsNullOrEmpty(c.ColumnName) && ContainColumn(table, c.ColumnName))
{
s1 += c.ColumnName + ",";
s2 += "@" + c.ColumnName + ",";
s3 += c.ColumnName + "=@" + c.ColumnName + ",";
if (c.DataType == System.Type.GetType("System.DateTime"))
param.Add(db.GetParameter(c.ColumnName, p[c.ColumnName], DbType.DateTime));
else
param.Add(db.GetParameter(c.ColumnName, p[c.ColumnName]));
}
}
}
}
///
/// 只返回Insert语句
///
///
///
///
///
///
public static void CreateSql(Control p, DataTable table, ref string s1, ref string s2, ref List param)
{
foreach (Control c in p.Parent.Controls)
{
if (!string.IsNullOrEmpty(c.ID) && ContainColumn(table, c.ID))
{
s1 += c.ID + ",";
s2 += "@" + c.ID + ",";
if ((c is DropDownList || c is CheckBoxList) && c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
s1 += c.ID.TrimEnd("id".ToCharArray()) + ",";
s2 += "@" + c.ID.TrimEnd("id".ToCharArray()) + ",";
}
string o = "";
if (c is TextBox)
{
if (((TextBox)c).TextMode == TextBoxMode.Password && ((TextBox)c).Text == "")
o = ((TextBox)p.Parent.FindControl(c.ID + "_hid")).Text;
else
//o=p.Page.get
o = string.IsNullOrEmpty(((TextBox)c).Text.Trim()) ? null : ((TextBox)c).Text.Trim();
}
if (c is CheckBox) o = ((CheckBox)c).Checked ? "1" : "0";
if (c is Label)
{
o = string.IsNullOrEmpty(((Label)c).Text.Trim()) ? null : ((Label)c).Text.Trim();
}
if (c is DropDownList)
{
if (c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
param.Add(db.GetParameter(c.ID.TrimEnd("id".ToCharArray()), !string.IsNullOrEmpty(((DropDownList)c).SelectedValue) ? ((DropDownList)c).SelectedItem.Text : ""));
param.Add(db.GetParameter(c.ID, ((DropDownList)c).SelectedItem.Value));
continue;
}
else
o = ((DropDownList)c).SelectedValue;
}
if (c is CheckBoxList)
{
string s = "", ss = "";
foreach (ListItem li in ((CheckBoxList)c).Items)
{
if (li.Selected)
{
s += li.Value + ",";
ss += li.Text + ",";
}
}
if (c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
param.Add(db.GetParameter(c.ID.TrimEnd("id".ToCharArray()), !string.IsNullOrEmpty(s.TrimEnd(',')) ? ss.TrimEnd(',') : ""));
param.Add(db.GetParameter(c.ID, s.TrimEnd(',')));
continue;
}
o = s.TrimEnd(',');
}
if (c is HiddenField)
{
o = ((HiddenField)c).Value;
}
if (!(c is Literal))
param.Add(db.GetParameter(c.ID, o));
}
}
}
///
/// 只返回Update语句
///
///
///
///
///
public static void CreateSql(Control p, DataTable table, ref string s3, ref List param)
{
foreach (Control c in p.Parent.Controls)
{
if (!string.IsNullOrEmpty(c.ID) && ContainColumn(table, c.ID))
{
s3 += c.ID + "=@" + c.ID + ",";
if ((c is DropDownList || c is CheckBoxList) && c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
s3 += c.ID.TrimEnd("id".ToCharArray()) + "=@" + c.ID.TrimEnd("id".ToCharArray()) + ",";
}
string o = "";
if (c is TextBox)
{
if (((TextBox)c).TextMode == TextBoxMode.Password && ((TextBox)c).Text == "")
o = ((TextBox)p.Parent.FindControl(c.ID + "_hid")).Text;
else
//o=p.Page.get
o = string.IsNullOrEmpty(((TextBox)c).Text.Trim()) ? null : ((TextBox)c).Text.Trim();
}
if (c is CheckBox) o = ((CheckBox)c).Checked ? "1" : "0";
if (c is Label)
{
o = string.IsNullOrEmpty(((Label)c).Text.Trim()) ? null : ((Label)c).Text.Trim();
}
if (c is DropDownList)
{
if (c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
param.Add(db.GetParameter(c.ID.TrimEnd("id".ToCharArray()), !string.IsNullOrEmpty(((DropDownList)c).SelectedValue) ? ((DropDownList)c).SelectedItem.Text : ""));
param.Add(db.GetParameter(c.ID, ((DropDownList)c).SelectedItem.Value));
continue;
}
else
o = ((DropDownList)c).SelectedValue;
}
if (c is CheckBoxList)
{
string s = "", ss = "";
foreach (ListItem li in ((CheckBoxList)c).Items)
{
if (li.Selected)
{
s += li.Value + ",";
ss += li.Text + ",";
}
}
if (c.ID.EndsWith("id") && ContainColumn(table, c.ID.TrimEnd("id".ToCharArray())))
{
param.Add(db.GetParameter(c.ID.TrimEnd("id".ToCharArray()), !string.IsNullOrEmpty(s.TrimEnd(',')) ? ss.TrimEnd(',') : ""));
param.Add(db.GetParameter(c.ID, s.TrimEnd(',')));
continue;
}
o = s.TrimEnd(',');
}
if (c is HiddenField)
{
o = ((HiddenField)c).Value;
}
if (!(c is Literal))
param.Add(db.GetParameter(c.ID, o));
}
}
}
public static void InitCheck(Control p, DataRow dr)
{
foreach (Control c in p.Parent.Controls)
{
if (c is CheckBox && !string.IsNullOrEmpty(c.ID) && ContainColumn(dr.Table, c.ID.Replace("chk_", "")))
{
c.Visible = true;
}
}
}
public static void InitPZ(Control p, string nid)
{
DataTable dt = db.GetSqlStrTable("select * from u_report_pz where valid=1 and repid=@nid", db.GetParameter("nid", nid));
foreach (Control c in p.Parent.Controls)
{
if (c is Label && !string.IsNullOrEmpty(c.ID) && c.ID.StartsWith("lb_"))
{
DataRow[] dr = dt.Select("fieldname='" + c.ID.Replace("lb_", "") + "'");
if (dr.Length > 0)
((Label)c).BackColor = System.Drawing.Color.Red;
}
}
}
public static void PZ(Control p, string nid)
{
db.SetSqlStrNonQuery("update u_report_pz set valid=0 where repid=@id", db.GetParameter("id", nid));
foreach (Control c in p.Parent.Controls)
{
if (c is CheckBox && !string.IsNullOrEmpty(c.ID) && c.ID.StartsWith("chk_"))
{
if (((CheckBox)c).Checked)
{
db.SetSqlStrNonQuery("insert into u_report_pz (repid,fieldname,valid,createtime) values (@repid,@fieldname,1,getdate())"
, db.GetParameter("repid", nid), db.GetParameter("fieldname", c.ID.Replace("chk_", "")));
}
}
}
}
#endregion
}
}