using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace QuickCode
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
string m_bootpath = AppDomain.CurrentDomain.BaseDirectory + "\\Template";
object SelectedItem;
private void button2_Click(object sender, EventArgs e)
{
BindData();
}
private void GetFiles(string filePath)
{
TemplateList.Items.Clear();
DirectoryInfo folder = new DirectoryInfo(filePath);
FileInfo[] chldFiles = folder.GetFiles();
foreach (FileInfo chlFile in chldFiles)
{
TemplateList.Items.Add(chlFile.Name, true);
}
}
private void BindData()
{
DataSet ds = null;
using (SqlConnection con = new SqlConnection(textBox1.Text.Trim()))
{
this.Text = con.Database;
try
{
con.Open();
}
catch (Exception oe)
{
MessageBox.Show("连接失败!详细:" + oe.Message);
return;
}
con.Close();
// 读出表
ds = new DataSet();
String sqltext = @"select SysObjects.Name as TableName from SysObjects
where ( Sysobjects.Xtype ='u' OR Sysobjects.Xtype ='v') order by SysObjects.Name";
SqlDataAdapter SDA = new SqlDataAdapter(sqltext, con);
SDA.Fill(ds);
}
TablesList.Items.Clear();
foreach (DataRow row in ds.Tables[0].Rows)
{
TablesList.Items.Add(row["TableName"].ToString().Trim(), false);
}
}
private void FormMain_Load(object sender, EventArgs e)
{
if (!Directory.Exists(m_bootpath))
{
Directory.CreateDirectory(m_bootpath);
}
GetFiles(m_bootpath); // 显示所有文件夹以及文件
textBox1.Text = Properties.Settings.Default.constr;
}
private void button6_Click(object sender, EventArgs e)
{
Properties.Settings.Default.constr = textBox1.Text.Trim();
Properties.Settings.Default.Save();
}
private void button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < TablesList.Items.Count; i++)
{
TablesList.SetItemChecked(i, true);
}
}
private void button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < TablesList.Items.Count; i++)
{
TablesList.SetItemChecked(i, !TablesList.GetItemChecked(i));
}
}
private void button5_Click(object sender, EventArgs e)
{
for (int i = 0; i < TemplateList.Items.Count; i++)
{
TemplateList.SetItemChecked(i, true);
}
}
private void button7_Click(object sender, EventArgs e)
{
for (int i = 0; i < TemplateList.Items.Count; i++)
{
TemplateList.SetItemChecked(i, !TemplateList.GetItemChecked(i));
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.label2.Text = this.folderBrowserDialog1.SelectedPath;
for (int i = 0; i < TablesList.CheckedItems.Count; i++)
{
Directory.CreateDirectory(this.label2.Text + "\\" + TablesList.CheckedItems[i].ToString());
for (int j = 0; j < TemplateList.CheckedItems.Count; j++)
{
File.Copy(m_bootpath + "\\" + TemplateList.CheckedItems[j].ToString(), this.label2.Text + "\\" + TablesList.CheckedItems[i].ToString() + "\\" + TemplateList.CheckedItems[j].ToString());
}
}
MessageBox.Show("成功!");
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormNewTemplate fn = new FormNewTemplate();
if (fn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (File.Exists(m_bootpath + "\\" + fn.file.Text.Trim()))
{
MessageBox.Show("已经存在");
return;
}
File.WriteAllText(m_bootpath + "\\" + fn.file.Text.Trim(), fn.richTextBox1.Text);
GetFiles(m_bootpath); // 显示所有文件夹以及文件
}
}
private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (TemplateList.SelectedItem != null)
{
FormNewTemplate fn = new FormNewTemplate();
fn.file.Text = TemplateList.SelectedItem.ToString().Trim();
fn.richTextBox1.Text = File.ReadAllText(m_bootpath + "\\" + TemplateList.SelectedItem.ToString());
if (fn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (File.Exists(m_bootpath + "\\" + fn.file.Text.Trim()))
{
MessageBox.Show("已经存在");
return;
}
File.WriteAllText(m_bootpath + "\\" + fn.file.Text.Trim(), fn.richTextBox1.Text);
GetFiles(m_bootpath); // 显示所有文件夹以及文件
}
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (TemplateList.SelectedItem != null)
{
if (!File.Exists(m_bootpath + "\\" + TemplateList.SelectedItem.ToString().Trim()))
{
MessageBox.Show("已经不存在");
return;
}
File.Delete(m_bootpath + "\\" + TemplateList.SelectedItem.ToString().Trim());
GetFiles(m_bootpath); // 显示所有文件夹以及文件
}
}
}
}
namespace QuickCode
{
partial class FormMain
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.button6 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.新建ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.编辑ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.删除ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.TemplateList = new System.Windows.Forms.CheckedListBox();
this.button1 = new System.Windows.Forms.Button();
this.TablesList = new System.Windows.Forms.CheckedListBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.button6);
this.splitContainer1.Panel1.Controls.Add(this.textBox1);
this.splitContainer1.Panel1.Controls.Add(this.label1);
this.splitContainer1.Panel1.Controls.Add(this.button2);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.label2);
this.splitContainer1.Panel2.Controls.Add(this.button7);
this.splitContainer1.Panel2.Controls.Add(this.button5);
this.splitContainer1.Panel2.Controls.Add(this.button4);
this.splitContainer1.Panel2.Controls.Add(this.button3);
this.splitContainer1.Panel2.Controls.Add(this.textBox3);
this.splitContainer1.Panel2.Controls.Add(this.textBox2);
this.splitContainer1.Panel2.Controls.Add(this.TablesList);
this.splitContainer1.Panel2.Controls.Add(this.TemplateList);
this.splitContainer1.Panel2.Controls.Add(this.button1);
this.splitContainer1.Size = new System.Drawing.Size(614, 544);
this.splitContainer1.SplitterDistance = 67;
this.splitContainer1.TabIndex = 0;
//
// button6
//
this.button6.Location = new System.Drawing.Point(435, 41);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(118, 23);
this.button6.TabIndex = 40;
this.button6.Text = "保存此连接字符串";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(117, 15);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(436, 21);
this.textBox1.TabIndex = 38;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(30, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 39;
this.label1.Text = "连接字符串:";
//
// button2
//
this.button2.Location = new System.Drawing.Point(344, 41);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 37;
this.button2.Text = "连接";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.新建ToolStripMenuItem,
this.编辑ToolStripMenuItem,
this.删除ToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 92);
//
// 新建ToolStripMenuItem
//
this.新建ToolStripMenuItem.Name = "新建ToolStripMenuItem";
this.新建ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.新建ToolStripMenuItem.Text = "新建";
this.新建ToolStripMenuItem.Click += new System.EventHandler(this.新建ToolStripMenuItem_Click);
//
// 编辑ToolStripMenuItem
//
this.编辑ToolStripMenuItem.Name = "编辑ToolStripMenuItem";
this.编辑ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.编辑ToolStripMenuItem.Text = "编辑";
this.编辑ToolStripMenuItem.Click += new System.EventHandler(this.编辑ToolStripMenuItem_Click);
//
// 删除ToolStripMenuItem
//
this.删除ToolStripMenuItem.Name = "删除ToolStripMenuItem";
this.删除ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.删除ToolStripMenuItem.Text = "删除";
this.删除ToolStripMenuItem.Click += new System.EventHandler(this.删除ToolStripMenuItem_Click);
//
// TemplateList
//
this.TemplateList.ContextMenuStrip = this.contextMenuStrip1;
this.TemplateList.FormattingEnabled = true;
this.TemplateList.Location = new System.Drawing.Point(309, 35);
this.TemplateList.Name = "TemplateList";
this.TemplateList.Size = new System.Drawing.Size(302, 324);
this.TemplateList.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(506, 413);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 39);
this.button1.TabIndex = 6;
this.button1.Text = "生成并保存";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// TablesList
//
this.TablesList.FormattingEnabled = true;
this.TablesList.Location = new System.Drawing.Point(5, 35);
this.TablesList.Name = "TablesList";
this.TablesList.Size = new System.Drawing.Size(302, 324);
this.TablesList.TabIndex = 8;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(5, 7);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(302, 21);
this.textBox2.TabIndex = 9;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(309, 7);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(302, 21);
this.textBox3.TabIndex = 10;
//
// button3
//
this.button3.Location = new System.Drawing.Point(13, 365);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 11;
this.button3.Text = "全选";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(95, 365);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 12;
this.button4.Text = "反选";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(309, 365);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 13;
this.button5.Text = "全选";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(391, 365);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(75, 23);
this.button7.TabIndex = 14;
this.button7.Text = "反选";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(129, 413);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 12);
this.label2.TabIndex = 15;
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(614, 544);
this.Controls.Add(this.splitContainer1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "代码生成";
this.Load += new System.EventHandler(this.FormMain_Load);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.Button button6;
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem 新建ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 编辑ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 删除ToolStripMenuItem;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.CheckedListBox TablesList;
private System.Windows.Forms.CheckedListBox TemplateList;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.Label label2;
}
}