⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autocreatecode.cs

📁 一款自动编码工具
💻 CS
📖 第 1 页 / 共 4 页
字号:
					TreeNode RootNode;
					RootNode = DBTreeView.Nodes.Add( dtDataBase.Rows[i][0].ToString() );
					// 得到指定库中的用户表
					DataTable dtTableName = sqlconn.GetTableName( dtDataBase.Rows[i][0].ToString() );
					if( dtTableName != null )
					{
						for( int k = 0; k < dtTableName.Rows.Count; k++ )
						{
							RootNode.Nodes.Add( dtTableName.Rows[k][0].ToString() );
						}
					}
				}
			}
		}


		private void button_create_Click(object sender, System.EventArgs e)
		{
			string tableName	=	txttabname.Text.Trim();
			string userName     =   textBox_Username.Text.Trim();  
			string filePath		=	Application.StartupPath+@"\config.ini";

			DataTable dt	    =   sqlconn.GetTableInfo(tableName);
			//this.dataGrid1.DataSource = dt;

			
			StreamWriter sw=  File.CreateText("DA"+tableName+"Table.cs");
			string type	= "";
			
			
			//生成类文件头

			sw.WriteLine("/************************************************************");
			sw.WriteLine("*                                        ");
			sw.WriteLine("* 类    名 :	DA" + tableName + "Table.cs");
			sw.WriteLine("* 功能描述 :                             ");
			sw.WriteLine("* 处理流程 :                             ");
			sw.WriteLine("* 算    法 :                             ");
			sw.WriteLine("* 姓    名 :	"+ userName +"             ");
			sw.WriteLine("* 日    期 :	" + DateTime.Now            );
			sw.WriteLine("* 修    改 :	                           ");
			sw.WriteLine("* 日    期 :	                           ");
			sw.WriteLine("* 错误编号 :	                           ");
			sw.WriteLine("* 备    注 :	                           ");
			sw.WriteLine("* 版    本 :	                           ");
			sw.WriteLine("* 错误编号 :	                           ");
			sw.WriteLine("*                                        ");
			sw.WriteLine(" ************************************************************/");

			sw.WriteLine("using System;");
			sw.WriteLine("using System.Data;");
			sw.WriteLine("using System.Data.SqlClient;");
			sw.WriteLine("using System.Text;\n");
			
			if( this.txtNameSpace.Text != "" )
			{
				sw.WriteLine("namespace "+ this.txtNameSpace.Text );
			}
			else
			{
				sw.WriteLine("namespace Null");
			}
			sw.WriteLine("{");
			sw.WriteLine("\t/// <summary>");
			sw.WriteLine("\t/// DA"+ tableName +" 的摘要说明。");
			sw.WriteLine("\t/// <summary>");

			sw.WriteLine("	public class DA" + tableName + "Table :" + " SqlServerDataBaseAccess");
			sw.WriteLine("	{");

			sw.WriteLine("		public	DA" + tableName + "Table()");
			sw.WriteLine("		{");
			sw.WriteLine("\t\t\t//");
			sw.WriteLine("\t\t\t// TODO: 在此处添加构造函数逻辑 ");
			sw.WriteLine("\t\t\t//");
			sw.WriteLine("		}");
			sw.Write("\n");



			//定义Table的各个字段
			sw.WriteLine("\t\t#region 定义字段");
			sw.Write("\n");
			for(int i=0;i<dt.Rows.Count;i++)
			{
				int err = 0;
				SqlDbType sqlType = new SqlDbType();
				err = Getparam(dt.Rows[i]["type_name"].ToString().ToUpper(),out sqlType);
				if( err == -1)
				{
					MessageBox.Show("发生类型转换错误","错误",MessageBoxButtons.OK);
				}
				else
				{
					type = ChangeToCSharpType(sqlType.ToString().ToLower());
				}
				if(type	== "int" )
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2}	=  0 ;",type,"_n",dt.Rows[i]["column_name"].ToString());
				}
				else if(type	== "DateTime")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2};",type,"_dt",dt.Rows[i]["column_name"].ToString());
				}
				else if(type	== "bool")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2};",type,"_b",dt.Rows[i]["column_name"].ToString());

				}
				else if(type	== "decimal")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2}	= 0;",type,"_d",dt.Rows[i]["column_name"].ToString());

				}
				else if(type	== "text")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2}	=	\"\";","string","_s",dt.Rows[i]["column_name"].ToString());
				}
				else if(type	== "strings")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2}	=	\"\";","string","_s",dt.Rows[i]["column_name"].ToString());
				}
				else
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("		private {0} {1}{2}	=	\"\";",type,"_s",dt.Rows[i]["column_name"].ToString());
				}	
			}
			sw.Write("\n");
			sw.WriteLine("\t\t#endregion");

			sw.WriteLine("\r");

			

			//定义Table各字段属性
			sw.WriteLine("\t\t#region 定义属性");
			sw.WriteLine("");
			for(int i1=0;i1<dt.Rows.Count;i1++)
			{	
				int err = 0;
				SqlDbType sqlType = new SqlDbType();
				err = Getparam(dt.Rows[i1]["type_name"].ToString().ToUpper(),out sqlType);
				if( err == -1)
				{
					MessageBox.Show("发生类型转换错误","错误",MessageBoxButtons.OK);
				}
				else
				{
					type = ChangeToCSharpType(sqlType.ToString().ToLower());
				}

				if(type	== "int")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}",type,dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_n",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_n",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
				else if(type	== "DateTime")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}",type,dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_dt",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_dt",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
				else if(type	== "bool")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}",type,dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_b",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_b",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
				else if(type	== "decimal")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}",type,dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_d",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_d",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
				else if(type	== "text")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}","string",dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_s",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_s",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
				else if(type	== "strings")
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}","string",dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_s",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_s",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
				else
				{
					sw.WriteLine("\t\t/// <summary> ");
					sw.WriteLine("\t\t/// {0}   ",dt.Rows[i1]["ColumDescription"].ToString());
					sw.WriteLine("\t\t/// </summary>");
					sw.WriteLine("        public {0} {1}",type,dt.Rows[i1]["column_name"].ToString());
					sw.WriteLine("        {");
					sw.Write("			set{ ");
					sw.Write("{0}{1} = value;","_s",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.Write("			get{ ");
					sw.Write("return {0}{1};","_s",dt.Rows[i1]["column_name"].ToString());
					sw.Write("}\n");
					sw.WriteLine("        }");
				}
			}
			sw.WriteLine("");
			sw.WriteLine("\t\t#endregion");
			sw.WriteLine("\n");



			//在Table中根据主键查询的Sql语句		
			sw.WriteLine("\t\t#region 定义方法");
			sw.WriteLine("");
			
			sw.WriteLine("\t\t/// <summary> ");
			sw.WriteLine("\t\t///  根据主键查询的Sql语句");
			sw.WriteLine("\t\t/// </summary>");
			sw.WriteLine("\t\t/// <returns></returns>");

			sw.WriteLine("	    public override string GetSelectString()");
			sw.WriteLine("	    {");
			sw.WriteLine("		    return	\"SELECT * FROM " + tableName + " WHERE " + dt.Rows[0]["column_name"].ToString() + "=@" + dt.Rows[0]["column_name"].ToString() + "\";");
			sw.WriteLine("	    }");

			sw.WriteLine("	    public override void setSelectParameter(SqlParameterCollection sqlParmeterCollection)");
			sw.WriteLine("	    {");

			if( dt.Rows[0]["type_name"].ToString() == "int" )
			{
				sw.WriteLine("			sqlParmeterCollection.Add(\"@{0}\", SqlDbType.Int).Value		=this.{1}{2};",dt.Rows[0]["column_name"].ToString(),"_n",dt.Rows[0]["column_name"].ToString());
			}
			if( dt.Rows[0]["type_name"].ToString() == "string" || dt.Rows[0]["type_name"].ToString() == "strings")
			{
				sw.WriteLine("			sqlParmeterCollection.Add(\"@" + dt.Rows[0]["column_name"].ToString() + "\", SqlDbType.VarChar,"+dt.Rows[1]["length"].ToString()+").Value	 =	this._s" + dt.Rows[0]["column_name"].ToString() + ";");

			}
			sw.WriteLine("	    }");
			sw.Write("\n");



			//在Table中增加一条信息
			sw.WriteLine("\t\t/// <summary> ");
			sw.WriteLine("\t\t///  添加基本信息链接");
			sw.WriteLine("\t\t/// </summary>");
			sw.WriteLine("\t\t/// <returns>返回添加Sql语句</returns>");

			sw.WriteLine("        public override string GetInsertString()");
			sw.WriteLine("	    {");
			sw.WriteLine("			StringBuilder sbInsert = new StringBuilder(\"insert into " + tableName + "(\");");
			sw.Write("\n");
			for(int i1=1;i1<dt.Rows.Count;i1++)
			{
				if(i1	==	dt.Rows.Count-1)
				{
					sw.WriteLine("			sbInsert.Append(\" " + dt.Rows[i1]["column_name"].ToString() + "\");");
				}
				else
				{
					sw.WriteLine("			sbInsert.Append(\" " + dt.Rows[i1]["column_name"].ToString() + ",\");");
				}
			}
			sw.Write("\n");
			sw.WriteLine("			sbInsert.Append(\") values (\");");
			sw.Write("\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -