string dir = "";
private string filepath = "";
private string model = "";
protected string data = "abcdefghi";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
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 button1_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.richTextBox1.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.richTextBox1.Text=sbshu.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.folderBrowserDialog1.ShowDialog();
this.filepath=this.folderBrowserDialog1.SelectedPath.ToString();
this.model = this.filepath.Substring(this.filepath.LastIndexOf("\")+1);
MessageBox.Show(this.model);
MessageBox.Show(this.filepath);
}
string all = "";
public void ShowAll(string dir)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
if (di.GetFiles().Length > 0)
{
System.IO.FileInfo[] files = di.GetFiles();
for (int i = 0; i < files.Length; i++)
all += "\n" + files[i].FullName.Trim() + "end\n";
}
}
private void button3_Click(object sender, EventArgs e)
{
this.folderBrowserDialog2.ShowDialog();
this.dir = this.folderBrowserDialog2.SelectedPath;
this.ShowAll(dir);
//MessageBox.Show(GetTable("select * from titles").Rows[0]["test"].ToString());
}
文章来源:
http://www.cnblogs.com/wangdetian168/archive/2008/06/25/1229993.html
posted on 2010-09-28 11:02
sanmao 阅读(419)
评论(0) 编辑 收藏