/// <summary> /// 拼接sql执行语句 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { ConfigModel configModel = new ConfigModel(); configModel.MARK = "@";//SQL的连接类型 SqlHelper.connStr = textBox2.Text.Trim(); DataTable dt = SqlHelper.GetDataTable(" select * from dbo.sysobjects where xtype = 'U' order by name"); if (dt == null) { MessageBox.Show("数据库连接失敗了!"); return; } string sql = @" SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, columnName = a.name, 标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 'Y'else 'N' end, primaryKey = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then 'Y' else 'N' end, columnType = b.name, 占用字节数 = a.length, char_col_decl_length = COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), nullable = case when a.isnullable=1 then 'N'else 'Y' end, data_default = isnull(e.text,''), columnComment = isnull(g.[value],'') FROM syscolumns a left join systypes b on a.xusertype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id left join sys. extended_properties f on d.id=f.major_id and f.major_id=0 where d.name=" + configModel.MARK + "a order by a.id,a.colorder"; SqlParameter[] sps = { new SqlParameter(configModel.MARK + "a", comboBox1.Text.Trim()) }; DataTable dt_tables = SqlHelper.GetDataTable(sql, sps); if (dt_tables == null) { return; } if (dt_tables.Rows.Count > 0) { #region 生成内容 StringBuilder sb_column1 = new StringBuilder(); //格式如 NO_ID,ST_NAME,ST_VALUES,NO_ORDER,ST_OTHER,ST_VALUES_ENG StringBuilder sb_column2 = new StringBuilder(); //格式如 @NO_ID,@ST_NAME,@ST_VALUES,@NO_ORDER,@ST_OTHER,@ST_VALUES_ENG StringBuilder sb_column3 = new StringBuilder(); //格式如 new SqlParameter("@NO_ID", SqlType.Number,4), StringBuilder sb_column4 = new StringBuilder(); //格式如 parameters[0].Value = model.NO_ID; StringBuilder sb_column5 = new StringBuilder(); //格式如 NO_ID=@NO_ID,ST_NAME=@ST_NAME for (int j = 0; j < dt_tables.Rows.Count; j++) { string colName = dt_tables.Rows[j]["columnName"].ToString().Trim(); //字段名 string colType = CommonCode.GetColType(dt_tables.Rows[j]["columnType"].ToString()); //字段类型 if (j == dt_tables.Rows.Count - 1) { sb_column1.Append(colName); sb_column2.Append(configModel.MARK + colName); sb_column3.AppendLine(" new SqlParameter(\""+ configModel.MARK + colName + "\", model." + colName + ";)};"); //sb_column4.AppendLine(" parameters[" + j + "].Value = model." + colName + ";"); sb_column4.AppendLine(" "+ colName + "= model." + colName + ","); sb_column5.Append(colName + "=" + configModel.MARK + colName); } else { sb_column1.Append(colName + ","); sb_column2.Append(configModel.MARK + colName + ","); sb_column3.AppendLine(" new SqlParameter(\""+ configModel.MARK + colName + "\", model." + colName + "),"); //sb_column4.AppendLine(" parameters[" + j + "].Value = model." + colName + ";"); sb_column4.AppendLine(" "+ colName + "= model." + colName + ","); sb_column5.Append(colName + "=" + configModel.MARK + colName + ","); } } richTextBox6.Text = sb_column1.ToString() + "\r\n\r\n"; richTextBox6.Text += sb_column2.ToString() + "\r\n\r\n"; richTextBox6.Text += sb_column5.ToString() + "\r\n\r\n"; richTextBox3.Text = sb_column3.ToString() + "\r\n\r\n"; richTextBox3.Text += sb_column4.ToString() + "\r\n\r\n"; ////生成model //CreateModel(dt_tables); #endregion 生成内容 } }