C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Tools
{
    public partial class FormHome : Form
    {
      
        private string filepath = "";
        private string model = "";
        protected string data = "abcdefghi";
        public FormHome()
        {
            InitializeComponent();
        }
        public DataTable GetTable(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "initial catalog=" + this.tbdata.Text.TrimEnd().ToString() + ";data source=.;user id=" + this.tbuser.Text.TrimEnd() + ";password=" + this.tbpass.Text.TrimEnd();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            return ds.Tables[0];
        }

        private void BtChange_Click(object sender, EventArgs e)
        {
            StringBuilder tempChangeData = new StringBuilder();
            string tempBeforeData = this.RtbBefore.Text;
            string[] tempLines = tempBeforeData.Split('\n');
            string[] tempSingleLine = null;
            foreach (string temp in tempLines)
            {            
                    tempSingleLine = temp.Split('=');
                     if (tempSingleLine.Length==2)
                    tempChangeData.AppendFormat("{0}={1};\n", tempSingleLine[1].Replace(";", ""), tempSingleLine[0]);            
              
            }
            this.RtbAfter.Text = tempChangeData.ToString();
        }

        private void BtClear_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.TpChange.Controls)
            {
                if (control.GetType().ToString() == "System.Windows.Forms.RichTextBox")
                {
                    ((System.Windows.Forms.RichTextBox)control).Text = "";
                }
            }
        }

        private void BtDataCodeMake_Click(object sender, EventArgs e)
        {
            DataTable dt = this.GetTable("select * from sysobjects where xtype='u' order by status desc");
            for (int i = 0; i < dt.Rows.Count - 1; i++)
            {
                string tableBean = Convert.ToString(dt.Rows[i]["name"] ?? "").ToUpper();
                tableBean = tableBean.Length > 1 ? (tableBean.Substring(0, 1).ToUpper() + tableBean.Substring(1).ToLower()) : (tableBean.ToUpper());
                DataTable dtt = this.GetTable("select * from [" + Convert.ToString(dt.Rows[i]["name"]) + "]");
                System.Text.StringBuilder sbshu = new StringBuilder("using System;").Append(System.Environment.NewLine).Append("using System.Collections.Generic;").Append(System.Environment.NewLine).Append("using System.Text;").Append(System.Environment.NewLine).Append("using System.Data;").Append(Environment.NewLine).Append("using System.Data.SqlClient;").Append(System.Environment.NewLine).Append("").Append(System.Environment.NewLine).Append("namespace ").Append(this.tbNamespace.Text.Trim()).Append(System.Environment.NewLine).Append("{").Append(System.Environment.NewLine);

                System.Text.StringBuilder sbset = new StringBuilder("");

                System.Text.StringBuilder sbbean = new StringBuilder("public " + tableBean + " SelectById(){" + System.Environment.NewLine + tableBean + "  " + tableBean.ToLower().ToString() + " = new " + tableBean + "();" + System.Environment.NewLine + "");
                sbbean.Append("string sql = \"select * from " + tableBean + " where " + dtt.Columns[0].ColumnName.ToString() + "=@" + dtt.Columns[0].ColumnName.ToString() + "\";").Append(Environment.NewLine);
                sbbean.Append("SqlParameter[] pas = {new SqlParameter(\"").Append("@").Append(dtt.Columns[0].ColumnName.ToString()).Append("\"").Append(",").Append(dtt.Columns[0].ColumnName.ToString()).Append(")};").Append(Environment.NewLine);
                sbbean.Append("DataTable dt = db.GetTable(sql,pas);").Append(Environment.NewLine);
                sbbean.Append("  if(dt.Rows.Count>0)").Append(Environment.NewLine).Append("    {").Append(Environment.NewLine);
                //sql前半部分

                StringBuilder sbpas = new StringBuilder("SqlParameter[] pas = {");
                StringBuilder sbadd = new StringBuilder("public bool Add()");
                sbadd.Append("{");
                sbadd.Append(Environment.NewLine);
                sbshu.Append(Environment.NewLine);
                sbshu.Append("public class ");
                sbshu.Append(tableBean);
                sbshu.Append("{");
                sbshu.Append(Environment.NewLine);

                System.Text.StringBuilder sbsqlbefore = new StringBuilder("insert into ").Append(tableBean).Append("(");
                System.Text.StringBuilder sbsqlafter = new StringBuilder(" values(");
                StringBuilder sbupd = new StringBuilder("public bool Update()" + Environment.NewLine + "{" + Environment.NewLine + "string sql =\"update ").Append(tableBean).Append(" set ");
                StringBuilder sbdel = new StringBuilder("public bool Delete()").Append(Environment.NewLine).Append("{").Append("string sql = \"delete ").Append(tableBean).Append(" where ").Append("[").Append(dtt.Columns[0].ColumnName.ToString()).Append("]").Append("=").Append("@").Append(dtt.Columns[0].ColumnName.ToString()).Append("\";").Append(Environment.NewLine);
                StringBuilder sbdelpas = new StringBuilder("SqlParameter[] pas = {");
                StringBuilder sbsel = new StringBuilder("public DataTable Select()").Append(Environment.NewLine).Append("{").Append(Environment.NewLine).Append("DataTable dt = db.GetTable(\"select * from ").Append(tableBean).Append("\")??new DataTable();").Append(Environment.NewLine).Append("return dt;").Append(Environment.NewLine).Append("}");
                //循环字段
                for (int j = 0; j < dtt.Columns.Count; j++)
                {
                    sbshu.Append("private string _" + dtt.Columns[j].ColumnName.ToString() + ";" + System.Environment.NewLine);
                    sbset.Append("public string " + dtt.Columns[j].ColumnName.ToString() + "{set{this._" + dtt.Columns[j].ColumnName.ToString() + "=value;}get{return this._" + dtt.Columns[j].ColumnName.ToString() + ";}}" + System.Environment.NewLine);
                    sbbean.Append("      ").Append(tableBean.ToLower().ToString() + "." + dtt.Columns[j].ColumnName.ToString() + "=Convert.ToString(dt.Rows[0][\"" + dtt.Columns[j].ColumnName.ToString() + "\"]);").Append(System.Environment.NewLine);
                    if (j != 0)
                    {
                        sbsqlbefore.Append(",");
                        sbsqlafter.Append(",");
                    }
                    ///添加
                    sbsqlbefore.Append("[").Append(dtt.Columns[j].ColumnName.ToString()).Append("]");
                    sbsqlafter.Append("@" + dtt.Columns[j].ColumnName.ToString());
                    sbpas.Append("new SqlParameter(\"");
                    sbpas.Append("@");
                    sbpas.Append(dtt.Columns[j].ColumnName.ToString());
                    sbpas.Append("\",");
                    sbpas.Append(dtt.Columns[j].ColumnName.ToString());
                    sbpas.Append(")");
                    if (j < dtt.Columns.Count - 1)
                    {
                        sbpas.Append(",");
                    }
                    ///修改
                    if (j != 0)
                    {
                        sbupd.Append("[").Append(dtt.Columns[j].ColumnName.ToString()).Append("]").Append("=");
                        sbupd.Append("@");
                        sbupd.Append(dtt.Columns[j].ColumnName.ToString());
                        if (j < dtt.Columns.Count - 1)
                        {
                            sbupd.Append(",");
                        }
                    }


                }
                sbpas.Append("};");
                sbsqlbefore.Append(")");
                sbsqlafter.Append(")");
                ///循环字段
                sbadd.Append("string sql = \"").Append(sbsqlbefore.ToString()).Append(sbsqlafter.ToString()).Append("\"").Append(";").Append(Environment.NewLine);
                sbadd.Append(sbpas.ToString()).Append(Environment.NewLine);
                sbadd.Append("return db.GetState(sql,pas);").Append(Environment.NewLine);
                sbadd.Append("}").Append(Environment.NewLine);
                //添加结束
                ///修改
                sbupd.Append(" where ");
                sbupd.Append(dtt.Columns[0].ColumnName.ToString());
                sbupd.Append("=");
                sbupd.Append("@");
                sbupd.Append(dtt.Columns[0].ColumnName.ToString()).Append("\";").Append(Environment.NewLine);
                sbupd.Append(sbpas).Append(Environment.NewLine);
                sbupd.Append("return db.GetState(sql,pas);").Append(Environment.NewLine);
                sbupd.Append("}").Append(Environment.NewLine);
                ///删除
                sbdelpas.Append("new SqlParameter(\"").Append("@").Append(dtt.Columns[0].ColumnName.ToString()).Append("\"").Append(",").Append(dtt.Columns[0].ColumnName.ToString()).Append(")};");
                sbdel.Append(sbdelpas.ToString()).Append(Environment.NewLine);
                sbdel.Append("return db.GetState(sql,pas);").Append(Environment.NewLine).Append("}");
                //查询


                sbshu.Append(sbset.ToString()).Append(System.Environment.NewLine).Append("DbAccess db = new DbAccess();").Append(Environment.NewLine).Append(sbadd.ToString()).Append(Environment.NewLine).Append(sbupd.ToString()).Append(Environment.NewLine).Append(sbdel.ToString()).Append(Environment.NewLine).Append(sbsel.ToString()).Append(Environment.NewLine).Append("//查询").Append(System.Environment.NewLine).Append(sbbean.Append(System.Environment.NewLine).Append("    }").Append(Environment.NewLine).Append("return " + tableBean.ToLower().ToString() + ";" + System.Environment.NewLine + "}").ToString());
                ///
                sbshu.Append(System.Environment.NewLine);

                sbshu.Append(System.Environment.NewLine).Append("}").Append(System.Environment.NewLine).Append("}");
                this.RtbCodes.Text = sbshu.ToString();
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(this.filepath + "\\" + tableBean + ".cs", false, System.Text.Encoding.GetEncoding("gb2312")))
                {
                    sw.Write(sbshu.ToString());
                    sw.Flush();
                }
                this.RtbCodes.Text = sbshu.ToString();

            }
        }

        private void BtBrower_Click(object sender, EventArgs e)
        {
            this.FbdFileBrower.ShowDialog();
            this.filepath = this.FbdFileBrower.SelectedPath.ToString();
            this.model = this.filepath.Substring(this.filepath.LastIndexOf("\\") + 1);
        }

    


     
       
     
    }
}

posted on 2008-12-02 18:48  王德田  阅读(277)  评论(0编辑  收藏  举报