c# 生成model 层代码 测试版

string dir = "" ;
private string filepath = "" ;
private string model = "" ;
protected string data = " abcdefghi " ;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 获取数据
/// </summary>
/// <paramname="sql"></param>
/// <returns></returns>
public DataTableGetTable( string sql)
{
DataSetds
= new DataSet();
try
{
SqlConnectionconn
= new SqlConnection();
conn.ConnectionString
= " initialcatalog= " + this .tbdata.Text.TrimEnd().ToString() + " ;datasource=.;userid= " + this .tbuser.Text.TrimEnd() + " ;password= " + this .tbpass.Text.TrimEnd();
SqlCommandcmd
= new SqlCommand();
cmd.Connection
= conn;
cmd.CommandText
= sql;
SqlDataAdapterda
= new SqlDataAdapter();
da.SelectCommand
= cmd;

da.Fill(ds);

}
catch (Exceptionex)
{

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


private void button1_Click( object sender,EventArgse)
{
DataTabledt
= this .GetTable( " select*fromsysobjectswherextype='u'orderbystatusdesc " );
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());
DataTabledtt
= this .GetTable( " select*from[ " + Convert.ToString(dt.Rows[i][ " name " ]) + " ] " );
System.Text.StringBuildersbshu
= new StringBuilder( " usingSystem; " ).Append(System.Environment.NewLine).Append( " usingSystem.Collections.Generic; " ).Append(System.Environment.NewLine).Append( " usingSystem.Text; " ).Append(System.Environment.NewLine).Append( " usingSystem.Data; " ).Append(Environment.NewLine).Append( " usingSystem.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.StringBuildersbset
= new StringBuilder( "" );

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

StringBuildersbpas
= new StringBuilder( " SqlParameter[]pas={ " );
StringBuildersbadd
= new StringBuilder( " publicboolAdd() " );
sbadd.Append(
" { " );
sbadd.Append(Environment.NewLine);
sbshu.Append(Environment.NewLine);
sbshu.Append(
" publicclass " );
sbshu.Append(tableBean);
sbshu.Append(
" { " );
sbshu.Append(Environment.NewLine);

System.Text.StringBuildersbsqlbefore
= new StringBuilder( " insertinto " ).Append(tableBean).Append( " ( " );
System.Text.StringBuildersbsqlafter
= new StringBuilder( " values( " );
StringBuildersbupd
= new StringBuilder( " publicboolUpdate() " + Environment.NewLine + " { " + Environment.NewLine + " stringsql="update " ).Append(tableBean).Append( " set " );
StringBuildersbdel
= new StringBuilder( " publicboolDelete() " ).Append(Environment.NewLine).Append( " { " ).Append( " stringsql="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);
StringBuildersbdelpas
= new StringBuilder( " SqlParameter[]pas={ " );
StringBuildersbsel
= new StringBuilder( " publicDataTableSelect() " ).Append(Environment.NewLine).Append( " { " ).Append(Environment.NewLine).Append( " DataTabledt=db.GetTable("select*from " ).Append(tableBean).Append( " ")??newDataTable(); " ).Append(Environment.NewLine).Append( " returndt; " ).Append(Environment.NewLine).Append( " } " );
// 循环字段
for ( int j = 0 ;j < dtt.Columns.Count;j ++ )
{
sbshu.Append(
" privatestring_ " + dtt.Columns[j].ColumnName.ToString() + " ; " + System.Environment.NewLine);
sbset.Append(
" publicstring " + dtt.Columns[j].ColumnName.ToString() + " {set{this._ " + dtt.Columns[j].ColumnName.ToString() + " =value;}get{returnthis._ " + 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(
" newSqlParameter(" " );
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( " stringsql=" " ).Append(sbsqlbefore.ToString()).Append(sbsqlafter.ToString()).Append( " " " ).Append( " ; " ).Append(Environment.NewLine);
sbadd.Append(sbpas.ToString()).Append(Environment.NewLine);
sbadd.Append(
" returndb.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(
" returndb.GetState(sql,pas); " ).Append(Environment.NewLine);
sbupd.Append(
" } " ).Append(Environment.NewLine);
/// 删除
sbdelpas.Append( " newSqlParameter(" " ).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(
" returndb.GetState(sql,pas); " ).Append(Environment.NewLine).Append( " } " );
// 查询


sbshu.Append(sbset.ToString()).Append(System.Environment.NewLine).Append(
" DbAccessdb=newDbAccess(); " ).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.StreamWritersw = 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,EventArgse)
{
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.DirectoryInfodi
= 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,EventArgse)
{
this .folderBrowserDialog2.ShowDialog();
this .dir = this .folderBrowserDialog2.SelectedPath;
this .ShowAll(dir);

// MessageBox.Show(GetTable("select*fromtitles").Rows[0]["test"].ToString());
}

你可能感兴趣的:(sql,C++,c,C#,J#)