//表、视图 绑定给下拉
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; using System.Text; using System.Collections; using System.Collections.Generic; namespace mode { public class " + DropDownList1.SelectedValue + " { "; foreach (DataRow dr in ds.Tables[0].Rows) { json += " /// <summary> /// " + dr[0].ToString() + " /// </summary> public " + sqlType(dr[2].ToString(), dr[3].ToString()) + " " + dr[1].ToString() + " { get; set; } "; } json += "} }"; 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; } }