public partial class Index : Form
{
public Index()
{
InitializeComponent();
GetDatabaseName();
}
//连接数据库,并显示数据库
public void GetDatabaseName()
{
string sql = " select name from sysdatabases " ;
using (SqlDataReader reader = DBHelper.ExecuteReader(String.Format(DBHelper.CONNSTRING, " master " ), CommandType.Text, sql, null ))
{
while (reader.Read())
{
cbbDateBaseName.Items.Add(reader[ " Name " ].ToString());
}
cbbDateBaseName.SelectedIndex = 0 ;
}
}
/// <summary>
/// 生成数据访问层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDAL_Click(object sender, EventArgs e)
{
classNameLastTag = "Service";
string sql = "SELECT * FROM " + cboDataTable.Text;
if (cboDataTable.Text == "")
{
MessageBox.Show("请选择要生成的表");
return;
}
//生成分页代码
using (SqlDataReader reader = DBHelper.ExecuteReader(String.Format(DBHelper.CONNSTRING, cbbDateBaseName.Text), CommandType.Text, sql, null ))
{
sb.AppendLine( " using System; " );
sb.AppendLine( " using System.Collections.Generic; " );
sb.AppendLine( " using System.Linq; " );
sb.AppendLine( " using System.Text; " );
sb.AppendLine( " using " + cbbDateBaseName.Text + " .Model; " );
sb.AppendLine( " using System.Data.SqlClient; " );
sb.AppendLine( " using System.Data " );
sb.AppendLine( " namespace " + cbbDateBaseName.Text + " .DAL " );
sb.AppendLine( " { " );
sb.AppendLine( " \tpublic class " + cboDataTable.Text + " Service " );
sb.AppendLine( " \t{ " );
sb.AppendLine( " \t\t///<summary> " );
sb.AppendLine( " \t\t///得到 " + cboDataTable.Text + " 数据进行分页 " );
sb.AppendLine( " \t\t///<summary> " );
sb.AppendLine( " \t\t///<param name=\ " pageSize\ " ></param> " );
sb.AppendLine( " \t\t///<param name=\ " pageIndex\ " ></param> " );
sb.AppendLine( " \t\tpublic static IList< " + cboDataTable.Text + " > GetAll " + cboDataTable.Text + " (int pageSize, int pageIndex) " );
sb.AppendLine( " \t\t{ " );
sb.AppendLine( " \t\t\tpageIndex-- " );
sb.AppendLine( " \t\t\tIList< " + cboDataTable.Text + " > list =new List< " + cboDataTable.Text + " >(); " );
sb.AppendLine( " \t\t\tstring sql=\ " select top (@pageSize) * from " + cboDataTable.Text + " where Id not in (select top (@pageSize * @pageIndex) Id from " + cboDataTable.Text + " order by id desc) order by id desc;\ "" );
sb.AppendLine( " \t\t\tSqlParameter[] pare = new SqlParameter[] " );
sb.AppendLine( " \t\t\t{ " );
sb.AppendLine( " \t\t\t\tnew SqlParameter(\ " @pageSize\ " ,pageSize), " );
sb.AppendLine( " \t\t\t\tnew SqlParameter(\ " @pageIndex\ " ,pageIndex) " );
sb.AppendLine( " \t\t\t}; " );
sb.AppendLine( " \t\t\tusing (SqlDataReader reader = DBHelper.ExecuteReader(DBHelper.CONNSTRING, CommandType.Text, sql,pare) " );
sb.AppendLine( " \t\t\t{ " );
sb.AppendLine( " \t\t\t\twhile (reader.Read()) " );
sb.AppendLine( " \t\t\t\t{ " );
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text + " " + cboDataTable.Text.ToLower() + " = new " + cboDataTable.Text + " (); " );
for ( int i = 0 ; i < reader.FieldCount; i ++ )
{
if (reader.GetFieldType(i).ToString() == " System.Int32 " )
{
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToInt32(reader[\ "" + reader.GetName(i) + " \ " ]); " );
}
else if (reader.GetFieldType(i).ToString() == " System.String " )
{
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = reader[\ "" + reader.GetName(i) + " \ " ].ToString(); " );
}
else if (reader.GetFieldType(i).ToString() == " System.DateTime " )
{
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToDateTime(reader[\ "" + reader.GetName(i) + " \ " ]); " );
}
else if (reader.GetFieldType(i).ToString() == " System.Boolean " )
{
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToBoolean(reader[\ "" + reader.GetName(i) + " \ " ]); " );
}
else if (reader.GetFieldType(i).ToString() == " System.Byte[] " )
{
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToByte(reader[\ "" + reader.GetName(i) + " \ " ]); " );
}
else if (reader.GetFieldType(i).ToString() == " System.Double " )
{
sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToDouble(reader[\ "" + reader.GetName(i) + " \ " ]); " );
}
}
sb.AppendLine( " \t\t\t\t\tlist.Add( " + cboDataTable.Text.ToLower() + " ); " );
sb.AppendLine( " \t\t\t\t} " );
sb.AppendLine( " \t\t\t\treturn list; " );
sb.AppendLine( " \t\t\t} " );
sb.AppendLine( " \t\t}\n\n " );
//通过Id得到对象
2 sb.AppendLine( " \t\t///得到 " + cboDataTable.Text + " 对象信息通过Id " );
3 sb.AppendLine( " \t\t///<summary> " );
4 sb.AppendLine( " \t\t///<param name=\ " Id\ " ></param> " );
5 sb.AppendLine( " \t\tpublic static " + cboDataTable.Text + " Get " + cboDataTable.Text + " ById(int Id) " );
6 sb.AppendLine( " \t\t{ " );
7 sb.AppendLine( " \t\t\t sting sql=\ " select * from " + cboDataTable.Text + " where Id = @Id;\ "" );
8 sb.AppendLine( " \t\t\t " + cboDataTable.Text + " " + cboDataTable.Text.ToLower() + " =null; " );
9 sb.AppendLine( " \t\t\tSqlParameter[] pare = new SqlParameter[] " );
10 sb.AppendLine( " \t\t\t{ " );
11 sb.AppendLine( " \t\t\t\tnew SqlParameter(\ " @Id\ " ,Id) " );
12 sb.AppendLine( " \t\t\t}; " );
13 sb.AppendLine( " \t\t\tusing (SqlDataReader reader = DBHelper.ExecuteReader(DBHelper.CONNSTRING, CommandType.Text, sql,pare) " );
14 sb.AppendLine( " \t\t\t{ " );
15 sb.AppendLine( " \t\t\t\tif (reader.Read()) " );
16 sb.AppendLine( " \t\t\t\t{ " );
17 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " = new " + cboDataTable.Text + " (); " );
18 for ( int i = 0 ; i < reader.FieldCount; i ++ )
19 {
20 if (reader.GetFieldType(i).ToString() == " System.Int32 " )
21 {
22 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToInt32(reader[\ "" + reader.GetName(i) + " \ " ]); " );
23
24 }
25 else if (reader.GetFieldType(i).ToString() == " System.String " )
26 {
27 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = reader[\ "" + reader.GetName(i) + " \ " ].ToString(); " );
28
29 }
30 else if (reader.GetFieldType(i).ToString() == " System.DateTime " )
31 {
32 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToDateTime(reader[\ "" + reader.GetName(i) + " \ " ]); " );
33
34 }
35 else if (reader.GetFieldType(i).ToString() == " System.Boolean " )
36 {
37 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToBoolean(reader[\ "" + reader.GetName(i) + " \ " ]); " );
38
39 }
40 else if (reader.GetFieldType(i).ToString() == " System.Byte[] " )
41 {
42 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToByte(reader[\ "" + reader.GetName(i) + " \ " ]); " );
43
44 }
45 else if (reader.GetFieldType(i).ToString() == " System.Double " )
46 {
47 sb.AppendLine( " \t\t\t\t\t " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " = Convert.ToDouble(reader[\ "" + reader.GetName(i) + " \ " ]); " );
48
49 }
50
51
52 }
53 sb.AppendLine( " \t\t\t\t} " );
54 sb.AppendLine( " \t\t\t\treturn " + cboDataTable.Text.ToLower() + " ; " );
55 sb.AppendLine( " \t\t\t} " );
56 sb.AppendLine( " \t\t} " );
57 // 更改
58 sb.AppendLine( " \t\t///<summary> " );
59 sb.AppendLine( " \t\t///更改 " + cboDataTable.Text + " 信息通过Id " );
60 sb.AppendLine( " \t\t///<summary> " );
61 sb.AppendLine( " \t\t///<param name=\ "" +cboDataTable.Text+ " \ " ></param> " );
62 sb.AppendLine( " \t\tpublic static int Update " + cboDataTable.Text + " ById( " + cboDataTable.Text + " " + cboDataTable.Text.ToLower() + " ) " );
63 sb.AppendLine( " \t\t{ " );
64 sb.Append( " \t\t\tstring sql =\ " update " + cboDataTable.Text + " set " +reader.GetName(1)+ " = @" +reader.GetName(1));
65 for (int i = 2; i < reader.FieldCount; i++)
66 {
67 sb.Append( " , " +reader.GetName(i)+ " = @" +reader.GetName(i));
68 }
69 sb.AppendLine( " where Id = @Id; " );
70 sb.AppendLine( " \t\t\tSqlParameter[] pare = new SqlParameter[] " );
71 sb.AppendLine( " \t\t\t{ " );
72 for ( int i = 0 ; i < reader.FieldCount; i ++ )
73 {
74 sb.AppendLine( " \t\t\t\tnew SqlParameter(\ "@" + reader.GetName(i) + " \ " , " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " ), " );
75 }
76 sb.AppendLine( " \t\t\t}; " );
77 sb.AppendLine( " \t\t\treturn Convert.ToInt32(DBHelper.ExecuteNonQuery(DBHelper.CONNSTRING, CommandType.Text, sql, para)); " );
78
79 sb.AppendLine( " \t\t} " );
80 // 添加
81 sb.AppendLine( " \t\t///<summary> " );
82 sb.AppendLine( " \t\t///添加 " + cboDataTable.Text + " 信息 " );
83 sb.AppendLine( " \t\t///<summary> " );
84 sb.AppendLine( " \t\t///<param name=\ "" + cboDataTable.Text + " \ " ></param> " );
85 sb.AppendLine( " \t\tpublic static int Insert " + cboDataTable.Text + " ( " + cboDataTable.Text + " " + cboDataTable.Text.ToLower() + " ) " );
86 sb.AppendLine( " \t\t{ " );
87 sb.Append( " \t\t\tstring sql =\ " Insert into " + cboDataTable.Text + " ( " + reader.GetName(1));
88 for ( int i = 2 ; i < reader.FieldCount; i ++ )
89 {
90 sb.Append( " , " + reader.GetName(i));
91 }
92 sb.Append( " ) values ( @ " + reader.GetName( 1 ) + "" );
93 for ( int i = 2 ; i < reader.FieldCount; i ++ )
94 {
95 sb.Append( " ,@ " + reader.GetName(i));
96 }
97 sb.AppendLine( " ); " );
98 sb.AppendLine( " \t\t\tSqlParameter[] pare = new SqlParameter[] " );
99 sb.AppendLine( " \t\t\t{ " );
100 for ( int i = 1 ; i < reader.FieldCount; i ++ )
101 {
102 sb.AppendLine( " \t\t\t\tnew SqlParameter(\ "@" + reader.GetName(i) + " \ " , " + cboDataTable.Text.ToLower() + " . " + reader.GetName(i) + " ), " );
103 }
104 sb.AppendLine( " \t\t\t}; " );
105 sb.AppendLine( " \t\t\treturn Convert.ToInt32(DBHelper.ExecuteNonQuery(DBHelper.CONNSTRING, CommandType.Text, sql, para)); " );
106
107 sb.AppendLine( " \t\t} " );
108
109
110
//根据Id删除对象
2 sb.AppendLine( " \t\t///删除 " + cboDataTable.Text + " 信息通过Id " );
3 sb.AppendLine( " \t\t///<summary> " );
4 sb.AppendLine( " \t\t///<param name=Id></param> " );
5 sb.AppendLine( " \t\tpublic static int Delete " + cboDataTable.Text + " ById(int Id) " );
6 sb.AppendLine( " \t\t{ " );
7 sb.AppendLine( " \t\t\t string sql =delete " + cboDataTable.Text + " where Id=@Id; " );
8 sb.AppendLine( " \t\t\tSqlParameter[] pare = new SqlParameter[] " );
9 sb.AppendLine( " \t\t\t{ " );
10 sb.AppendLine( " \t\t\t\tnew SqlParameter(\ " @Id\ " ,Id) " );
11 sb.AppendLine( " \t\t\t}; " );
12 sb.AppendLine( " \t\t\treturn Convert.ToInt32(DBHelper.ExecuteNonQuery(DBHelper.CONNSTRING, CommandType.Text, sql, para)); " );
13 sb.AppendLine( " \t\t} " );
14 // 批量删除对象
15 sb.AppendLine( " \t\t///<summary> " );
16 sb.AppendLine( " \t\t///删除 " + cboDataTable.Text + " 信息通过Id " );
17 sb.AppendLine( " \t\t///<summary> " );
18 sb.AppendLine( " \t\t///<param name=Id></param> " );
19 sb.AppendLine( " \t\tpublic static int Delete " + cboDataTable.Text + " ById(string Id) " );
20 sb.AppendLine( " \t\t{ " );
21 sb.AppendLine( " \t\t\t string sql =delete " + cboDataTable.Text + " where Id in (\ " + Id + \ " ); " );
22 sb.AppendLine( " \t\t\treturn Convert.ToInt32(DBHelper.ExecuteNonQuery(DBHelper.CONNSTRING, CommandType.Text, sql, para)); " );
23 sb.AppendLine( " \t\t} " );
24 }
25
26 sb.AppendLine( " \t} " );
27 sb.AppendLine( " } " );
28
29
30 rtb.Text = sb.ToString();
31
32
33
/// <summary>
/// 生成业务逻辑层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBLL_Click(object sender, EventArgs e)
{
classNameLastTag = "Mananger";
if (cboDataTable.Text == "")
{
MessageBox.Show("请选择要生成的表");
return;
}
}
/// <summary>
/// 生成实体类
2 /// <param name="sender"></param>
3 /// <param name="e"></param>
4
5 private void btnModule_Click( object sender, EventArgs e)
6 {
7 classNameLastTag = "" ;
8 if (cboDataTable.Text == "" )
9 {
10 MessageBox.Show( " 请选择要生成的表 " );
11 return ;
12 }
13 string sql = " SELECT * FROM " + cboDataTable.Text;
14 StringBuilder sb = new StringBuilder();
15 using (SqlDataReader reader = DBHelper.ExecuteReader(String.Format(DBHelper.CONNSTRING, cbbDateBaseName.Text), CommandType.Text, sql, null ))
16 {
17 sb.AppendLine( " using System; " );
18 sb.AppendLine( " using System.Collections.Generic; " );
19 sb.AppendLine( " using System.Linq; " );
20 sb.AppendLine( " using System.Text; " );
21 sb.AppendLine( " namespace " + cbbDateBaseName.Text + " .Model " );
22 sb.AppendLine( " \t{ " );
23 sb.AppendLine( " \t \t[Serializable] " );
24 sb.AppendLine( " \t\tpublic class " + cboDataTable.Text + "" );
25 sb.AppendLine( " \t\t{ " );
26 for ( int i = 0 ; i < reader.FieldCount; i ++ )
27 {
28 sb.AppendLine( " \t\t\tpublic " + reader.GetFieldType(i) + " " + reader.GetName(i) + " { get; set; } " );
29 }
30 sb.AppendLine( " \t\t} " );
31 sb.AppendLine( " \t} " );
32 rtb.Text = sb.ToString();
33 }
34 }
35 private void cbbDateBaseName_SelectedValueChanged( object sender, EventArgs e)
36 {
37 cboDataTable.Items.Clear();
38 string sql = " SELECT Name FROM SysObjects Where XType='U' " ;
39 using (SqlDataReader reader = DBHelper.ExecuteReader(String.Format(DBHelper.CONNSTRING, cbbDateBaseName.Text), CommandType.Text, sql, null ))
40 {
41 while (reader.Read())
42 {
43 cboDataTable.Items.Add(reader[ " Name " ].ToString());
44 }
45 }
46 if (cboDataTable.Items.Count != 0 )
47 {
48 cboDataTable.SelectedIndex = 0 ;
49 }
50 else
51 {
52 cboDataTable.Text = "" ;
53 }
54
55
56
57 }
58
59
//生成cs类库文件
private void btnUpload_Click(object sender, EventArgs e)
{
FolderDialog f = new FolderDialog();
if (f.DisplayDialog() != DialogResult.OK)
{
return;
}
else
{
try
{
FileStream nFile = new FileStream(f.Path + "" + cboDataTable.Text + classNameLastTag + ".cs", FileMode.CreateNew);
StreamWriter writer = new StreamWriter(nFile);
writer.WriteLine(rtb.Text);
writer.Close();
MessageBox.Show("类库生成成功!!!");
}
catch
{
MessageBox.Show("出错了");
}
}
}
private void cbbDateBaseName_SelectedIndexChanged(object sender, EventArgs e)
{
cboDataTable.Items.Clear();
string sql = "SELECT Name FROM SysObjects Where XType='U'";
using (SqlDataReader reader = DBHelper.ExecuteReader(String.Format(DBHelper.CONNSTRING, cbbDateBaseName.Text), CommandType.Text, sql, null))
{
while (reader.Read())
{
cboDataTable.Items.Add(reader["Name"].ToString());
}
}
if (cboDataTable.Items.Count != 0)
{
cboDataTable.SelectedIndex = 0;
}
else
{
cboDataTable.Text = "";
}
}
}