using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using SysBaseLibs; using SysDataLibs; //using WinRunTimeControls; namespace CreateTableColumnFile { public partial class frmMain : Form { string _filePath = ".\\codes\\"; private SetProperties _properties = null; private DBConnSql _DBConn = null; public frmMain() { InitializeComponent(); Initialize(); } // 测试数据库连接 private void btTestConn_Click(object sender, EventArgs e) { try { if (_DBConn == null) _DBConn = new DBConnSql(); if (_DBConn.IsOpened) _DBConn.Close(); _DBConn.OnDBConnectionError += new evDBConnectionError(_DBConn_OnDBConnectionError); _DBConn.Open(_properties.GetConnString()); if (_DBConn.IsOpened) { ThreadLog.LogInfo("连接成功!"); } else ThreadLog.LogInfo("连接失败!"); } catch (Exception err) { MessageBox.Show(err.Message); return; } } void _DBConn_OnDBConnectionError(string pcCommand, string pcMessage, string pcErrorNumbers) { ThreadLog.LogErr("Command:" + pcCommand); ThreadLog.LogErr("Message:" + pcMessage); ThreadLog.LogErr("ErrorMessage:" + pcErrorNumbers); } private void CreateTableColumnList(List poTables) { string lcTCList = ""; foreach (rsIdName idname in poTables) { rsQuery loTableCols = GetColumnsInfoByTableName(idname.Id); if (loTableCols != null && loTableCols.IsOpened) { string lcTableColList = ""; loTableCols.MoveFirst(); for (int i = 0; i < loTableCols.RecCount; i++) { lcTableColList += (lcTableColList == "" ? "" : ",") + loTableCols.GetString("ColumnId").Trim(); loTableCols.MoveNext(); } lcTableColList = "===========================" + idname.Id + "======================\r\n" + " " + lcTableColList + " \r\n \r\n"; lcTCList += lcTableColList; lcTableColList = ""; } } WriteStringToFile(lcTCList, _filePath + "Columnslist.cs", false); } #region About CSharpCode Motheds /// /// 创建所有的表名常数类 /// /// private void CreatTablesList(List poTables) { ThreadLog.LogInfo("正在创建文件……"); SysDataLibs.TableClass.Sys_Tables_Qry loTables = new SysDataLibs.TableClass.Sys_Tables_Qry(); loTables.OpenFromDataTable(_DBConn.OpenDataTable(" Select * from Sys_Tables ")); string lcWriteStr = ""; lcWriteStr += "using System;\r\n"; lcWriteStr += "using System.Text;\r\n"; lcWriteStr += "\r\n"; lcWriteStr += "namespace " + _properties.NameSpace + " \r\n"; lcWriteStr += "{\r\n"; lcWriteStr += " public class " + _properties.TablesClassName + " \r\n"; lcWriteStr += " {\r\n"; ThreadLog.LogInfo("产生所有的表名……"); int liCount = poTables.Count; int li = 0; foreach (rsIdName idname in poTables) { if (loTables.GoToRecordByFieldsAndValues(SysDataLibs.TableClass.Sys_Tables_info.cTableId, idname.Id)) { lcWriteStr += " /// \r\n"; lcWriteStr += " /// " + loTables.TableName + " ( Table Type:" + loTables.SysType + "; IsInMemory:" + loTables.IsInMemory + " ) \r\n"; lcWriteStr += " /// \r\n"; } lcWriteStr += " public const string " + idname.Id + " = \"" + idname.Id + "\";\r\n"; ThreadLog.LogInfo("产生表" + idname); li++; } lcWriteStr += " }\r\n"; lcWriteStr += "}\r\n"; WriteStringToFile(lcWriteStr, _filePath + "Tn.cs", false); } public string GetDefaultSQLStr(string pcDefault, string pcDataType) { string text1 = "'\"+" + pcDefault + "+\"'"; pcDataType = UtilStr.UAndT(pcDataType); if (pcDataType == "INT" || pcDataType == "NUMERIC" || pcDataType == "MONEY") { text1 = "\"+" + pcDefault + "+\""; } return text1; } #endregion #region Motheds private void Initialize() { try { _properties = new SetProperties(); this.propertyGrid1.SelectedObject = _properties; ThreadLog.LogInfoEvnet += new evLogInfo(ThreadLog_LogInfoEvnet); if (!Directory.Exists(_filePath)) { Directory.CreateDirectory(_filePath); } if (!Directory.Exists(_filePath+"Classes\\")) { Directory.CreateDirectory(_filePath + "Classes\\"); } } catch (Exception er) { ThreadLog.LogException(er); } } //GetAllTableNames() private List GetAllTableNames() { List loRetVal = new List(); rsQuery loQuery = new rsQuery(); string lcSql = " select name,type from sysobjects where type in('u','V') and name<>'sysdiagrams' order by type ,name "; //SELECT DISTINCT TableId FROM TableColumns // string lcSql = " select name from sysobjects where type in('u','V') and name not in (SELECT DISTINCT TableId FROM TableColumns) order by type "; //SELECT DISTINCT TableId FROM TableColumns loQuery.Open(new DataSet(), "AllTable", lcSql, _DBConn); if (loQuery != null && loQuery.IsOpened) { loQuery.MoveFirst(); loRetVal.Clear(); for (int i = 0; i < loQuery.RecCount; i++) { loRetVal.Add(new rsIdName(loQuery.GetString("name"), loQuery.GetString("name") + "(" + loQuery.GetString("type") + ")")); loQuery.MoveNext(); } // loRetVal.Sort(); } return loRetVal; } /// /// 2005 /// /// /// private rsQuery GetColumnsInfoByTableName(string pcTableId) { string lcSql = "Select * from Sys_TableColumns where TableId='" + pcTableId + "'"; rsQuery loQuery = new rsQuery(); loQuery.Open(new DataSet(), pcTableId, lcSql, _DBConn); if (loQuery != null && loQuery.IsOpened) { loQuery.SortBy("ColumnId"); loQuery.MoveFirst(); return loQuery; } return null; } // 2000 // private rsQuery GetColumnsInfoByTableName(string pcTableId) // { // string lcSql = @" SELECT // a.name ColumnId, // (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 'Y'else 'N' end) AutoGen, // (case when (SELECT count(*) // FROM sysobjects // WHERE (name in // (SELECT name // FROM sysindexes // WHERE (id = a.id) AND (indid in // (SELECT indid // FROM sysindexkeys // WHERE (id = a.id) AND (colid in // (SELECT colid // FROM syscolumns // WHERE (id = a.id) AND (name = a.name))))))) AND // (xtype = 'PK'))>0 then 'Y' else 'N' end) PrimaryKey, // b.name DataType, // a.length CharNum, // COLUMNPROPERTY(a.id,a.name,'PRECISION') as Length, // isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as DotNum, // (case when a.isnullable=1 then 'Y'else 'N' end) AllowNull, // isnull(e.text,'') DefValue, // isnull(g.[value],'') AS ColDesc // FROM syscolumns a left join systypes b // on a.xtype=b.xusertype // inner join sysobjects d // on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' and d.name = '" + pcTableId + @"' // left join syscomments e // on a.cdefault=e.id // left join sysproperties g // on a.id=g.id AND a.colid = g.smallid // order by a.id,a.colorder "; // rsQuery loQuery = new rsQuery(); // loQuery.Open(new DataSet(), pcTableId, lcSql, _DBConn); // if (loQuery != null && loQuery.IsOpened) // { // loQuery.SortBy("ColumnId"); // loQuery.MoveFirst(); // return loQuery; // } // return null; // } //private void WriteStringToFile(string pcMsg) //{ // WriteStringToFile(pcMsg, _filePath, false); //} //private void WriteStringToFile(string pcMsg, string filepath) //{ // WriteStringToFile(pcMsg, filepath, false); //} private void WriteStringToFile(string pcMsg, string filepath, bool append) { try { StreamWriter writer1 = new StreamWriter(filepath, append, Encoding.UTF8); ThreadLog.LogInfo("Write:" + filepath); writer1.Write(pcMsg); writer1.Flush(); writer1.Close(); } catch (Exception exception1) { ThreadLog.LogException(exception1); } } #endregion public List SelectedTables() { List loRetVal = new List(); foreach (rsIdName idname in this.lbDesc.Items) { loRetVal.Add(idname); } return loRetVal; } /// /// 创建数据库记录插入语句 /// /// /// private void btCreate_Click(object sender, EventArgs e) { List loTables = SelectedTables(); StringBuilder loOutStr = new StringBuilder(); //string lcOutStr = ""; foreach (rsIdName idName in loTables) { string lcTn = idName.Id; rsQuery loColsInfo = GetColumnsInfoByTableName(lcTn); string lcSql = "select * from " + lcTn; rsQuery loQuery = new rsQuery(); loQuery.Open(new DataSet(), lcTn, lcSql, _DBConn); if (loQuery != null && loColsInfo != null && loColsInfo.IsOpened && loQuery.IsOpened) { string lcSingleStr = ""; loQuery.MoveFirst(); for (int i = 0; i < loQuery.RecCount; i++) { loColsInfo.MoveFirst(); string lcCols = ""; string lcValues = ""; for (int k = 0; k < loColsInfo.RecCount; k++) { string lcDataType = loColsInfo.GetString("DataType"); string lcColumnId = loColsInfo.GetString("ColumnId"); //string lcColValue = GetDefaultSQLStr(lcColumnId, lcDataType); string lcColValue = loQuery.GetString(lcColumnId); lcColValue = lcColValue.Replace("'", "''"); if (!(lcDataType == "int" || lcDataType == "money")) lcColValue = "'" + lcColValue + "'"; lcCols += (lcCols == "" ? "" : ",") + lcColumnId; lcValues += (lcValues == "" ? "" : ",") + lcColValue; loColsInfo.MoveNext(); } lcSingleStr += " insert into " + lcTn + " (" + lcCols + ") values (" + lcValues + ") \r\n"; loQuery.MoveNext(); } loOutStr.Append("/* ============================================================ */ \r\n"); loOutStr.Append("/* Table: " + lcTn + " */ \r\n"); loOutStr.Append("/* ============================================================ */ \r\n"); loOutStr.Append(lcSingleStr); } } WriteStringToFile(loOutStr.ToString(), _filePath + "SqlRecords.sql", false); } private void btLoadTables_Click(object sender, EventArgs e) { List loTables = GetAllTableNames(); this.lbSource.Items.Clear(); this.lbDesc.Items.Clear(); foreach (rsIdName idname in loTables) { this.lbSource.Items.Add(idname); } } private void btCreatClass_Click(object sender, EventArgs e) { List loTables = SelectedTables(); //string lcOutStr = ""; foreach (rsIdName idName in loTables) { string lcTn = idName.Id; rsQuery loColsInfo = GetColumnsInfoByTableName(lcTn); if (loColsInfo != null && loColsInfo.IsOpened && loColsInfo.RecCount > 0) { StringBuilder loOutStr = new StringBuilder(); loOutStr.Append("using System; \r\n"); loOutStr.Append("using System.Collections.Generic; \r\n"); loOutStr.Append("using System.Text; \r\n"); loOutStr.Append("using System.Data; \r\n"); loOutStr.Append("using SysBaseLibs; \r\n"); loOutStr.Append("using System.Web.Script.Serialization;\r\n"); loOutStr.Append(" namespace SysDataLibs.TableClass \r\n"); loOutStr.Append(" { \r\n"); loOutStr.Append(" #region " + lcTn + " \r\n"); loOutStr.Append(" public class " + lcTn.Trim() + "_info : ITableInfo \r\n"); loOutStr.Append(" { \r\n"); //添加表字段信息 loOutStr.Append(" #region " + lcTn + "表 字段信息 \r\n"); for (int i = 0; i < loColsInfo.RecCount; i++) { loOutStr.Append(" /// \r\n"); loOutStr.Append(" /// 为关键字段: " + loColsInfo.GetString("PrimaryKey") + " " + (loColsInfo.GetBool("PrimaryKey") ? "--- PrimaryKey" : "") + ";\r\n"); loOutStr.Append(" /// 自动增长: " + loColsInfo.GetString("AutoGen") + ";\r\n"); loOutStr.Append(" /// 数据类型: " + loColsInfo.GetString("DataType") + ";\r\n"); loOutStr.Append(" /// 数据长度: " + loColsInfo.GetString("CharNum") + ";\r\n"); loOutStr.Append(" /// 是否允许为空: " + loColsInfo.GetString("AllowNull") + ";\r\n"); loOutStr.Append(" /// 默认值: " + loColsInfo.GetString("DefValue") + ";\r\n"); loOutStr.Append(" /// 描述: " + loColsInfo.GetString("ColDesc") + ";\r\n"); loOutStr.Append(" /// \r\n"); loOutStr.Append(" public const string c" + loColsInfo.GetString("ColumnId").Trim() + " = \"" + loColsInfo.GetString("ColumnId") + "\";\r\n"); loOutStr.Append("\r\n"); loColsInfo.MoveNext(); } loOutStr.Append(" #endregion \r\n"); loOutStr.Append(" public " + lcTn.Trim() + "_info(){} \r\n \r\n"); loOutStr.Append(" public " + lcTn.Trim() + "_info(DataRow poRow) \r\n"); loOutStr.Append(" { \r\n"); loOutStr.Append(" CreateTableInfo(poRow); \r\n"); loOutStr.Append(" } \r\n \r\n"); loOutStr.Append(" public void CreateTableInfo(DataRow poRow) \r\n"); loOutStr.Append(" { \r\n"); loColsInfo.MoveFirst(); for (int k = 0; k < loColsInfo.RecCount; k++) { string lcDataType = loColsInfo.GetString("DataType"); string lcColumnId = loColsInfo.GetString("ColumnId").Trim(); loOutStr.Append(" _" + lcColumnId + "=UtilStr.StrFromObj(poRow[c" + lcColumnId.Trim() + "]); \r\n"); loColsInfo.MoveNext(); } loOutStr.Append(" } \r\n \r\n"); string lcWhere1 = ""; string lcPanduan = ""; string lcKeys = ""; loColsInfo.MoveFirst(); for (int k = 0; k < loColsInfo.RecCount; k++) { if (loColsInfo.GetBool("PrimaryKey")) { // string lcDataType = loColsInfo.GetString("DataType"); string lcColumnId = loColsInfo.GetString("ColumnId").Trim(); lcKeys += (lcKeys == "" ? "" : ",") + " string pc" + lcColumnId.Trim(); lcWhere1 += (lcWhere1 == "" ? "" : " and ") + lcColumnId + "='\"+pc" + lcColumnId.Trim() + "+\"'"; lcPanduan += (lcPanduan == "" ? "" : "&&") +" pc"+ lcColumnId + ".Trim().Length > 0"; } loColsInfo.MoveNext(); } if (lcKeys.Trim().Length > 0) { loOutStr.Append(" public " + lcTn + "_info(" + lcKeys + ", DBConnSql poDBConn) \r\n "); loOutStr.Append(" { \r\n "); loOutStr.Append(" if (" + lcPanduan + " && poDBConn != null) \r\n "); loOutStr.Append(" { \r\n "); loOutStr.Append(" string lcSql = \"select * from \" + Tn." + lcTn + " + \" where " + lcWhere1 + "\"; \r\n"); loOutStr.Append(" rsQuery loQuery = poDBConn.OpenQuery(lcSql); \r\n"); loOutStr.Append(" if (loQuery != null && loQuery.IsOpened && loQuery.RecCount == 1) \r\n"); loOutStr.Append(" { \r\n"); loOutStr.Append(" loQuery.MoveFirst(); \r\n"); loOutStr.Append(" CreateTableInfo(loQuery.CurrentRow); \r\n"); loOutStr.Append(" } \r\n"); loOutStr.Append(" } \r\n"); loOutStr.Append(" } \r\n"); } loColsInfo.MoveFirst(); for (int k = 0; k < loColsInfo.RecCount; k++) { string lcDataType = loColsInfo.GetString("DataType"); string lcColumnId = loColsInfo.GetString("ColumnId").Trim(); loOutStr.Append(" private string _" + lcColumnId + "=\"\"; \r\n"); loOutStr.Append(" public string " + lcColumnId + " \r\n"); loOutStr.Append(" { \r\n"); loOutStr.Append(" get { return _" + lcColumnId + "; } \r\n"); loOutStr.Append(" set { _" + lcColumnId + " = value; } \r\n"); loOutStr.Append(" } \r\n \r\n"); loColsInfo.MoveNext(); } loOutStr.Append(" [ScriptIgnore] \r\n"); loOutStr.Append(" public rsXmlNode DataXMLNode \r\n"); loOutStr.Append(" { \r\n "); loOutStr.Append(" get \r\n "); loOutStr.Append(" { \r\n "); loOutStr.Append(" rsXmlNode loMainNode = new rsXmlNode(\"" + lcTn + "Record\", \"\"); \r\n "); loOutStr.Append(" rsXmlNode loNode =null; \r\n "); loColsInfo.MoveFirst(); for (int k = 0; k < loColsInfo.RecCount; k++) { loOutStr.Append(" loNode = new rsXmlNode(c" + loColsInfo.GetString("ColumnId").Trim() + ", " + loColsInfo.GetString("ColumnId").Trim() + "); \r\n "); loOutStr.Append(" loMainNode.AddChild(loNode); \r\n "); loColsInfo.MoveNext(); } loOutStr.Append(" return loMainNode ; \r\n "); loOutStr.Append(" } \r\n "); loOutStr.Append(" } \r\n "); ///=================================== string lcCols = ""; string lcValues = ""; string lcUpdateColValue = ""; string lcWhere = ""; loColsInfo.MoveFirst(); for (int k = 0; k < loColsInfo.RecCount; k++) { string lcDataType = loColsInfo.GetString("DataType"); string lcColumnId = loColsInfo.GetString("ColumnId").Trim(); string lcColValue = "_" + lcColumnId.Trim(); if (lcDataType == "int" || lcDataType == "money") lcColValue = "\"+" + lcColValue + "+\""; else lcColValue = "'\"+" + lcColValue + "+\"'"; if (!loColsInfo.GetBool("AutoGen")) { lcCols += (lcCols == "" ? "" : ",") + "\"+c" + lcColumnId + "+\""; lcValues += (lcValues == "" ? "" : ",") + lcColValue; } if (loColsInfo.GetBool("PrimaryKey")) { lcWhere += (lcWhere == "" ? "" : " and ") + "\"+c" + lcColumnId + "+\"" + "=" + lcColValue; } else { lcUpdateColValue += (lcUpdateColValue == "" ? "" : ",") + "\"+c" + lcColumnId + "+\"" + "=" + lcColValue; } loColsInfo.MoveNext(); } string lc_Sql = " insert into \"+Tn." + lcTn + "+\" \"+ \r\n \" (" + lcCols + ") \"+ \r\n \" values (" + lcValues + ") "; string lcInsert = " public string InsertSql() \r\n"; lcInsert += " { \r\n"; lcInsert += " return \"" + lc_Sql + "\" ; \r\n"; lcInsert += " } \r\n \r\n"; loOutStr.Append(lcInsert); lc_Sql = " update \"+Tn." + lcTn + "+\" \"+ \r\n \" set " + lcUpdateColValue + " \"+ \r\n \" where " + lcWhere; string lcUpdate = " public string UpdateSql() \r\n"; lcUpdate += " { \r\n"; lcUpdate += " return \"" + lc_Sql + "\" ; \r\n"; lcUpdate += " } \r\n \r\n"; loOutStr.Append(lcUpdate); lc_Sql = "Delete \"+Tn." + lcTn + "+\" where " + lcWhere; string lcDelete = " public string DeleteSql() \r\n"; lcDelete += " { \r\n"; lcDelete += " return \"" + lc_Sql + "\" ; \r\n"; lcDelete += " } \r\n"; loOutStr.Append(lcDelete); loOutStr.Append(" } \r\n"); loOutStr.Append(" #endregion \r\n\r\n"); //======================================= loOutStr.Append(" #region \r\n\r\n"); loOutStr.Append(" public class " + lcTn.Trim() + "_Qry : rsQuery \r\n"); loOutStr.Append(" { \r\n"); loColsInfo.MoveFirst(); for (int k = 0; k < loColsInfo.RecCount; k++) { string lcDataType = loColsInfo.GetString("DataType"); rsDataType dtype = new rsDataType(lcDataType); dtype.ColPrecision = loColsInfo.GetInt("ColPrecision"); dtype.ColLength = loColsInfo.GetInt("CharNum"); dtype.ColDecimal = loColsInfo.GetInt("ColDecimal"); string lcColumnId = loColsInfo.GetString("ColumnId").Trim(); //lcOutStr += " private string _" + lcColumnId + "=\"\"; \r\n"; loOutStr.Append(" public " + (dtype.CSharpType == null ? "string" : dtype.CSharpType.Name) + " " + lcColumnId + " \r\n"); loOutStr.Append(" { \r\n"); loOutStr.Append(" get { return " + dtype.rsQueryMethod + "(" + lcTn.Trim() + "_info.c" + lcColumnId.Trim() + "); } \r\n"); loOutStr.Append(" // set { SetField(" + lcTn.Trim() + "_info.c" + lcColumnId.Trim() + ", value); } \r\n"); loOutStr.Append(" } \r\n \r\n"); loColsInfo.MoveNext(); } loOutStr.Append(" } \r\n"); loOutStr.Append(" #endregion \r\n\r\n"); loOutStr.Append("} \r\n"); //ThreadLog.LogInfo(loOutStr.ToString()); WriteStringToFile(loOutStr.ToString(), _filePath + "Classes\\" + lcTn.Trim() + "_info.cs", false); } } } private void btCreateTableList_Click(object sender, EventArgs e) { CreatTablesList(GetAllTableNames()); } private void btLeftToRight_Click(object sender, EventArgs e) { while (lbSource.SelectedItems.Count > 0) { this.lbDesc.Items.Add(lbSource.SelectedItems[0]); this.lbSource.Items.Remove(lbSource.SelectedItems[0]); } } private void btRightToLeft_Click(object sender, EventArgs e) { while (lbDesc.SelectedItems.Count > 0) { this.lbSource.Items.Add(lbDesc.SelectedItems[0]); this.lbDesc.Items.Remove(lbDesc.SelectedItems[0]); } } private void lbSource_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.A) { for (int i = 0; i < this.lbSource.Items.Count; i++) this.lbSource.SetSelected(i, true); } } void SetMsg(double pcMsg) { SetMsg(pcMsg + ""); } void SetMsg(int pcMsg) { SetMsg(pcMsg + ""); } void SetMsg(string pcMsg) { if (richTextBox1.IsHandleCreated) richTextBox1.BeginInvoke((MethodInvoker)delegate() { SetMsgToTextBox(pcMsg); }); } void SetMsgToTextBox(string pcMsg) { richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss.fff") + " --- " + pcMsg + "\r\n"); richTextBox1.ScrollToCaret(); } void ThreadLog_LogInfoEvnet(string logInfo, MsgType poMsgType) { SetMsg(poMsgType.ToString() + " " + logInfo); } private void btTableColumns_Click(object sender, EventArgs e) { CreateTableColumnList(GetAllTableNames()); } private void btnTables_Click(object sender, EventArgs e) { // string lcSql = " select name,type from sysobjects where type in('u','V','P') and name not in (select Tableid from sys_tables) and name<>'sysdiagrams' order by type ,name "; rsQuery query = _DBConn.OpenQuery(lcSql); if (query != null && query.IsOpened && query.RecCount>0) { query.MoveFirst(); do { string name = query.GetString("name"); string type = query.GetString("type"); lcSql += " insert sys_tables (TableId,TableName,TableDesc,TableType,IsInMemory,IsCreate,SysType,Remark)values('" + name + "','" + name + "','" + name + "','U','N','N','" + type + "','') \r\n"; } while (query.Next()); if (_DBConn.ExcuteSqlTran(lcSql)) { SetMsg(lcSql); MessageBox.Show("执行成功"); } } } private void btnTableColumns_Click(object sender, EventArgs e) { // string lcSql = " select name,type from sysobjects where type in('u','V') and name not in (select distinct tableid from Sys_tablecolumns) and name<>'sysdiagrams' order by type ,name "; rsQuery query = _DBConn.OpenQuery(lcSql); if (query != null && query.IsOpened && query.RecCount > 0) { query.MoveFirst(); do { lbDesc.Items.Add(new rsIdName(query.GetString("name"), query.GetString("name") + "(" + query.GetString("type") + ")")); } while (query.Next()); } } private void btCreateTC_Click(object sender, EventArgs e) { foreach (rsIdName idname in this.lbDesc.Items) { IdCollection loColl = new IdCollection(); SqlSPPar sqlP = new SqlSPPar("TableID", SqlDbType.VarChar); sqlP.ParaLength = 50; sqlP.ParameterValue = idname.Id; loColl.Add(sqlP); if (_DBConn.ExecuteStoredProc("sp_UpdateTableColumns_2005", loColl)) { SetMsg(idname.Id + "执行 sp_UpdateTableColumns_2005 成功"); } } } private void button1_Click(object sender, EventArgs e) { List loTables = SelectedTables(); StringBuilder loOutStr = new StringBuilder(); //string lcOutStr = ""; foreach (rsIdName idName in loTables) { string lcTn = idName.Id; rsQuery loColsInfo = GetColumnsInfoByTableName(lcTn); string lcSql = "select * from " + lcTn; rsQuery loQuery = new rsQuery(); loQuery.Open(new DataSet(), lcTn, lcSql, _DBConn); if (loQuery != null && loColsInfo != null && loColsInfo.IsOpened && loQuery.IsOpened) { string lcSingleStr = ""; loQuery.MoveFirst(); for (int i = 0; i < loQuery.RecCount; i++) { loColsInfo.MoveFirst(); string lcSet = ""; string lcWhere = ""; for (int k = 0; k < loColsInfo.RecCount; k++) { string lcDataType = loColsInfo.GetString("DataType"); string lcColumnId = loColsInfo.GetString("ColumnId"); //string lcColValue = GetDefaultSQLStr(lcColumnId, lcDataType); string lcColValue = loQuery.GetString(lcColumnId); lcColValue = lcColValue.Replace("'", "''"); if (!(lcDataType == "int" || lcDataType == "money")) lcColValue = "'" + lcColValue + "'"; if (loColsInfo.GetBool("PrimaryKey")) { lcWhere += (lcWhere == "" ? "" : " and ") + (lcColumnId + "=" + lcColValue); } else lcSet += (lcSet == "" ? "" : ",") + (lcColumnId + "=" + lcColValue); loColsInfo.MoveNext(); } lcSingleStr += " Update " + lcTn + " set " + lcSet + " where " + lcWhere + " \r\n"; loQuery.MoveNext(); } loOutStr.Append("/* ============================================================ */ \r\n"); loOutStr.Append("/* Table update: " + lcTn + " */ \r\n"); loOutStr.Append("/* ============================================================ */ \r\n"); loOutStr.Append(lcSingleStr); } } WriteStringToFile(loOutStr.ToString(), _filePath + "SqlUpdateRecords.sql", false); } } }