实例介绍
【实例简介】字段(属性)生成器
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Business; namespace 字段_属性_生成器 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } ServerBll sb = new ServerBll(); string ServerName = ""; string dataBaseName = ""; string tableName = ""; string inputString = ""; //存放输出字符串 bool copy = false; private void cboDataBase_MouseClick(object sender, MouseEventArgs e) { if (cboServerName.Text.Trim() == null || cboServerName.Text.Trim() == "") { MessageBox.Show("请输入一个服务器名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboServerName.Focus(); return; } if (cboServerName.Text.Trim() != ServerName) { cboDataBase.Items.Clear(); List<string> databaseNames = sb.GetDataBaseNames(cboServerName.Text.Trim(), "master"); //for (int i = 0; i < databaseNames.Count; i ) // cboDataBase.Items.Add(databaseNames[i]); cboDataBase.DisplayMember = "name"; cboDataBase.DataSource = databaseNames; cboDataBase.Text = null; ServerName = cboServerName.Text.Trim(); } } private void cboTableName_MouseClick(object sender, MouseEventArgs e) { if (cboServerName.Text.Trim() == null || cboServerName.Text.Trim() == "") { MessageBox.Show("请输入一个服务器名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboServerName.Focus(); return; } if (cboDataBase.Text.Trim() == null || cboDataBase.Text.Trim() == "") { MessageBox.Show("请选择一个数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboDataBase.Focus(); return; } if (cboDataBase.Text.Trim() != dataBaseName) { cboTableName.Items.Clear(); List<string> tableNames = sb.GetTables(cboServerName.Text.Trim(), cboDataBase.Text.Trim()); //for (int i = 0; i < tableNames.Count; i ) // cboTableName.Items.Add(tableNames[i]); cboTableName.DisplayMember = "name"; cboTableName.DataSource = tableNames; cboTableName.Text = null; dataBaseName = cboDataBase.Text.Trim(); } } string[] propertyName ={ "INT", "STRING", "FLOAT", "DECIMAL", "BOOL", "ENUM", "STRUCT", "CHAR", "SBYTE","SHORT","LONG","BYTE","USHORT","UINT","ULONG","DOUBLE" }; private void btnConsole_Click(object sender, EventArgs e) { #region 判断服务器名、数据库名、表名下拉框中是否有值 /* if (cboServerName.Text.Trim() == null || cboServerName.Text.Trim() == "") { MessageBox.Show("请输入一个服务器名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboServerName.Focus(); return; } if (cboDataBase.Text.Trim() == null || cboDataBase.Text.Trim() == "") { MessageBox.Show("请选择一个数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboDataBase.Focus(); return; } if (cboTableName.Text.Trim() == null || cboTableName.Text.Trim() == "") { MessageBox.Show("请选择一个表!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboDataBase.Focus(); return; } * */ #endregion if (txtInput.Text.Trim() == "" && cboTableName.Text.Trim() == "") { MessageBox.Show("请先输入您要操作的字段\n或选择数据库中的一个表", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } if (cboType.Text.Trim() != "") { if (cboType.Text == " 属 性") { txtOut.Text = ""; if (txtInput.Text.Trim() == "") { MessageBox.Show("您还没有输入字段呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } int index = txtInput.Text.Trim().IndexOf(';'); if (index < 0) { MessageBox.Show("您没有在变量后面输入(英文)分号“;”", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } string input = txtInput.Text.Trim(); string[] str = input.Split(';'); for (int i = 0; i < str.Length - 1; i ) { str[i] = str[i].Trim(); string[] s = str[i].Split(' ', '\r', '\n'); if (s.Length == 1) { MessageBox.Show("您还没有输入变量名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } string second = s[s.Length - 1].Substring(0, 1).ToLower(); //把下划线后的第一个字母变成小写 string private1 = " _" second s[s.Length - 1].Substring(1); //存放私有字段 for (int j = 0; j < propertyName.Length; j ) { if (s[0].ToUpper() == propertyName[j]) s[0] = s[0].ToLower(); } if (s[0].ToLower() == "datetime") s[0] = "DateTime"; txtOut.Text = "private " s[0] private1 ";"; //s[0]是类型 string first = (s[s.Length - 1].Substring(0, 1)).ToUpper(); s[s.Length - 1] = first (s[s.Length - 1].Substring(1)); txtOut.Text = "\r\npublic " s[0] " " s[s.Length - 1]; txtOut.Text = "\r\n{\r\n"; txtOut.Text = "\tget { return " private1 " ; }\r\n"; txtOut.Text = "\tset { " private1 " = value ; }\r\n"; txtOut.Text = "}\r\n\r\n"; } } else if (cboType.Text == " 枚 举") { if (cboTableName.Text.Trim() == "" && btnConsole.Text == "生 成 枚 举") { MessageBox.Show("不能以自定义字段来生成枚举类型!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); cboType.Focus(); return; } txtOut.Text = ""; if (txtInput.Text.Trim() == "") { MessageBox.Show("您还没有输入字段呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } int index = txtInput.Text.Trim().IndexOf(';'); if (index < 0) { MessageBox.Show("您没有在变量后面输入(英文)分号“;”", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } string input = txtInput.Text.Trim(); string[] str = input.Split(';'); string one = cboTableName.Text.Trim().Substring(0, 1).ToUpper(); //把表中的第一个字母变成大写 one = cboTableName.Text.Trim().Substring(1); txtOut.Text = "public enum " one "\r\n{\r\n"; for (int i = 0; i < str.Length - 1; i ) { str[i] = str[i].Trim(); string[] s = str[i].Split(' ', '\r', '\n'); string oneString = s[1].Substring(0, 1).ToUpper(); oneString = s[1].Substring(1); txtOut.Text = "\t" oneString; if (i != str.Length - 2) txtOut.Text = ",\r\n"; else txtOut.Text = "\r\n"; } txtOut.Text = "}\r\n\r\n"; } else if (cboType.Text == " 脚 本") { MessageBox.Show("此功能正在升级中……", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("请选择一个生成类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboType.Focus(); return; } } private void cboType_SelectedValueChanged(object sender, EventArgs e) { if (cboType.Text == " 属 性") btnConsole.Text = "生 成 属 性"; if (cboType.Text == " 枚 举") btnConsole.Text = "生 成 枚 举"; if (cboType.Text == " 脚 本") btnConsole.Text = "生 成 脚 本"; if (cboType.Text == " 脚 本") { MessageBox.Show("此功能正在升级中……", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (tableName != cboTableName.Text.Trim()) { LoadProperty(); } } /// <summary> /// 加载数据库中的字段到文本框中 /// </summary> private void LoadProperty() { string[] TypeString = { "varchar", "nvarchar", "ntext", "text", "nchar", "char" }; List<string> typeAndPropertys = sb.GetTypeAndProperty(cboServerName.Text.Trim(), cboDataBase.Text.Trim(), cboTableName.Text.Trim()); for (int i = 0; i < typeAndPropertys.Count; i ) { if (i % 2 == 0) { if (typeAndPropertys[i] == "bigint") typeAndPropertys[i] = "int"; for (int j = 0; j < TypeString.Length; j ) { if (typeAndPropertys[i] == TypeString[j]) //SQL中的char和nchar在C#中的数据类型是哪种 { typeAndPropertys[i] = "string"; break; } } inputString = typeAndPropertys[i] " "; } else { inputString = typeAndPropertys[i]; inputString = ";\r\n"; } } txtInput.Text = ""; txtInput.Text = inputString; tableName = cboTableName.Text.Trim(); } private void cboTableName_SelectedIndexChanged(object sender, EventArgs e) { if (cboTableName.Text.Trim() != tableName) { if (txtInput.Text.Trim() != "") { if (cboDataBase.Text.Trim() != dataBaseName) return; DialogResult dr = MessageBox.Show(" 重新选择一个表\n您要保留原有的字段吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr != DialogResult.Yes) { txtInput.Text = ""; inputString = ""; txtOut.Text = ""; LoadProperty(); btnConsole.Text = "生 成 属 性"; } else { txtOut.Text = ""; inputString = "\r\n"; LoadProperty(); btnConsole.Text = "生 成 属 性"; } cboType.Text = null; } //else //{ // LoadProperty(); //} } } private void btnCopy_Click(object sender, EventArgs e) { if (txtInput.Text.Trim() != "" && txtOut.Text.Trim() == "") { MessageBox.Show("您还没有生成属性、枚举、脚本!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } if (txtInput.Text.Trim() == "" || txtOut.Text.Trim() == "") { MessageBox.Show("您还没有输入内容呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } else { Clipboard.SetDataObject(txtOut.Text); //复制代码 copy = true; MessageBox.Show("代码复制成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void btnClear_Click(object sender, EventArgs e) { if (txtInput.Text.Trim() == "" && txtOut.Text.Trim() == "") { MessageBox.Show("您还没有输入内容呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } DialogResult dr = MessageBox.Show("您确实要清空内容吗?", "清空", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr != DialogResult.No) { txtInput.Text = ""; txtOut.Text = ""; } cboType.Text = null; } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (txtOut.Text.Trim() != "" && copy == false) { DialogResult drt = MessageBox.Show("您生成的代码还没有复制\n您是否要现在复制代码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (drt == DialogResult.Yes) { Clipboard.SetDataObject(txtOut.Text); //复制代码 copy = true; MessageBox.Show("代码复制成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Cancel = true; } else { e.Cancel = false; } } } private void btnCancel_Click(object sender, EventArgs e) { if (btnCancel.Text == "取消窗体总在最前") { btnCancel.Text = "设置窗体总在最前"; this.TopMost = false; } else { btnCancel.Text = "取消窗体总在最前"; this.TopMost = true; } } private void cboDataBase_SelectedIndexChanged(object sender, EventArgs e) { cboTableName.Text = null; cboType.Text = null; txtInput.Text = ""; } string property = ""; private void cboType_SelectedIndexChanged(object sender, EventArgs e) { //if (cboTableName.Text.Trim() != "" && tableName == cboTableName.Text.Trim() && property != cboType.Text) //{ // LoadProperty(); // property = cboType.Text; //} } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论