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

📄 table.cs

📁 可以自动生成一些应用代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;


namespace test
{
   public  class Display
    {
        public String control;
        public String toValue;
    
    }
    
   public  class ConTraint
    {
        public String ConTraintType;//约束名
        public String ConTraintValue;//约束值
       
    
    }
    
   public  class Field
    {
        public String FieldName;
        public String FieldType;
       public bool isPrimaryKey=false ;
       public String FieldDescription;
        public ArrayList arrConstraints;
        public Display display;
      
        public Boolean Check( String value) 
        {
            foreach (ConTraint conTraint in arrConstraints)
            { 
               if (conTraint.ConTraintType.Equals ("isNull")  )
                {
                    if (value == null) return false;
                    
                }
                if (conTraint.ConTraintType.Equals("max"))
                {
                    return true;
                
                }
            
            }
            return true;
        }
    }
    public class Table
    {
        public string  TableName;
        public ArrayList Fields;
        public string  AccesFileName="";
        public string  PrimaryKey;
        public Table()
        {
            Fields = new ArrayList();
        }
        public Boolean  changeTableToObjectFile(String filePath,String nameSpace )
        { 
           string filename; 
           filename =filePath +"\\"+TableName+".cs";
           //测试文件是否已经存在
           if (File.Exists(filename ))
           {
               File.Delete(filename);
           }
           FileStream fs = new FileStream(filename, FileMode.CreateNew);
           StreamWriter sw = new StreamWriter(fs,Encoding.Default  );
           sw.WriteLine("using System; ");
           sw.WriteLine("using System.Collections.Generic;");
           sw.WriteLine("using System.Text;");

           sw.WriteLine("namespace  " + nameSpace);
           sw.WriteLine("{");
           sw.WriteLine("public class "+TableName );
           sw.WriteLine("{");
            

           foreach (Field  field in Fields )//遍历每一张表
           {
                
               String dataType = "";
               if (field.FieldType.Equals("130")) dataType = "string";
               if (field.FieldType.Equals("3")) dataType = " Int32";
               if (field.FieldType.Equals("4")) dataType = "float";
               if (field.FieldType.Equals("5")) dataType = "float";  //双精度
               if (field.FieldType.Equals("7")) dataType = "string";  //日期类型
               if (field.FieldType.Equals("2")) dataType = "int";

               sw.WriteLine(" public "+dataType +"  "+field.FieldName+";"  );
           }
           sw.WriteLine("public "+TableName +"(){ }");
           sw.WriteLine("}");
           sw.WriteLine("}");
           sw.Close();
           fs.Close();
           return true;
           
        
        }
        public Boolean changeTableToObjectAttributeFile(String filePath, String nameSpace)
        {
            string filename;
            filename = filePath + "\\" + TableName + ".cs";
            //测试文件是否已经存在
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            FileStream fs = new FileStream(filename, FileMode.CreateNew);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.WriteLine("using System; ");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Text;");

            sw.WriteLine("namespace  " + nameSpace);
            sw.WriteLine("{");
            sw.WriteLine("public class " + TableName);
            sw.WriteLine("{");

          
            foreach (Field field in Fields)//遍历每一字段
            {


                String dataType = "";
                if (field.FieldType.Equals("130")) dataType = "string";
                if (field.FieldType.Equals("3")) dataType = " Int32";
                if (field.FieldType.Equals("4")) dataType = "float";
                if (field.FieldType.Equals("5")) dataType = "float";  //双精度
                if (field.FieldType.Equals("7")) dataType = "string";  //日期类型
                if (field.FieldType.Equals("2")) dataType = "int";
                sw.WriteLine(" public " + dataType + "  _"+ field.FieldName + ";");
               //使用描述做属性
                //    sw.WriteLine(" public " + dataType + "  " + field.FieldDescription  + "");
                //使用变量明做属性
                sw.WriteLine(" public " + dataType + "  " + field.FieldName + "");

               
                sw.WriteLine ("{");
                sw.WriteLine (" get");
                sw.WriteLine ("{  ");
                sw.WriteLine ("  return  _"+ field.FieldName +";");
                sw.WriteLine (" }");
                sw.WriteLine (" set");
                sw.WriteLine (" {");
                sw.WriteLine ( " _"+ field.FieldName +" = value;");
                sw.WriteLine (" }");
                sw.WriteLine ("}");
        }
           
            sw.WriteLine("}");
            sw.WriteLine("}");
            sw.Close();
            fs.Close();
            return true;


        }


        public Boolean changeTableToObjectLocatorFile(String filePath, String nameSpace)
        {
            string filename;
            filename = filePath + "\\" + TableName + "Dao.cs";
            //测试文件是否已经存在
            if (File.Exists(filename))
            {
                File.Delete(filename );
                
            }
            FileStream fs = new FileStream(filename, FileMode.CreateNew);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.WriteLine("using System; ");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("using System.Data.OleDb;");
            sw.WriteLine("using System.Collections;");

            sw.WriteLine("namespace  " + nameSpace);
            sw.WriteLine("{");
            sw.WriteLine("public class " + TableName+"Dao");
            sw.WriteLine("{");
            //DATA_TYPE  
            //string   130
            //longint    3
            //float   4
            // int    2
            sw.WriteLine("OleDbConnection ConDB; ");
            sw.WriteLine("OleDbCommand DbCommand;");
            sw.WriteLine("OleDbDataReader DataReader;");
            sw.WriteLine("OleDbDataAdapter oleDbDataAdapter;");

            //创建构造函数
            sw.WriteLine("public  " +TableName +"Dao()");
            sw.WriteLine("{");
            sw.WriteLine(" ConDB = new OleDbConnection(\"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+"db.mdb" +"\");");
           // sw.WriteLine("ConDB.Open();");
            sw.WriteLine("}");


            //创建FindALl函数---------------------------------------------------------------------------------------


            sw.WriteLine("public ArrayList  FindAll" + TableName + "()");
            sw.WriteLine("{");
            sw.WriteLine(" String SQL;");
            sw.WriteLine("SQL = \"select * from " + TableName +"\";");
            sw.WriteLine("ArrayList Arr"+TableName+" = new ArrayList();");
            sw.WriteLine("DbCommand = new OleDbCommand(SQL, ConDB);");
            sw.WriteLine("DbCommand.Connection.Open();");
            sw.WriteLine("DataReader = DbCommand.ExecuteReader();");
            sw.WriteLine("while (DataReader.Read())");
            sw.WriteLine("{");
            sw.WriteLine(TableName +" "+TableName +" = new "+TableName +"();");
            int i = 0;
            foreach (Field field in Fields)//遍历每一个字段
            {

                String dataType = "";

                //student.age = (int.Parse)(DataReader["age"].ToString());

                sw.WriteLine(  "if (DataReader[\""+ field.FieldName+"\"] != DBNull.Value)");
                if (field.FieldType.Equals("130") || field.FieldType.Equals("7")) //dataType = "String";
                {
                    sw.WriteLine(TableName + "." + field.FieldName + "=" + "DataReader[\"" + field.FieldName  + "\"].ToString();");

                }
               
                if (field.FieldType.Equals("3")) //dataType = "long int ";
                {
                    sw.WriteLine(TableName + "." + field.FieldName + "=" + "Int32.Parse(DataReader[\"" + field.FieldName + "\"].ToString());");

                }
                if (field.FieldType.Equals("4") || field.FieldType.Equals("5")) //dataType = "float 4";
                {
                    sw.WriteLine(TableName + "." + field.FieldName + "=" + " float.Parse (DataReader[\"" + field.FieldName + "\"].ToString());");

                }

⌨️ 快捷键说明

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