C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着
private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = this.GetTable("select * from sysobjects where xtype='u'");
            for (int i = 0; i < dt.Rows.Count-1; i++)
            {
                string tableBean = Convert.ToString(dt.Rows[i]["name"]).ToUpper().Replace("WZ_", "");
                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(System.Environment.NewLine).Append("using MediInfo.PocketPC.SBGL.LoadData;").Append(System.Environment.NewLine).Append("using MediInfo.PocketPC.SBGL.DataAccess;").Append(System.Environment.NewLine).Append("namespace MediInfo.PocketPC.SBGL.BizCom").Append(System.Environment.NewLine).Append("{").Append(System.Environment.NewLine).Append("public class Biz" + tableBean + " : Base.BizComBase").Append(System.Environment.NewLine).Append("{").Append(System.Environment.NewLine);

                System.Text.StringBuilder sbset = new StringBuilder("");
               
                System.Text.StringBuilder sbbean = new StringBuilder("public Biz" + tableBean + " SelectById(string id){"+System.Environment.NewLine+"Biz" + tableBean + " biz" + tableBean + " = new Biz" + tableBean + "();"+System.Environment.NewLine+"");
                //sql前半部分
                System.Text.StringBuilder sbsqlbefore = new StringBuilder("strSql = \"insert into ").Append("WZ_").Append(tableBean).Append("(");
                System.Text.StringBuilder sbsqlafter = new StringBuilder(" values(");
                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("biz" + tableBean + "." + 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(dtt.Columns[j].ColumnName.ToString());
                    sbsqlafter.Append("'\" + yhxxDt.Rows[i][\""+dtt.Columns[j].ColumnName.ToString()+"\"].ToString() + \"'");
                }
                sbsqlbefore.Append(")");
                sbsqlafter.Append(")");
                sbshu.Append(sbset.ToString()).Append(sbbean.Append("return biz" + tableBean+";"+System.Environment.NewLine+"}").ToString());
               // 加载函数;
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("public ServiceResult LoadData(LoadMode loadMode)");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("  {");//开始
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("    try");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("      {");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          LoadManager loadManager = LoadManager.CreateInstance();");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          //获取初始插入本地的数据");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          DataTable yhxxDt = loadManager.GetInitData(loadMode, \"\", \"WZ"+tableBean+".XML\");");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          string strSql = \"delete from WZ_" +tableBean+ "\";");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          string connString = LocalDataBase.CreateInstance().GetConnectionString();");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          SqlCeHelper.ExecuteNonQuery(connString, CommandType.Text, strSql, null);");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("          for (int i = 0; i < yhxxDt.Rows.Count; i++)");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("           {");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("              ").Append(sbsqlbefore.ToString()).Append(sbsqlafter.Append("\";").ToString());
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("              SqlCeHelper.ExecuteNonQuery(connString, CommandType.Text, strSql, null);");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("            }");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("            yhxxDt.Dispose();");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("            serviceResult.RetCode = 0;");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("            return serviceResult;");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("       }");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("     catch (Exception ex)");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("       {");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("        ExceptionManager.Publish(ex);");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("        serviceResult.RetCode = -1;");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("        serviceResult.RetMsg = ex.Message;");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("        return serviceResult;");
                sbshu.Append(System.Environment.NewLine);
                sbshu.Append("        }");
                sbshu.Append(System.Environment.NewLine);

                sbshu.Append("    }");//结束
                sbshu.Append(System.Environment.NewLine).Append("}").Append(System.Environment.NewLine).Append("}");
                this.richTextBox1.Text = sbshu.ToString();
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(this.filepath + "file://Biz/" + tableBean + ".cs",false,System.Text.Encoding.GetEncoding("gb2312")))
                {
                    sw.Write(sbshu.ToString());
                    sw.Flush();
                }

            }
            //MessageBox.Show(this.filepath);
            //DataTable dt = this.GetTable(this.comboBox1.SelectedValue.ToString(),this.tbName.Text.Trim(),this.tbPass.Text.Trim(),"select * from sysobjects where xtype='u'");


        }
        public DataTable GetTable(string database, string user, string pass, string sql)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "initial catalog=" + database + ";data source=.;user id=" + user + ";password=" + pass;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
        public DataTable GetTable(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "initial catalog=demo;data source=.;user id=sa;password=sa";
                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];
        } 

posted on 2008-06-12 00:38  王德田  阅读(505)  评论(0编辑  收藏  举报