7.3 添加业务表的数据库客户端工具
7.3.1 cjsjb.aspx.cs程序代码
业务数据表定义工具是一个ASP.NET表单应用程序,可以完成字段名称编辑、字段数据类型选择、字段默认值设置、主键设置、标识字段设置等工作。有了业务数据表定义工具,用户不需安装数据库管理客户端工具,直接通过浏览器就可以完成数据表定义所需要的大部分工作。
......
using System.Text.RegularExpressions;
using DataAccess;
namespace workflow.admin
{
public class cjsjb : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.CheckBox CheckBox2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.TextBox Textbox5;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.RegularExpressionValidator
RegularExpressionValidator1;
protected System.Web.UI.WebControls.RegularExpressionValidator
RegularExpressionValidator2;
//字段列表,成员还是ArrayList(字段各属性)
protected ArrayList fieldlists=new ArrayList();
protected ArrayList strfields=new ArrayList();
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Button Button4;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.CheckBox CheckBox3;
protected System.Web.UI.WebControls.Table Tbl;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)//第一次加载页面时被访问的代码
{
//验证用户是否登录
if(Session["userid"] == null)
Response.Redirect("./Message.aspx");
//使用会话变量解决页面重载时变量保存问题,也可以使用ViewState变量
Session["fieldlists"]=fieldlists;
Session["strfields"]=strfields;
//保存页面的视图状态,这里初始化为空,后面的删除字段方法中利用页面的当前视图状态与
//会话变量中存放的页面的上一个视图状态相比较,防止页面刷新引起字段删除.
Session["myviewstate"]="";
//数据表保存成功并重定向回自身时给予提示
if(Request.QueryString["success"]=="true")
{
this.RegisterStartupScript("alert",
"<script>alert('数据表创建成功!');</script>");
}
}
//下面可以添加每次页面加载都被执行的代码
//去掉页面中的非空提示信息(由于每次提交页面都先执行,既不影响随后的添加按钮非空验证
//信息正常显示,又能在点击其它按钮时不显示提示信息).
Label2.Text="";
Label3.Text="";
//与上述原因相同,恢复在数据类型选择控件事件处理中设置的下列控件属性
TextBox2.ReadOnly=false;//对整型字段等来说,字段长度不可编辑
Label4.Visible=false;//输入要修改字段的正确序号提示
//执行删除操作后如果进行刷新则提示信息改变,这里恢复提示信息默认值.
Label4.Text="请输入正确的数字序号";
Label5.Text="";//默认值提示
TextBox6.ReadOnly=false;//输入要修改字段序号的文本框,正在修改字段时不可编辑
//在客户端确认删除
Button5.Attributes.Add("onclick","javascript:if(confirm('请确认是否删除!')
==false) return false;");
}
//根据选择的数据类型设置字段长度
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//字符串则要求设置字段长度,而小数则设置精度和小数位设置等
string lx=DropDownList1.SelectedItem.Value;
if (lx=="datetime")
{
TextBox2.Text="8";//字段长度
TextBox2.ReadOnly=true;
}
if(lx=="int")
{
TextBox2.Text="4";
TextBox2.ReadOnly=true;
}
if(lx=="smallint")
{
TextBox2.Text="2";
TextBox2.ReadOnly=true;
}
if(lx=="bigint")
{
TextBox2.Text="8";
TextBox2.ReadOnly=true;
}
if(lx=="float")
{
TextBox2.Text="8";
TextBox2.ReadOnly=true;
}
if(lx=="decimal")
{
TextBox2.Text="9";
TextBox2.ReadOnly=true;
}
//如果不是整型自增字段
if(!((lx=="smallint" || lx=="int" || lx=="bigint") & CheckBox3.Checked))
{
TextBox3.Enabled=true;//可以设置默认值
//如果不是主键
if(CheckBox2.Checked !=true)
CheckBox1.Enabled=true;//允许字段值为空
//如果不是整型而自增单选框被选中。这个判断应该是多余的,因为不是整型的话自增单
//选框不显示
if(CheckBox3.Checked)
CheckBox3.Checked=false;
}
fieldlists=(ArrayList)Session["fieldlists"];//字段列表
//由于该事件要重新加载页面,而pageload中不处理显示字段列表,所以要自行调用列表显示.
DisplayTable();
}
//设置主键后不能再设置空值
private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
{
if(CheckBox2.Checked==true)
{
CheckBox1.Checked=false;
CheckBox1.Enabled=false;
}
if(CheckBox2.Checked==false)
{
//如果不是自增字段
if(! CheckBox3.Checked)
{
CheckBox1.Enabled=true;
}
}
string lx=DropDownList1.SelectedItem.Value;
if (lx=="datetime" || lx=="int" || lx=="smallint" || lx=="bigint" || lx=="float"
|| lx=="decimal" )
TextBox2.ReadOnly=true;//字段长度不可编辑
fieldlists=(ArrayList)Session["fieldlists"];//不用重新赋值,直接使用会话变量也可
DisplayTable();
}
//添加字段到列表
private void Button1_Click(object sender, System.EventArgs e)
{
fieldlists=(ArrayList)Session["fieldlists"];
if(TextBox1.Text.Trim()=="" || TextBox2.Text.Trim()=="")
{
if(TextBox1.Text.Trim()=="")
Label2.Text="字段名称不能为空";
if(TextBox2.Text.Trim()=="")
Label3.Text="字段长度不能为空";
DisplayTable();
return;
}
//如果默认值不为空,验证默认值是否合理.
string lx=DropDownList1.SelectedItem.Value;
string mrz= TextBox3.Text.Trim();
if(mrz !="")
{
if(lx=="int" || lx=="bigint" || lx=="smallint")
{
Regex reg =new Regex(@"^[0-9]*$");
if( !reg.IsMatch(mrz))
{
Label5.Text="请输入整数";
DisplayTable();
return;
}
}
if(lx=="decimal" || lx=="float")
{
Regex reg =new Regex(@"^[0-9]*[.]?[0-9]*$");
if( !reg.IsMatch(mrz))
{
Label5.Text="请输入整数或小数";
DisplayTable();
return;
}
}
if(lx=="datetime")
{
//“\”这是引用符,例如\$被用来匹配美元符号,\.用来匹配点字符;
//“|”表示或运算。
Regex reg =new Regex(@"^\d{1,4}(\-?)|(\/?)\d{1,2}(\-?)|(\/?)\d{1,2}$");
if( !reg.IsMatch(mrz))
{
Label5.Text="请输入类似1900-1-1日期";
DateTime t=DateTime.Now;
string strdate=t.ToShortDateString();
TextBox3.Text=strdate;
DisplayTable();
return;
}
}
}
//解决刷新时添加重复字段问题
if(fieldlists.Count>0)
{
for(int i=0;i<fieldlists.Count;i++)
{
//测试后添加的判断语句,如果字段列表中已经存在主键,不能再重复添加主键.
if(((ArrayList)fieldlists[i])[4].ToString().Trim()=="主键")
{
if(CheckBox2.Checked)
{
CheckBox2.Checked=false;
CheckBox2.Enabled=false;
CheckBox1.Enabled=true;//允许空
Label2.Text="不能重复添加主键";
return;
}
}
if(((ArrayList)fieldlists[i])[6].ToString().Trim()=="自增")
{
if((lx=="smallint" || lx=="int" || lx=="bigint") & CheckBox3.Checked)
{
CheckBox3.Checked=false;
TextBox3.Enabled=true;//默认值
if(!CheckBox2.Checked)
CheckBox1.Enabled=true;//允许空
Label2.Text="不能重复设置自增字段";
return;
}
}
//如果列表中已存在该字段名则不添加
if(TextBox1.Text.Trim() ==
((ArrayList)fieldlists[i])[0].ToString().Trim())
{
DisplayTable();
Label2.Text="字段名称不能重复";
return;
}
}
}
//将部分字段设置控件的值保存,用于显示.
ArrayList ctrvalues=new ArrayList();
ctrvalues.Add(TextBox1.Text);
ctrvalues.Add(DropDownList1.SelectedItem.Value);
ctrvalues.Add(TextBox2.Text);
if(CheckBox1.Checked==true)//允许空
ctrvalues.Add("是");
else
ctrvalues.Add("否");
if(CheckBox2.Checked==true)
ctrvalues.Add("主键");
else
ctrvalues.Add("");
ctrvalues.Add(TextBox3.Text);
if((lx=="smallint" || lx=="int" || lx=="bigint") & CheckBox3.Checked)
ctrvalues.Add("自增");
else
ctrvalues.Add("");
//把字段部分值保存到列表中用于显示字段列表
fieldlists.Add(ctrvalues);
//显示字段列表
DisplayTable();
//由于变量的生命周期只能在一次客户端与服务器交互中保持,所以要用会话变量保持字段列
//表变量.
Session["fieldlists"]=fieldlists;
//定义添加字段的sql子句字符串
string field="";
//小数
if(lx == "decimal")
{
field=TextBox1.Text.Trim()+" "+DropDownList1.SelectedItem.Value
+"("+TextBox2.Text.Trim()+","+Textbox5.Text.Trim()+")";
}
//非小数
else if(lx=="int" || lx=="bigint" || lx=="smallint" || lx=="float"
|| lx=="datetime")
field=TextBox1.Text.Trim()+" "+DropDownList1.SelectedItem.Value;
else
{
field=TextBox1.Text.Trim()
+" "+DropDownList1.SelectedItem.Value+"("+TextBox2.Text.Trim()+")";
}
//整数自增判断
if((lx=="int" || lx=="bigint" || lx=="smallint") & (CheckBox3.Checked))
{
field=field+" IDENTITY (1, 1)";
//设置一个自增字段后,其它字段不能再设置自增
CheckBox3.Checked=false;
CheckBox3.Enabled=false;
//重新起用空值设置
if(! CheckBox2.Checked)
CheckBox1.Enabled=true;
}
//默认值
if(TextBox3.Text.Trim() !="")
{
field=field+" DEFAULT('"+TextBox3.Text.Trim()+"')";
}
//主键
if(CheckBox2.Checked==true)
{
field=field+" NOT NULL primary key";
//设置一个主键后,其它字段不能再设置主键.这里是考虑只设置一个主键的情况.
CheckBox2.Checked=false;
CheckBox2.Enabled=false;
//重新起用空值设置
CheckBox1.Checked=true;
CheckBox1.Enabled=true;
}
else
{
//可以是空值
if(CheckBox1.Checked==true)
{
field=field+" NULL";
}
else
{
field=field+" NOT NULL";
}
}
//把添加字段的sql子句添加到列表对象中
strfields=(ArrayList)Session["strfields"];
strfields.Add(field);
Session["strfields"]=strfields;
//调试用
//置空输入控件
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
//重新使能默认值控件
TextBox3.Enabled=true;
Textbox5.Text="";
//重新设置非空选择
if(CheckBox1.Checked==false)
CheckBox1.Checked=true;
CheckBox1.Enabled=true;
//重新设置默认数据类型为变长字符串
DropDownList1.SelectedValue="varchar";
//显示保存数据表控件
Label1.Visible=true;
TextBox4.Visible=true;
Button2.Visible=true;
//显示修改按钮
TextBox6.Visible=true;
Button4.Visible=true;
Button5.Visible=true;
//重新设置自增选择为否
if(CheckBox3.Checked==true)
CheckBox3.Checked=false;
}
......(待续)