Exemple #1
0
 public static void create_db_tab()
 {
     MyDatabase domain = new MyDatabase();
         domain.DbName = "管理";
         domain.TableName = "admin";
         Dictionary<string, string> columns = new Dictionary<string, string>();
             columns.Add("username", "varchar(50)");
             columns.Add("upass", "varchar(50)");
             columns.Add("级别", "int");
             domain.Columns = columns;
     AccessOp.CreateAccessDb(domain.DbName);
     AccessOp.CreateAccessTab(domain);
 }
Exemple #2
0
 public static void test_3()
 {
     MyDatabase domain = new MyDatabase();
     domain.DbName = "最烂的是C#";
     Console.WriteLine(domain.DbName);
     Console.ReadLine();
 }
Exemple #3
0
        // 创建表
        //  通过遍历map对象,获取字段名称,以及字段类型
        public static bool CreateAccessTab(MyDatabase domain)
        {
            // get the database value
            string dbName = domain.DbName;
            string tableName = domain.TableName;
            Dictionary<string, string> columns = domain.Columns;
            // end get the database value

            // the head sql
            string tabSql = "create table ?(id int not null identity(1,1) primary key";
            // make suer the table name
            tabSql = tabSql.Replace("?", tableName);
            // the end ")"
            string end = ")";

            foreach(KeyValuePair<string, string> entry in columns)
            {
                string column_name = entry.Key;
                string column_prop = entry.Value;

                string catSql = ", " + column_name + " " + column_prop + " not null";
              //  catSql = catSql.Replace("?", column_name).Replace("?", column_prop);

                tabSql = tabSql + catSql;
            }
            // the created SQL has finished
            tabSql = tabSql + end;

            // connect the db
            OleDbConnection con = AccessOp.getCon(dbName);
            OleDbCommand cmd = con.CreateCommand();
                cmd.Connection = con;
                cmd.CommandText = tabSql;

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (SystemException ex)
                {
                    return false;
                }

                // close the con
                AccessOp.closeAll(con, cmd, null);

            return true;
        }