Tool generation code database model generation code

Source: Internet
Author: User

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)
{< br> foreach (Control in this. tpchange. controls)
{< br> If (control. getType (). tostring () = "system. windows. forms. richTextBox ")
{< br> (system. windows. forms. richTextBox) control ). TEXT = "";
}< BR >}

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 );
// The first half of 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 ("}");
// Cyclic Field
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 (",");
}
/// Add
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 (",");
}
/// Modify
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 (")");
/// Cyclic Field
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 );
// Add end
/// Modify
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 );
/// Delete
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 ("}");
// Query

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 ("// query "). 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 );
}




}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.