bitscn.com
以前很多时候都是使用codesmith或动软生成相关的数据库访问代码(不喜欢使用持久化框架),可以codesmith对中文的支持不好,新下载的6.5的版本,怎样都不能正确显示中文,动软代码生成器的表字段的注释永远都显示不了(李天平的败笔),有几个属性如mysql的smallint,tinyint,得重新更改选项的映射表,太麻烦了。虽然说动软现在提供了模块生成功能,批量生成中对表的重命名也不能够,功能有点弱呀,于是自己写个代码生成器,按自己的规则去生成,注释也出来了,多了注释提示,对开发帮助很大的。代码如下:
private void createmodel() { string connectionstring = server=127.0.0.1;port=3306;user id=root;password=root;database=ipbroadcast;connect timeout=60;treat tiny as boolean=false; mysql.data.mysqlclient.mysqlconnection conn = new mysql.data.mysqlclient.mysqlconnection(connectionstring); conn.open(); datatable table = conn.getschema(tables); foreach (datarow row in table.rows) { datatable sheettable = getschematable(conn, row); string tname = row[table_name].tostring(); createentityfile(conn, tname, sheettable); } conn.close(); } private datatable getschematable(mysqlconnection conn, datarow row) { string sheetname = row[table_name].tostring(); mysqlcommand com = conn.createcommand(); com.commandtype = commandtype.text; com.commandtext = select * from + sheetname; idatareader reader = com.executereader(commandbehavior.schemaonly); datatable table = reader.getschematable(); com.dispose(); reader.close(); return table; } private void createentityfile(mysqlconnection conn, string tname, datatable table) { //定义生成文件的路径 string tablename = tname; string schemaname = ; string colname = ; string path = @f:/ipbroadcastmodel/model1; if (!directory.exists(path)) directory.createdirectory(path); mysqlcommand com = conn.createcommand(); com.commandtype = commandtype.text; dictionary dict = new dictionary(); bool isgetcomment = false; //写文件 string clsname = ety + tablename.substring(3, 1).toupper() + tablename.substring(4); string filename = clsname + .cs; string fullname = path.combine(path, filename); using (filestream fs = new filestream(fullname, filemode.create)) { streamwriter sw = new streamwriter(fs, encoding.utf8); sw.writeline(using system;); sw.writeline(using system.text;); sw.writeline(using system.collections.generic; ); sw.writeline(using system.data;); sw.writeline(namespace aebell.dbmanager); sw.writeline({); sw.writeline(/t[serializable]); sw.writeline(/tpublic class + clsname); sw.writeline(/t{); foreach (datarow row in table.rows) { //tablename = row[basetablename].tostring(); colname = row[columnname].tostring(); schemaname = row[baseschemaname].tostring(); if (!isgetcomment) { isgetcomment = true; getfielcomment(tablename, schemaname, com, dict); } sw.writeline(/t/t/// ); sw.writeline(/t/t/// + dict[colname]); sw.writeline(/t/t/// ); type info = row[datatype] as type; string declear = info.name; if (declear.toupper() == sbyte) //为了适应动软。 declear = byte; bool isnull = (bool)row[allowdbnull]; if (isnull && info.basetype.name == valuetype) declear += ?; sw.writeline(/t/tpublic + declear + + colname.substring(0, 1).toupper() + colname.substring(1)); sw.writeline(/t/t{); sw.writeline(/t/t/tget;); sw.writeline(/t/t/tset;); sw.writeline(/t/t}); } sw.writeline(/t}); sw.writeline(}); sw.close(); } } private static void getfielcomment(string tablename, string schemaname, mysqlcommand com, dictionary dict) { string comment = ; com.commandtext = select column_name,data_type, column_comment from information_schema.columns where table_name =' + tablename + ' and table_schema = ' + schemaname + '; // and column_name like ' + colname + '; idatareader reader = com.executereader(); while (reader.read()) { string colname = reader.getstring(0); comment = reader.getstring(2); dict[colname] = comment; } reader.close(); }
虽然实现不是很严谨,dal和bll层也没有实现,但有需要之人是可以很快实现的。不对之处,请指正。
bitscn.com