using System;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Drawing;
namespace DataCon{
public class SqlData:Control,INamingContainer{
	public int TotalCols;
	public string SQLString{//目前SQL语法
		get{
			return ViewState["SQLString"].ToString();
		}
		set{
			ViewState["SQLString"]=value;
		}
	}
	[DefaultValue("")]public string ConnectSQL{//目前连至SQL的字符串
		get{
			return ViewState["ConnectSQL"].ToString();
		}
		set{
			ViewState["ConnectSQL"]=value;
		}
	}
	public string ServerName{//SQL服务器名称
		get{
			return ViewState["ServerName"].ToString();
		}
		set{
			ViewState["ServerName"]=value;
		}
	}
	public string Uid{//登录SQL的ID
		get{
			return ViewState["Uid"].ToString();
		}
		set{
			ViewState["Uid"]=value;
		}
	}
	public string Pass{//登陆SQL的密码
		get{
			return ViewState["Pass"].ToString();
		}
		set{
			ViewState["Pass"]=value;
		}
	}
	public string DBName{// 数据库名称
		get{
			return ViewState["DBName"].ToString();
		}
		set{
			ViewState["DBName"]=value;
		}
	}
	public string TBName{//DataTable名称
		get{
			return ViewState["TBName"].ToString();
		}
		set{
			ViewState["TBName"]=value;
		}
	}
	public int NowRecord{//目前条数
		get{
			return Convert.ToInt32(ViewState["NowRecord"]);
		}
		set{
			ViewState["NowRecord"]=Convert.ToInt32(value);
		}
	}
	public int AllRecords{//总条数
		get{
			return Convert.ToInt32(ViewState["AllRecords"]);
		}
		set{
			ViewState["AllRecords"]=Convert.ToInt32(value);
		}
	}
	public DataSet NowDataSet{//目前之Dataset
		get{
			return (DataSet)ViewState["NowDataSet"];
		}
		set{
			ViewState["NowDataSet"]=(DataSet)value;
		}
	}
	public string NowMode{//目前模式
		get{
			return ViewState["NowMode"].ToString();
		}
		set{
			ViewState["NowMode"]=value;
		}
	}
	public string DataKeyValue{//目前主键字段值
		get{
			return ViewState["DataKeyValue"].ToString();
		}
		set{
			ViewState["DataKeyValue"]=value;
		}
	}
	public string DataKeyField{//主键字段
		get{
			return ViewState["DataKeyField"].ToString();
		}
		set{
			ViewState["DataKeyField"]=value;
		}
	}
	public Boolean Save(){
		SqlConnection k = new SqlConnection(ConnectSQL);
		SqlDataAdapter Comm = new SqlDataAdapter(SQLString,k);
		DataSet DS = new DataSet();
		Comm.Fill(DS,TBName);
		try{
			for(int i=0; i <= DS.Tables[TBName].Columns.Count-1;i++){
				string CN = DS.Tables[TBName].Columns[i].ColumnName;
				TextBox tx = (TextBox)FindControl(CN);
				if(tx.Text != ""){
					DS.Tables[TBName].Rows[NowRecord][CN]=tx.Text;
				}
			}
			SqlCommandBuilder custCB = new SqlCommandBuilder(Comm);
			Comm.Update(DS,TBName);
			this.NowMode="View";
			bindNew();
			return true;
		}
		catch{
			return false;//保存时发生错误
		}
	}
	public void Delete(string DeleteString){
		string ConnectString ="server=" + this.ServerName + ";uid=" + this.Uid + ";pwd=" + this.Pass + ";database=" + this.DBName;
		this.ConnectSQL=ConnectString;
		SqlConnection k = new SqlConnection(ConnectString);
		SqlCommand DelRec = new SqlCommand(DeleteString,k);
		DelRec.Connection.Open();
		DelRec.ExecuteNonQuery();
		DelRec.Connection.Close();
		AllRecords-=1;
		if(NowRecord>0){
			NowRecord-=1;
		}
	}
	private void bindNew(){
		string ConnectString ="server=" + this.ServerName + ";uid=" + this.Uid + ";pwd=" + this.Pass + ";database=" + this.DBName;
		this.ConnectSQL=ConnectString;
		SqlConnection k = new SqlConnection(ConnectString);
		SqlDataAdapter Comm = new SqlDataAdapter(SQLString,k);
		DataSet DS = new DataSet();
		Comm.Fill(DS,TBName);
		NowDataSet=DS;
	}
	protected override void CreateChildControls(){
		DataTable NowData=null;
		if(NowDataSet==null){
			bindNew();//取得记录
		}
		NowData=NowDataSet.Tables[TBName];
		TotalCols=NowDataSet.Tables[TBName].Columns.Count;
		this.Controls.Clear();
		this.Controls.Add(new LiteralControl("<br>"));//开始加入控制项至网页
		if(NowMode=="Edit"){//若现为编辑模式
			for(int i=0;i<=TotalCols - 1;i++){
				TextBox tx = new TextBox();
				Label lb = new Label();
				tx.ID=NowData.Columns[i].ColumnName;
				tx.Text=NowData.Rows[NowRecord][i].ToString();
				string u=NowData.Rows[NowRecord][i].ToString();
				int p = u.Length + 5;
				if(p < 7){
					p = 7;
				}
				tx.Width = p * 10;
				lb.Text=NowData.Columns[i].ColumnName + "：";
				this.Controls.Add(lb);
				this.Controls.Add(tx);
				this.Controls.Add(new LiteralControl("<br>"));
			}
		}
		else if(NowMode=="View"){//若现为查看模式
			for(int i = 0;i<=TotalCols - 1;i++){
				Label txc = new Label();
				Label lb = new Label();
				txc.ID=NowData.Columns[i].ColumnName;
				txc.Text=NowData.Rows[NowRecord][i].ToString();
				string u=NowData.Rows[NowRecord][i].ToString();
				lb.Text=NowData.Columns[i].ColumnName + "：";
				this.Controls.Add(lb);
				this.Controls.Add(txc);
				this.Controls.Add(new LiteralControl("<br>"));
			}
		}
		DataKeyValue=NowData.Rows[NowRecord][DataKeyField].ToString();
		AllRecords=NowDataSet.Tables[TBName].Rows.Count;
	}
	protected override void OnPreRender(EventArgs e){
		DataTable NowData=null;
		if(NowDataSet==null){
			bindNew();
		}
		NowData=NowDataSet.Tables[TBName];
		TotalCols=NowDataSet.Tables[TBName].Columns.Count;
		this.Controls.Clear();
		this.Controls.Add(new LiteralControl("<br>"));
		if(NowMode=="Edit"){
			for(int i=0;i<=TotalCols - 1;i++){
				TextBox tx = new TextBox();
				Label lb = new Label();
				tx.ID=NowData.Columns[i].ColumnName;
				tx.Text=NowData.Rows[NowRecord][i].ToString();
				string u=NowData.Rows[NowRecord][i].ToString();
				int p = u.Length + 5;
				if(p < 7){
					p = 7;
				}
				tx.Width = p * 10;
				lb.Text=NowData.Columns[i].ColumnName + "：";
				this.Controls.Add(lb);
				this.Controls.Add(tx);
				this.Controls.Add(new LiteralControl("<br>"));
			}
		}
		else if(NowMode=="View"){
			for(int i = 0;i<=TotalCols - 1;i++){
				Label txc = new Label();
				Label lb = new Label();
				txc.ID=NowData.Columns[i].ColumnName;
				txc.Text=NowData.Rows[NowRecord][i].ToString();
				string u=NowData.Rows[NowRecord][i].ToString();
				lb.Text=NowData.Columns[i].ColumnName + "：";
				this.Controls.Add(lb);
				this.Controls.Add(txc);
				this.Controls.Add(new LiteralControl("<br>"));
			}
		}
		DataKeyValue=NowData.Rows[NowRecord][DataKeyField].ToString();
		AllRecords=NowDataSet.Tables[TBName].Rows.Count;
        }
}
}

