最重要的是,这里的代码针对所有的数据库数据源都是相同的写法。我们想方设法地消除了数据库之间的差异性。
a.新增表
C# |
Database db=Proj.SysDataFactory["UserDB"]; string strTableName = "TestTable"; TableBuilder tb = new TableBuilder(db, strTableName,false); Col col1 = new Col("FSzTableID", tb); col1.DataType = ChineseDataTypeEnum.备注.ToString(); tb.Columns.Add(col1); //添加字符列 Col col3 = new Col("FSsTableName", tb); col3.DataType = ChineseDataTypeEnum.字符.ToString(); col3.MaxLength = 36; tb.Columns.Add(col3); //添加逻辑列 Col col4 = new Col("逻辑列", tb); col4.DataType = ChineseDataTypeEnum.逻辑.ToString(); tb.Columns.Add(col4); //添加自增长列\锁定列\排序列三列特殊列 tb.AutoAddSpecialCols = true; //获得生成新表 string strCMD = tb.GetCreateTableSQL(); //开始运行生成新表的SQL语句 try { int intCount=db.ExecuteNonQuery(strCMD, true); if(intCount>0) { MessageBox.Show("创建表成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } } catch (Exception ex) { UnhandledExceptionManager.ShowAndSaveLog(ex); } |
VB.Net |
|
b.修改表
C# |
Database db = Proj.SysDataFactory("UserDB"); string strTableName = "TestTable"; // 当修改表结构模式时,第三个参数一定要设置为True TableBuilder tb = new TableBuilder(db, strTableName, true); tb.Columns("FSzTableID").DataType = ChineseDataTypeEnum.字符.ToString(); try { tb.Save(); } catch (Exception ex) { UnhandledExceptionManager.ShowAndSaveLog(ex); } |
VB.Net |
Dim db As Database = Proj.SysDataFactory("UserDB") Dim strTableName As String = "TestTable" '当修改表结构模式时,第三个参数一定要设置为True Dim tb As TableBuilder = New TableBuilder(db, strTableName, True) tb.Columns("FSzTableID").DataType=ChineseDataTypeEnum.字符.ToString() Try tb.Save() Catch ex As Exception UnhandledExceptionManager.ShowAndSaveLog(ex) End Try |
c.删除表
C# |
Database db = Proj.SysDataFactory("UserDB"); string strTableName = "TestTable"; // 当修改表结构模式时,第三个参数一定要设置为True TableBuilder tb = new TableBuilder(db, strTableName); try { tb.DropTable(true); } catch (Exception ex) { UnhandledExceptionManager.ShowAndSaveLog(ex); } |
VB.Net |
Dim db As Database = Proj.SysDataFactory("UserDB") Dim strTableName As String = "TestTable" Dim tb As TableBuilder = New TableBuilder(db, strTableName) Try tb.DropTable(True) Catch ex As Exception UnhandledExceptionManager.ShowAndSaveLog(ex) End Try |