You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.3 KiB

2 years ago
  1. using Kevin.SyntaxTextBox;
  2. using SqlSugar;
  3. using SqlSugar.Base;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Windows.Forms;
  9. namespace EntityBuilder
  10. {
  11. public partial class Form1 : Form
  12. {
  13. private SyntaxTextBox txtSyntax;
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private List<DbTableInfo> m_tables = new List<DbTableInfo>();
  19. private List<DbTableInfo> m_SelTables = new List<DbTableInfo>();
  20. private List<ColumnInfo> m_tableColumns = new List<ColumnInfo>();
  21. #region "初始化datagridview数据"
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. m_tables = TableHelper.GetTables();
  25. m_tables = m_tables.OrderBy(u => u.Name).ToList();
  26. dataGridView1.DataSource = m_tables;
  27. //reLoadColumns("student");
  28. }
  29. #endregion "初始化datagridview数据"
  30. #region "查询所有数据"
  31. private void reLoadColumns(string tablename)
  32. {
  33. m_tableColumns.Clear();
  34. m_tableColumns = TableHelper.GetColumnField(tablename);
  35. dataGridView2.DataSource = m_tableColumns;
  36. }
  37. #endregion "查询所有数据"
  38. #region "选择行,并填充到表单中,然后可做修改和删除操作"
  39. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  40. {
  41. if (e.ColumnIndex == 0)
  42. { //当单击複選框,同时处于组合编辑状态时
  43. var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
  44. var ifcheck1 = Convert.ToBoolean(cell.FormattedValue);
  45. var ifcheck2 = Convert.ToBoolean(cell.EditedFormattedValue);
  46. if (ifcheck1 != ifcheck2)
  47. {
  48. var tb = m_tables[e.RowIndex];
  49. reLoadColumns(tb.Name);
  50. var db = SugarBase.DB;
  51. var cls = db.DbFirst.IsCreateAttribute().Where(tb.Name).ToClassStringList("Entity.Sugar").First();
  52. var code = cls.Value;
  53. txtSyntax.Text = code;
  54. }
  55. }
  56. }
  57. #endregion "选择行,并填充到表单中,然后可做修改和删除操作"
  58. private void btnGen_Click(object sender, EventArgs e)
  59. {
  60. m_SelTables.Clear();
  61. var iRow = 0;
  62. foreach (DataGridViewRow row in dataGridView1.Rows)
  63. {
  64. var iCol = 0;
  65. foreach (DataGridViewCell cell in row.Cells)
  66. {
  67. if (iCol == 0)
  68. {
  69. var ifcheck1 = Convert.ToBoolean(cell.FormattedValue);
  70. if (ifcheck1)
  71. {
  72. var tb = m_tables[iRow];
  73. m_SelTables.Add(tb);
  74. }
  75. }
  76. iCol++;
  77. }
  78. iRow++;
  79. }
  80. if (m_SelTables.Count > 0)
  81. {
  82. var sPath = txtPath.Text.Trim();
  83. var db = SugarBase.DB;
  84. if (sPath == "")
  85. {
  86. sPath = DBUnit.GetAppSettings("OrmSugarPath");
  87. }
  88. foreach (DbTableInfo n in m_SelTables)
  89. {
  90. db.DbFirst.IsCreateAttribute().Where(n.Name).CreateClassFile(sPath, "Entity.Sugar");
  91. }
  92. MessageBox.Show(@"ORM(Sugar) 生成完成");
  93. }
  94. else
  95. {
  96. MessageBox.Show(@"請選擇要產生的table");
  97. }
  98. }
  99. private void allcheck_CheckedChanged(object sender, EventArgs e)
  100. {
  101. foreach (DataGridViewRow row in dataGridView1.Rows)
  102. {
  103. ((DataGridViewCheckBoxCell)row.Cells[0]).Value = allcheck.Checked;
  104. }
  105. }
  106. private void btnGen_Helper_Click(object sender, EventArgs e)
  107. {
  108. m_SelTables.Clear();
  109. var iRow = 0;
  110. foreach (DataGridViewRow row in dataGridView1.Rows)
  111. {
  112. var iCol = 0;
  113. foreach (DataGridViewCell cell in row.Cells)
  114. {
  115. if (iCol == 0)
  116. {
  117. var ifcheck1 = Convert.ToBoolean(cell.FormattedValue);
  118. if (ifcheck1)
  119. {
  120. var tb = m_tables[iRow];
  121. m_SelTables.Add(tb);
  122. }
  123. }
  124. iCol++;
  125. }
  126. iRow++;
  127. }
  128. var sPath = txtPath.Text.Trim();
  129. if (m_SelTables.Count > 0)
  130. {
  131. var code = CreateFileHelper.BuilderEntityHelperCode(m_SelTables);
  132. code = code.Replace("\n", "\r\n");
  133. txtSyntax.Text = code;
  134. if (sPath == "")
  135. {
  136. sPath = DBUnit.GetAppSettings("OrmEasyPath");
  137. }
  138. CreateFileHelper.CreateEntityHelper(m_SelTables, sPath);
  139. }
  140. else
  141. {
  142. MessageBox.Show("請選擇要產生的table");
  143. }
  144. }
  145. }
  146. }