Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
public partial class OrderModule_E_OrderCustomize : System.Web.UI.Page
{
    
/// 添加新字段按钮!
    
/// <summary>
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Button1_Click1(object sender, EventArgs e)
    {
        
string strName = TextBox1.Text.Trim();///字段名称                        
        string strType = TransDBtype(DropDownList1.SelectedItem.Text, 0);///字段类型

        
string iLength = DropDownList1.SelectedItem.Text;///字段长度
        string strOtherName = TextBox5.Text.Trim();
        
if (strOtherName.Length == 0)
        {
            strOtherName 
= strName;
        }
        
if (DropDownList1.SelectedIndex == 4)///如果是字符型的情况

        {
            
if (TextBox2.Text.Trim() == "")
            {
                iLength 
= "50";///默认长度50个字节

            }
            
else
            {
                iLength 
= TextBox2.Text.Trim();
            }
            strType 
= strType + "(" +iLength + ")";
        }
        
        
if (strName.Length == 0)
        {
            Operation.Alert(
"字段名称不能为空!",this.Page );
            
return;
        }
        
for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            
if (strName == GridView1.Rows[i].Cells[0].Text)
            {
                Operation.Alert(
"字段名称【"+strName +"】已经存在!"this.Page);
                
return;
            }
        }

        
string strsql = "ALTER table T_ProductOrder  ADD " + strName + "  " + strType + " null";
        AccessDB objDB 
= new AccessDB("Crunode_EntireSpaceTime");
        
if (objDB.excNonQuery(strsql))
        {
            Operation.Alert(
"字段添加成功!"this.Page);

            strsql 
= "insert into T_OrderSchema values(";
            strsql 
+= "'" + strName + "',";
            strsql 
+= "'" + DropDownList1.SelectedItem.Text + "',";
            strsql 
+= "'" + GetLenth(iLength) + "',";
            strsql 
+= "0,";
            strsql 
+= "0,";
            strsql
+="'"+strOtherName+"')";

            objDB.excNonQuery(strsql);
            getTabSchema();
        }
        
else
        {
            Operation.Alert(
"字段添加失败!"this.Page);
            
return;
        }

    
    }


    
/// 根据类型获取该类型的长度!

    
/// <summary>
    
/// </summary>
    
/// <param name="strType"></param>
    
/// <returns></returns>
    private string GetLenth(string strType)
    {
        
string strlg = strType;
        
switch (strType)
        {
            
case "整数类型":
                strlg 
= "4";
                
break;
            
case "时间类型":
                strlg 
= "8";
                
break;
            
case "小数类型":
                strlg 
= "8";
                
break;
            
case "货币类型":
                strlg 
= "8";
                
break;
        }
        
return strlg;
    }


    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
           
//初始化页面控件! 
            initpage();
            
//获取表字段结构!
            getTabSchema();
        }
    }


    
/// 初始化页面控件!
    
/// <summary>
    
/// </summary>
    private void initpage()
    {
        TextBox1.Text 
= "";
        TextBox2.Text 
= "";
        DropDownList1.Items.Insert(
0new ListItem("整数类型""整数类型"));
        DropDownList1.Items.Insert(
1new ListItem("时间类型""时间类型"));
        DropDownList1.Items.Insert(
2new ListItem("小数类型""小数类型"));
        DropDownList1.Items.Insert(
3new ListItem("货币类型""货币类型"));
        DropDownList1.Items.Insert(
4new ListItem("文本类型""文本类型"));
        DropDownList1.SelectedIndex 
= 4;
        
//如果不是文本类行的话 不显示字段长度

        
if (DropDownList1.SelectedIndex == 4)
        {
            Label1.Visible 
= true;
            TextBox2.Visible 
= true;
        }

        TextBox3.Text 
= "";
        TextBox4.Text 
= "";
        DropDownList2.Items.Insert(
0new ListItem("整数类型""整数类型"));
        DropDownList2.Items.Insert(
1new ListItem("时间类型""时间类型"));
        DropDownList2.Items.Insert(
2new ListItem("小数类型""小数类型"));
        DropDownList2.Items.Insert(
3new ListItem("货币类型""货币类型"));
        DropDownList2.Items.Insert(
4new ListItem("文本类型""文本类型"));
        DropDownList2.SelectedIndex 
= 4;
        
//如果不是文本类行的话 不显示字段长度

        
if (DropDownList2.SelectedIndex == 4)
        {
            Label2.Visible 
= true;
            TextBox4.Visible 
= true;
        }
    }


    
/// 获取已有的结构,绑定gridview!

    
/// <summary>
    
/// </summary>
    private void getTabSchema()
    {
        AccessDB objDb 
= new AccessDB("Crunode_EntireSpaceTime");
        
///预设所有订单表名称为 T_ProductOrder
        string strsql = "select * from T_OrderSchema "
        
///设置gridview显示结构!

        
//DataTable dt_New = new DataTable();
        
//dt_New.Columns.Add("字段名称", Type.GetType("System.String"));
        
//dt_New.Columns.Add("字段类型", Type.GetType("System.String"));
        
//dt_New.Columns.Add("字段长度", Type.GetType("System.String"));
        
//dt_New.Columns.Add("fld_IsSelected", Type.GetType("System.String"));
        
//dt_New.Columns.Add("fld_IsPreinstall", Type.GetType("System.String"));
        
//获取结构
        DataTable datatable = objDb.GetDataTable(strsql);
        
if (datatable != null)
        {
            
///定义一个二位数组!
            //string[] temp = new string[5];
            ////DataRow drNew = new DataRow();
            //int i = 0;
            
//foreach (DataRow dr in datatable.Rows)
            
//{
            
//    DataRow drNew = dt_New.NewRow();
            
//    temp= new string[] {    
            
//                            dr["fld_Name"].ToString(),//字段名称
            
//                            TransDBtype(dr["fld_Type"].ToString(), 1),//字段类型
            
//                            dr["fld_FieldSize"].ToString(),//字段大小
            
//                            dr["fld_IsSelected"].ToString(),
            
//                            dr["fld_IsPreinstall"].ToString()
            
//                        };
            
//    drNew.ItemArray = temp;
            
//    dt_New.Rows.Add(drNew);
            
//}
            GridView1.DataSource = null;
            GridView1.DataSource 
= datatable;
            GridView1.DataBind();
        }
    }


    
/// 选则数据类型,如果是文本类型的话 显示字段长度
    
/// <summary>
    
/// </summary>
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
if (DropDownList1.SelectedIndex == 4)
        {
            TextBox2.Visible 
= true; Label1.Visible = true;
        }
        
else
        {
            TextBox2.Visible 
= false; Label1.Visible = false;

        }
    }


    
/// 翻译数据库类型 ii=0 从界面上的类型到数据库类型 ii=1反之
    
/// <summary>
    
/// </summary>
    private string TransDBtype(string strtype, int ii)
    {
        
string sttype = "";
        
if (ii == 0)//从界面上的类型到数据库类型

        {
            
switch (strtype)
            {
                
case "整数类型":
                    sttype 
= "int";
                    
break;

                
case "时间类型":
                    sttype 
= "datetime";
                    
break;
                
case "小数类型":
                    sttype 
= "float";
                    
break;
                
case "货币类型":
                    sttype 
= "money";
                    
break;
                
case "文本类型":
                    sttype 
= "varchar";
                    
break;
            }
        }
        
else//从数据库类型到界面上的类型

        {
            
switch (strtype)
            {
                
case "int":
                    sttype 
= "整数类型";
                    
break;
                
case "datetime":
                    sttype 
= "时间类型";
                    
break;
                
case "float":
                    sttype 
= "小数类型";
                    
break;
                
case "money":
                    sttype 
= "货币类型";
                    
break;
                
case "varchar":
                    sttype 
= "文本类型";
                    
break;
                
default:
                    sttype 
= strtype;
                    
break;
            }
        }
        
return sttype;
    }


    
/// 编辑
    
/// <summary>
   
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton linkB 
= (LinkButton)sender;
        DataControlFieldCell d 
= (DataControlFieldCell)linkB.Parent;
        GridViewRow r 
= (GridViewRow)d.Parent;
        TextBox6.Text 
= r.Cells[1].Text;
        TextBox3.Text 
= r.Cells[0].Text;///名称
        DropDownList2.SelectedValue = TransDBtype(r.Cells[2].Text,1);
        
if (DropDownList2.SelectedIndex == 4)
        {
            Label2.Visible 
= true;
            TextBox4.Text 
= r.Cells[3].Text;

            
        }
        
else
        {
            Label2.Visible 
= false;
            TextBox4.Visible 
= false;
        }
        ViewState[
"OrderID"= ((Label)r.FindControl("Label3")).Text;
        ViewState[
"FieldName"]=r.Cells[0].Text;
    }


    
/// 删除
    
/// <summary>
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        LinkButton linkB 
= (LinkButton)sender;
        DataControlFieldCell d 
= (DataControlFieldCell)linkB.Parent;
        GridViewRow r 
= (GridViewRow)d.Parent;
        
string strName = r.Cells[0].Text;
        
string  strsql = "ALTER TABLE T_ProductOrder DROP  COLUMN " + strName;
        AccessDB objdb 
= new AccessDB("Crunode_EntireSpaceTime");
        
if (objdb.excNonQuery(strsql))
        {
            
            
if (ViewState["FieldName"!= null)
            {
                
if (strName == ViewState["FieldName"].ToString())///如果删除的字段是曾经编辑时选中的字段,
                
///编辑部分重新初始化

                {
                    ViewState[
"oldName"= null;
                    TextBox3.Text 
= "";
                    TextBox4.Text 
= "";
                    DropDownList2.Items.Insert(
0new ListItem("整数类型""整数类型"));
                    DropDownList2.Items.Insert(
1new ListItem("时间类型""时间类型"));
                    DropDownList2.Items.Insert(
2new ListItem("小数类型""小数类型"));
                    DropDownList2.Items.Insert(
3new ListItem("货币类型""货币类型"));
                    DropDownList2.Items.Insert(
4new ListItem("文本类型""文本类型"));
                    DropDownList2.SelectedIndex 
= 4;
                    
//如果不是文本类行的话 不显示字段长度

                    
if (DropDownList2.SelectedIndex == 4)
                    {
                        Label2.Visible 
= true;
                        TextBox4.Visible 
= true;
                    }

                }
            }

            strsql 
= "delete from T_OrderSchema where fld_ID =" + ((Label)r.FindControl("Label3")).Text;
            objdb.excNonQuery(strsql);
            getTabSchema();Operation.Alert(
"删除成功!"this.Page);
        }
        
else
        {
            Operation.Alert(
"删除失败!"this.Page);
        }
    }


    
///gridview行绑定

    
/// <summary>
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
if (e.Row.RowIndex != -1)
        {
            
if (((Label)e.Row.FindControl("Label1")).Text.Equals("True"))
            {
                ((LinkButton)e.Row.FindControl(
"LinkButton1")).Enabled = false;
                ((LinkButton)e.Row.FindControl(
"LinkButton2")).Enabled = false;
            }
            
else
            {
                ((LinkButton)e.Row.FindControl(
"LinkButton1")).Enabled = true;
                ((LinkButton)e.Row.FindControl(
"LinkButton2")).Enabled = true;
            }
            e.Row.Cells[
2].Text = TransDBtype(e.Row.Cells[2].Text,1);
        }
    }


    
/// 换行
    
/// <summary>
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex 
= e.NewPageIndex;
        getTabSchema();
    }


    
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        
    }


    
/// 选则修改数据类型,如果是文本类型的话 显示字段长度
    
/// <summary>
    
/// </summary>
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        
if (DropDownList2.SelectedIndex == 4)
        {
            TextBox4.Visible 
= true; Label2.Visible = true;
        }
        
else
        {
            TextBox4.Visible 
= false; Label2.Visible = false;

        }
    }


    
/// 修改按钮!

    
/// <summary>
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {

        
if(ViewState["OrderID"]!=null&& ViewState ["FieldName"]!=null  )        
        {
            
string strName = TextBox3.Text.Trim();///字段名称                        
            string strType = TransDBtype(DropDownList2.SelectedItem.Text, 0);///字段类型
            string strOldName = ViewState["FieldName"].ToString();
            
string iLength = DropDownList2.SelectedItem.Text;///字段长度
            string strOtherName = TextBox6.Text.Trim() ;
            
if(strOtherName.Length ==0)
            {
                strOtherName
=strName ;
            }
            
if (DropDownList2.SelectedIndex == 4)///如果是字符型的情况

            {
                
if (TextBox4.Text.Trim() == "")
                {
                    iLength 
= "50";///默认长度50个字节

                }
                
else
                {
                    iLength 
= TextBox4.Text.Trim();
                }
                strType 
= strType + "(" + iLength + ")";
            }

            
if (strName.Length == 0)
            {
                Operation.Alert(
"字段名称不能为空!"this.Page);
                
return;
            }
            
for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                
if (strName == GridView1.Rows[i].Cells[0].Text)
                {

                    
if (((Label)GridView1.Rows[i].FindControl("Label3")).Text != ViewState["OrderID"].ToString())
                    {
                        Operation.Alert(
"字段名称【" + strName + "】已经存在!"this.Page);
                        
return;
                    }
                }
            }
            
///重命名!
            
///

                AccessDB objdb 
= new AccessDB("Crunode_EntireSpaceTime");
                
string strsql = "";
                
if (strOldName != strName)
                {
                    strsql 
= " EXEC sp_rename 'T_ProductOrder." + strOldName + " ', '" + strName + "'";
                    
if (!objdb.excNonQuery(strsql))
                    {
                        Operation.Alert(
"重命名失败!"this.Page);
                        
return;
                    }                   
                }
                ViewState[
"FieldName"= strName;///重命名之后 保存新名称!
                
///修改
                strsql = "  alter   table   T_ProductOrder    alter column " + strName + "  " + strType;
                
if (!objdb.excNonQuery(strsql))
                {
                    Operation.Alert(
"修改失败!"this.Page);
                    
return;
                }
                
else
                {
                    Operation.Alert(
"修改成功!"this.Page);
                    
///更新T_OrderSchema表

                    strsql 
= "update T_OrderSchema set fld_Type = '" + TransDBtype(DropDownList2.SelectedItem.Text, 0+ "',fld_Name = '" + strName + "',fld_FieldSize ='" + GetLenth(iLength) + "', fld_OtherName='" + strOtherName + "' where fld_ID =" + ViewState["OrderID"].ToString();
                    objdb.excNonQuery(strsql);
                    getTabSchema();
///刷新gridview            
                }

        
            }

        
    }


    
protected void Button3_Click(object sender, EventArgs e)
    {
        
//AccessDB objDb = new AccessDB("Crunode_EntireSpaceTime");
        
//string strsql = "select * from T_ProductOrder where 1<>1";
        
//DataTable datatable = objDb.GetSchema(strsql, true);

        
//foreach (DataRow dr in datatable.Rows)
        
//{
        
//    string strsql1 = "insert into T_OrderSchema values(";
        
//    strsql1 += "'" + dr["ColumnName"].ToString() + "',";
        
//    strsql1 += "'" + dr["DataTypeName"].ToString() + "',";
        
//    strsql1 += "'" + dr["ColumnSize"].ToString() + "',";
        
//    strsql1 += "1,";
        
//    strsql1 += "1)";
        
//    objDb.excNonQuery(strsql1);

        
//}
    }
    
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
posted on 2009-04-21 17:55  Robin99  阅读(252)  评论(0编辑  收藏  举报