Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Imports System.Drawing
namespace DataCon_vb
public class SqlData_vb: Inherits Control : Implements INamingContainer
	public TotalCols as integer
	public Property SQLString() as string'目前SQL语法
		get
			return ViewState("SQLString").ToString()
		end get
		set
			ViewState("SQLString")=value
		end set
	end property
	public Property ConnectSQL() as string'目前连至SQL的字串
		get
			return ViewState("ConnectSQL").ToString()
		end get
		set
			ViewState("ConnectSQL")=value
		end set
	end property
	public Property ServerName() as string 'SQL服务器名称
		get
			return ViewState("ServerName").ToString()
		end get
		set
			ViewState("ServerName")=value
		end set
	end property
	public Property Uid() as string'登录SQL的ID
		get
			return ViewState("Uid").ToString()
		end get
		set
			ViewState("Uid")=value
		end set
	end property
	public Property Pass() as string'登录SQL的密码
		get
			return ViewState("Pass").ToString()
		end get
		set
			ViewState("Pass")=value
		end set
	end property
	public Property DBName() as string' 数据库名称
		get
			return ViewState("DBName").ToString()
		end get
		set
			ViewState("DBName")=value
		end set
	end property
	public Property TBName() as string'DataTable名称
		get
			return ViewState("TBName").ToString()
		end get
		set
			ViewState("TBName")=value
		end set
	end property
	public Property NowRecord() as integer'目前个数
		get
			return Convert.ToInt32(ViewState("NowRecord"))
		end get
		set
			ViewState("NowRecord")=Convert.ToInt32(value)
		end set
	end property
	public Property AllRecords() as Integer'总条数
		get
			return Convert.ToInt32(ViewState("AllRecords"))
		end get
		set
			ViewState("AllRecords")=Convert.ToInt32(value)
		end set
	end property
	public Property NowDataSet() as dataset'目前的Dataset
		get
			return Ctype(ViewState("NowDataSet"),DataSet)
		end get
		set
			ViewState("NowDataSet")=Ctype(value,DataSet)
		end set
	end property
	public Property NowMode() as string'目前模式
		get
			return ViewState("NowMode").ToString()
		end get
		set
			ViewState("NowMode")=value
		end set
	end property
	public property DataKeyValue() as string'目前主键字段值
		get
			return ViewState("DataKeyValue").ToString()
		end get
		set
			ViewState("DataKeyValue")=value
		end set
	end property
	public property DataKeyField() as string'主键字段
		get
			return ViewState("DataKeyField").ToString()
		end get
		set
			ViewState("DataKeyField")=value
		end set
	end property
	public Function Save() as Boolean
		Dim k as SqlConnection = new SqlConnection(ConnectSQL)
		Dim Comm as SqlDataAdapter = new SqlDataAdapter(SQLString,k)
		Dim DS as DataSet = new DataSet()
		Comm.Fill(DS,TBName)
		try
			Dim i as integer=0
			for i=0 to DS.Tables(TBName).Columns.Count-1
				Dim CN as string = DS.Tables(TBName).Columns(i).ColumnName
				Dim tx as TextBox = Ctype(FindControl(CN),TextBox)
				if tx.Text <> "" then DS.Tables(TBName).Rows(NowRecord)(CN)=tx.Text
			next
			Dim custCB as SqlCommandBuilder = new SqlCommandBuilder(Comm)
			Comm.Update(DS,TBName)
			Me.NowMode="View"
			bindNew()
			return true
		catch
			return false'保存时发生错误
		end try
	end function
	public sub Delete(DeleteString as string)
		dim ConnectString as string ="server=" & Me.ServerName & ";uid=" & Me.Uid & ";pwd=" & Me.Pass & ";database=" & Me.DBName
		Me.ConnectSQL=ConnectString
		Dim k as SqlConnection = new SqlConnection(ConnectString)
		Dim DelRec as SqlCommand = new SqlCommand(DeleteString,k)
		DelRec.Connection.Open()
		DelRec.ExecuteNonQuery()
		DelRec.Connection.Close()
		AllRecords-=1
		if NowRecord>0 then NowRecord-=1
	end sub
	private sub bindNew()
		Dim ConnectString as string ="server=" & Me.ServerName & ";uid=" & Me.Uid & ";pwd=" & Me.Pass & ";database=" & Me.DBName
		Me.ConnectSQL=ConnectString
		Dim k as SqlConnection = new SqlConnection(ConnectString)
		Dim Comm as SqlDataAdapter = new SqlDataAdapter(SQLString,k)
		Dim DS as DataSet = new DataSet()
		Comm.Fill(DS,TBName)
		NowDataSet=DS
	end sub
	protected overrides sub CreateChildControls()
		Dim NowData as DataTable =nothing
'		if NowDataSet is nothing then
			bindNew()'取得记录
'		end if
		NowData=NowDataSet.Tables(TBName)
		TotalCols=NowDataSet.Tables(TBName).Columns.Count
		Me.Controls.Clear()
		Me.Controls.Add(new LiteralControl("<br>"))'开始加入控制项至网页
		if NowMode="Edit" then'若现为编辑模式
			dim i as integer =0 
			for i=0 to TotalCols - 1
				dim tx as TextBox = new TextBox()
				dim lb as Label = new Label()
				tx.ID=NowData.Columns(i).ColumnName
				tx.Text=NowData.Rows(NowRecord)(i).ToString()
				Dim u as string =NowData.Rows(NowRecord)(i).ToString()
				'dim m as unit
				'dim p as integer = u.Length + 5
				'if p < 7 then
			'	'	p = 7
				'end if
				'm.Value=p*10
				'tx.Width = m
				lb.Text=NowData.Columns(i).ColumnName & "："
				Me.Controls.Add(lb)
				Me.Controls.Add(tx)
				Me.Controls.Add(new LiteralControl("<br>"))
			Next
		elseif NowMode="View" then'若现为查看模式
			dim i as integer=0
			for i = 0 to TotalCols - 1
				dim txc as Label = new Label()
				dim lb as Label = new Label()
				txc.ID=NowData.Columns(i).ColumnName
				txc.Text=NowData.Rows(NowRecord)(i).ToString()
				dim u as string =NowData.Rows(NowRecord)(i).ToString()
				lb.Text=NowData.Columns(i).ColumnName + "："
				Me.Controls.Add(lb)
				Me.Controls.Add(txc)
				Me.Controls.Add(new LiteralControl("<br>"))
			next
		end if
		DataKeyValue=NowData.Rows(NowRecord)(DataKeyField).ToString()
		AllRecords=NowDataSet.Tables(TBName).Rows.Count
	end sub
	protected overrides sub OnPreRender(e as EventArgs)
		Dim NowData as DataTable =nothing
		if NowDataSet is nothing then
			bindNew()'取得记录
		end if
		NowData=NowDataSet.Tables(TBName)
		TotalCols=NowDataSet.Tables(TBName).Columns.Count
		Me.Controls.Clear()
		Me.Controls.Add(new LiteralControl("<br>"))'开始加入控制项至网页
		if NowMode="Edit" then'若现为编辑模式
			dim i as integer =0 
			for i=0 to TotalCols - 1
				dim tx as TextBox = new TextBox()
				dim lb as Label = new Label()
				tx.ID=NowData.Columns(i).ColumnName
				tx.Text=NowData.Rows(NowRecord)(i).ToString()
				dim u as string=NowData.Rows(NowRecord)(i).ToString()
				dim p as integer = u.Length + 5
				'if p < 7 then
				'	p = 7
				'end if
				'tx.Width = p * 10
				lb.Text=NowData.Columns(i).ColumnName & "："
				Me.Controls.Add(lb)
				Me.Controls.Add(tx)
				Me.Controls.Add(new LiteralControl("<br>"))
			next
		elseif NowMode="View" then'若现为查看模式
			dim i as integer=0
			for i = 0 to TotalCols - 1
				dim txc as Label = new Label()
				dim lb as Label = new Label()
				txc.ID=NowData.Columns(i).ColumnName
				txc.Text=NowData.Rows(NowRecord)(i).ToString()
				Dim u as string=NowData.Rows(NowRecord)(i).ToString()
				lb.Text=NowData.Columns(i).ColumnName + "："
				Me.Controls.Add(lb)
				Me.Controls.Add(txc)
				Me.Controls.Add(new LiteralControl("<br>"))
			next
		end if
		DataKeyValue=NowData.Rows(NowRecord)(DataKeyField).ToString()
		AllRecords=NowDataSet.Tables(TBName).Rows.Count
        end sub
end class
end Namespace

