数据库获取所有的表,所有的视图,字段说明,字段类型,字段名
//表、视图 绑定给下拉
public void BindDropName() { string sql = "Select Name FROM SysObjects Where XType='U' orDER BY Name";//表 DataTable dt = SqlOP.ExecuteDataset(sql).Tables[0]; DataSet dv=SqlOP.ExecuteDataset("select [name] as Name from sys.views ");//视图 foreach (DataRow dr in dv.Tables[0].Rows) { DataRow row = dt.NewRow(); row["Name"] = dr[0]; dt.Rows.Add(row);//这样就可以添加了 } DropDownList1.DataSource = dt; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "Name"; DropDownList1.DataBind(); }
生成实体
protected void ToEity() { string sql = "SELECT CAST(g.value AS nvarchar)as notes, a.name,b.name as ztype ,c.isnullable FROM systypes b, sys.columns AS a LEFT OUTER JOIN sys.syscolumns AS c ON a.name = c.name AND a.object_id = c.id left join sys.extended_properties g on (a.object_id = g.major_id AND a.column_id=g.minor_id) WHERE (a.object_id = OBJECT_ID('" + DropDownList1.SelectedValue + "'))and c.xtype=b.xusertype order by object_id,a.column_id"; DataSet ds = SqlOP.ExecuteDataset(sql); string json = "using System;\n using System.Text;\n using System.Collections; \n using System.Collections.Generic;\n namespace mode \n {\n public class " + DropDownList1.SelectedValue + "\n{ "; foreach (DataRow dr in ds.Tables[0].Rows) { json += " /// <summary> \n /// " + dr[0].ToString() + " \n /// </summary> \n public " + sqlType(dr[2].ToString(), dr[3].ToString()) + " " + dr[1].ToString() + " \n{ \n get;\n set;\n }\n"; } json += "} \n}"; txtVaule.Text = json; } protected string sqlType(string xtpye, string isnullable) { switch (xtpye) { case "uniqueidentifier": if (isnullable.Equals("1")) { return "Guid?"; } else { return "Guid"; } break; case "nvarchar": return "String"; break; case "varchar": return "String"; break; case "sysname": return "String"; break; case "text": return "String"; break; case "char": return "String"; break; case "int": if (isnullable.Equals("1")) { return "Int32?"; } else { return "Int32"; } break; case "tinyint": if (isnullable.Equals("1")) { return "Byte?"; } else { return "Byte"; } break; case "bit": if (isnullable.Equals("1")) { return "Byte?"; } else { return "Byte"; } break; case "datetime": if (isnullable.Equals("1")) { return "DateTime?"; } else { return "DateTime"; } break; case "float": if (isnullable.Equals("1")) { return "Double?"; } else { return "Double"; } break; case "decimal": if (isnullable.Equals("1")) { return "Decimal?"; } else { return "Decimal"; } break; default: return "String"; break; } }