您的位置:首页 > 数据库

第七章 业务数据库的管理(四)-- 数据库客户端工具

2012-09-19 09:28 441 查看
7.3 添加业务表的数据库客户端工具

7.3.1 cjsjb.aspx.cs程序代码

......(续前页)

//"修改"按钮

private void Button4_Click(object sender, System.EventArgs e)

{

//fieldlists是字段列表,其成员还是ArrayList对象(字段各属性)

fieldlists=(ArrayList)Session["fieldlists"];

DisplayTable();

//获取输入的要修改的字段序号

String str1 = TextBox6.Text.Trim();

Regex reg =new Regex(@"^[0-9]*$");

int i;

if(reg.IsMatch(str1))

i=int.Parse(TextBox6.Text.Trim());

else

{

Label4.Visible=true;//提示字段序号输入错误

return;

}

//如果列表中已经没有字段则屏蔽修改按钮

if(fieldlists.Count<=0)

return;

//如果序号输入正确

if(i>=0 & i<fieldlists.Count)

{

//在保存修改前,禁止修改序号文本框,避免把修改内容保存到其它字段.

TextBox6.ReadOnly=true;

TextBox1.Text=((ArrayList)fieldlists[i])[0].ToString();

DropDownList1.SelectedValue=((ArrayList)fieldlists[i])[1].ToString();

TextBox2.Text=((ArrayList)fieldlists[i])[2].ToString();

TextBox3.Text=((ArrayList)fieldlists[i])[5].ToString();

//如果类型为整数等时不能编辑字段长度,除非重新选择其它类型.

string lx=((ArrayList)fieldlists[i])[1].ToString();

if (lx=="datetime" || lx=="int" || lx=="smallint" || lx=="bigint"

|| lx=="float" || lx=="decimal" )

TextBox2.ReadOnly=true;

if(((ArrayList)fieldlists[i])[3].ToString()=="是")

CheckBox1.Checked=true;//允许空

else

CheckBox1.Checked=false;

if(((ArrayList)fieldlists[i])[4].ToString()=="主键")

{

CheckBox2.Checked=true;

CheckBox2.Enabled=true;

}

else

{

CheckBox2.Checked=false;

}

//如果修改的是自增字段

if(((ArrayList)fieldlists[i])[6].ToString()=="自增")

{

CheckBox3.Checked=true;//自增

CheckBox3.Enabled=true;

CheckBox1.Checked=false;

CheckBox1.Enabled=false;

TextBox3.Enabled=false;//默认值

}

}

//如果序号输入错误,则显示提示信息

else

Label4.Visible=true;

Button1.Visible=false;//添加按钮

Button3.Visible=true;//修改按钮

}

//修改字段内容

private void Button3_Click(object sender, System.EventArgs e)

{

fieldlists=(ArrayList)Session["fieldlists"];

String str1 = TextBox6.Text.Trim();

Regex reg1 =new Regex(@"^[0-9]*$");

int i;

if(reg1.IsMatch(str1))

i=int.Parse(TextBox6.Text.Trim());//要修改的字段在字段列表中的序号

else

{

Label4.Visible=true;

DisplayTable();

return;

}

if(fieldlists.Count<=i)

{

Label4.Visible=true;

DisplayTable();

return;

}

//判断字段名称是否为空

if(TextBox1.Text.Trim()=="")

Label2.Visible=true;

//如果字段长度为空(整数等显示为默认长度,不可编辑),则显示错误信息

if(TextBox2.Text.Trim()=="")

Label3.Visible=true;

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;

}

}

}

//以上为验证部分,下面是把字段添加到列表的代码

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.RemoveAt(i);

fieldlists.Insert(i,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)";

//默认值

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.RemoveAt(i);

strfields.Insert(i,field);

Session["strfields"]=strfields;

//重新显示添加字段按钮

Button1.Visible=true;

Button3.Visible=false;

//保存修改后,重新激活修改序号文本框(与Button4_Click事件处理方法中内容对应).

TextBox6.ReadOnly=false;

//置空输入控件

TextBox1.Text="";

TextBox2.Text="";

TextBox3.Text="";

Textbox5.Text="";

//重新设置非空选择

if(CheckBox1.Checked==false)

CheckBox1.Checked=true;

//修改的字段为主键并保持为主键时,主键单选按钮会保持钩选并可用,会影响到添加字段

//(可能出现多主键情况,而这里sql语句设置只处理单个主键情况).

if(CheckBox2.Checked==true)//这里的很多设置都是测试后修改的.

{

CheckBox2.Checked=false;

CheckBox2.Enabled=false;

CheckBox1.Enabled=true;

}

//如果修改的字段被设置为自增

if(CheckBox3.Checked==true)

{

CheckBox3.Checked=false;

CheckBox3.Enabled=false;

CheckBox1.Enabled=true;

TextBox3.Enabled=true;

}

//重新设置默认数据类型为变长字符串

DropDownList1.SelectedValue="varchar";

}

//删除字段

private void Button5_Click(object sender, System.EventArgs e)

{

fieldlists=(ArrayList)Session["fieldlists"];

strfields=(ArrayList)Session["strfields"];

String str1 = TextBox6.Text.Trim();

Regex reg =new Regex(@"^[0-9]*$");

int i;

if(reg.IsMatch(str1))

i=int.Parse(TextBox6.Text.Trim());

else

{

Label4.Visible=true;

DisplayTable();

return;

}

//使用上一次的页面状态与本次页面状态相比,如果是刷新页面则状态值相同,不执行删除

//操作.实验证明该方法还是不太稳定.

string strdele=this.Request.Form["__VIEWSTATE"];

if( strdele ==(string)Session["myviewstate"])

{

DisplayTable();

Label4.Text="刚执行了刷新操作";

Label4.Visible=true;

return;

}

if(i>=0 & i<fieldlists.Count)

{

fieldlists.RemoveAt(i);

Session["fieldlists"]=fieldlists;

strfields.RemoveAt(i);

Session["strfields"]=strfields;

TextBox6.Text="999";//重新设置序号输入框

DisplayTable();

//如果已经删除了全部字段就要屏蔽修改按钮和显示添加按钮

if(fieldlists.Count<=0)

{

Button1.Visible=true;

Button3.Visible=false;

}

}

else

{

Label4.Visible=true;

DisplayTable();

}

//使用会话变量存储提交页面的页面状态

Session["myviewstate"]=this.Request.Form["__VIEWSTATE"];

}

//保存数据表

//要添加错误信息处理代码

private void Button2_Click(object sender, System.EventArgs e)

{

strfields=(ArrayList)Session["strfields"];

string sqlstr="create table "+TextBox4.Text.Trim()+"(";

int i;

for(i=0;i<strfields.Count-1;i++)

{

sqlstr=sqlstr+strfields[i].ToString().Trim()+",";

}

sqlstr=sqlstr+strfields[i].ToString().Trim()+")";

Base basesql=new Base();

if(basesql.SQLExeNonQuery(sqlstr) == true)

{

Session["fieldlists"]=null;

Session["strfields"]=null;

//this.RegisterStartupScript("alert","<script>alert('数据表创建成

//功!');</script>");

//上一行提示成功的信息,由于下面的页面重定向而起不到提示的作用;改为根据重定向

//时传递的参数在pageload中加载提示信息框的显示.

//页面重定向到自身

Response.Redirect( Request.Url.ToString( ) +"?success=true");

}

else

{

fieldlists=(ArrayList)Session["fieldlists"];

DisplayTable();

this.RegisterStartupScript("alert","<script>alert('保存表"

+TextBox4.Text.Trim()+"失败!');</script>");

Response.Write("<FONT color=#ff0066>"+basesql.BaseSqlErrDes+"</FONT>");

}

}

//显示字段列表

private void DisplayTable()

{

TableRow tr=new TableRow();

tr.BackColor=ColorTranslator.FromHtml("#eeeeee");

tr.HorizontalAlign=HorizontalAlign.Center;

TableCell tc1=new TableCell();

tc1.Text="序号";

tr.Cells.Add(tc1);

TableCell tc2=new TableCell();

tc2.Text="字段名";

tr.Cells.Add(tc2);

TableCell tc3=new TableCell();

tc3.Text="数据类型";

tr.Cells.Add(tc3);

TableCell tc4=new TableCell();

tc4.Text="长度";

tr.Cells.Add(tc4);

TableCell tc5=new TableCell();

tc5.Text="是否允许空值";

tr.Cells.Add(tc5);

TableCell tc6=new TableCell();

tc6.Text="字段约束";

tr.Cells.Add(tc6);

TableCell tc7=new TableCell();

tc7.Text="默认值";

tr.Cells.Add(tc7);

TableCell tc8=new TableCell();

tc8.Text="标识";

tr.Cells.Add(tc8);

Tbl.Rows.Add(tr);

if(fieldlists.Count>0)

{

ArrayList fieldvalues=new ArrayList();

for(int i=0;i<fieldlists.Count;i++)

{

fieldvalues=(ArrayList)fieldlists[i];

TableRow tr1=new TableRow();

tr1.HorizontalAlign=HorizontalAlign.Center;

TableCell tc9=new TableCell();

tc9.Text=i.ToString();

tr1.Cells.Add(tc9);

for(int j=0;j<fieldvalues.Count;j++)

{

TableCell tc=new TableCell();

tc.Text=fieldvalues[j].ToString();

tr1.Cells.Add(tc);

}

Tbl.Rows.Add(tr1);

}

}

}

//如果选择了自增就不能再设置默认值,不能为空.

private void CheckBox3_CheckedChanged(object sender, System.EventArgs e)

{

if(CheckBox3.Checked==true)

{

TextBox3.Text="";

TextBox3.Enabled=false;

CheckBox1.Checked=false;

CheckBox1.Enabled=false;

}

else

{

TextBox3.Enabled=true;

if(CheckBox1.Enabled==false)

{

if(! CheckBox2.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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: