public void Covert(Sqlite2CsConfig config) { CleanupOutput(config.outputPath); SQLiteConnectionStringBuilder sqlitestring = new SQLiteConnectionStringBuilder(); sqlitestring.DataSource = config.databaseFilePath; mSqliteConn = new SQLiteConnection(sqlitestring.ToString()); mSqliteConn.Open(); //mSqliteConn.StateChange += MSqliteConn_StateChange; Dictionary <string, string> tableInfoMap = GetTableInfo(config.databaseName, config.filterName); foreach (KeyValuePair <string, string> tableInfo in tableInfoMap) { CSFileWriter csFile = ConvertTable(config.databaseName, tableInfo.Key, tableInfo.Value, config.CSdataClassImpledInterface); string filePath = config.outputPath + "/" + tableInfo.Key + ".cs"; if (File.Exists(filePath)) { Console.WriteLine("{0} already exists.", filePath); return; } using (StreamWriter sw = File.CreateText(filePath)) { sw.Write(csFile.GenerateFilsString()); sw.Close(); } } mSqliteConn.Close(); System.Windows.Forms.MessageBox.Show("写入完毕!!!"); }
CSFileWriter ConvertTable(string dBName, string tableName, string comment, string[] impledInterface) { CSFileWriter writer = new CSFileWriter(); string sqlString = string.Format("PRAGMA table_info([{0}]);", tableName); using (SQLiteCommand cmd = mSqliteConn.CreateCommand()) { cmd.CommandText = sqlString; //using writer.AddReferencedLib("System"); CSClass classObj = writer.AddCSClass("public", tableName, impledInterface); SQLiteDataAdapter da = new SQLiteDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); DataColumn nameCol = dt.Columns["name"]; DataColumn typeCol = dt.Columns["type"]; CSParseFunc parseFunc = new CSParseFunc("public", tableName, "ReadData"); CSVar dataForm = parseFunc.AddParam(string.Empty, "ASteinGameDataHolder", "dataForm"); classObj.AddFuncDeclare(parseFunc); foreach (DataRow row in dt.Rows) { CSField dataVar = classObj.AddVar("public", SqliteType2CsType(row[typeCol].ToString()), row[nameCol].ToString()); CSFuncCall parseCall = new CSFuncCall(string.Empty, SqliteType2CsType(row[typeCol].ToString()), SqliteType2CsReadType(row[typeCol].ToString())); parseCall.AddParam("string", row[nameCol].ToString()); parseCall.targetObj = dataForm; parseCall.returnVar = dataVar; parseFunc.AddParseCall(parseCall); } } //System.Diagnostics.Trace.WriteLine(writer.GenerateFilsString()); return(writer); }