Sql根据连接字符串获取库下的表列表、获取表结构
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=182.168.10.240)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));User Id=JG_BASEPLATFORM;Password=123456
#region 获取表/// <summary>
/// 获取表
/// 添加人:zec 添加时间:2015-12-22
/// </summary>
/// <param name="ConnectionStr">连接字符串</param>
/// <param name="DBName">库命</param>
/// <param name="ObjType">对象类型 U表 V视图 P存储过程</param>
/// <returns></returns>
public DataTable GetDBObj(string ConnectionStr, string ObjType = "U")
{
DataTable dt = new DataTable();
try
{
Hzjg.Common.DB.DBHelper oProxy = new Hzjg.Common.DB.DBHelper(ConnectionStr, "System.Data.SqlClient");
string sql = @" SELECT 对象名 = D.NAME,对象说明 = ISNULL(F.VALUE,'')
FROM SYSOBJECTS D LEFT JOIN sys.extended_properties F ON
D.ID=F.major_id AND F.minor_id=0 AND F.name = 'MS_Description'
WHERE D.XTYPE='" + ObjType + @"' AND D.NAME<>'DTPROPERTIES'
ORDER BY D.NAME";
DataSet ds = oProxy.EntityBase.ExecuteDataSet(System.Data.CommandType.Text, sql);
if (ds != null && ds.Tables.Count > 0)
{
dt = ds.Tables[0];
}
}
catch (Exception ex)
{
Hzjg.Common.Utility.Log.fWriterLog("获取表出错:", ex);
}
return dt;
}
#region 获取表信息
/// <summary>
/// 获取表信息
/// </summary>
/// <param name="ConnStr"></param>
/// <param name="TableSpace"></param>
/// <returns></returns>
public DataTable GetDBTable(string ConnStr, string Owner, string TableSpace)
{
DataTable dt = new DataTable();
Hzjg.Common.DB.DBHelper oProxy = new Hzjg.Common.DB.DBHelper(ConnStr, "System.Data.OracleClient");
//string sql = @"SELECT A.TABLE_NAME AS 表名,B.COMMENTS AS 表说明 FROM USER_TABLES A LEFT JOIN ALL_TAB_COMMENTS B ON A.TABLE_NAME = B.TABLE_NAME WHERE B.OWNER = '" + Owner.ToUpper() + "'";
string sql = @"SELECT A.TABLE_NAME AS 对象名,B.COMMENTS AS 对象说明 FROM USER_TABLES A LEFT JOIN ALL_TAB_COMMENTS B ON A.TABLE_NAME = B.TABLE_NAME WHERE B.OWNER = '" + Owner.ToUpper() + "'";
if (TableSpace != "")
{
sql += " AND A.TABLESPACE_NAME ='" + TableSpace + "'";
}
DataSet ds = oProxy.EntityBase.ExecuteDataSet(System.Data.CommandType.Text, sql);
if (ds != null && ds.Tables.Count > 0)
dt = ds.Tables[0];
return dt;
}
#endregion
#endregion
#region 获取表结构
/// <summary>
/// 获取表、视图字段信息
/// 添加人:朱二超 添加时间:2015-12-22
/// </summary>
/// <param name="TableName">表名</param>
/// <param name="ConnectionStr">连接字符串</param>
/// <param name="ObjType">对象类型 U表 V视图 P存储过程</param>
/// <returns></returns>
public DataTable GetDBObjColumn(string TableName, string ConnectionStr, string ObjType = "U")
{
DataTable dt = new DataTable();
try
{
#region 获取表结构
Hzjg.Common.DB.DBHelper oProxy = new Hzjg.Common.DB.DBHelper(ConnectionStr, "System.Data.SqlClient");
string sql =@" SELECT 字段名 = a.name,字段序号 = a.colorder,标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键 = 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 '√' else '' end,
类型 = b.name,占用字节数 = a.length,长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,默认值 = isnull(e.text,''),
字段说明 = 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='" + ObjType + @"' 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
where d.name='" + TableName + "'";
DataSet ds = oProxy.EntityBase.ExecuteDataSet(System.Data.CommandType.Text, sql);
if (ds != null && ds.Tables.Count > 0)
dt = ds.Tables[0];
#endregion
}
catch (Exception ex)
{
Hzjg.Common.Utility.Log.fWriterLog("获取表结构出错:", ex);
}
return dt;
}
#endregion
/// 获取表、视图字段信息
/// 添加人:朱二超 添加时间:2015-12-22
/// </summary>
/// <param name="TableName">表名</param>
/// <param name="ConnectionStr">连接字符串</param>
/// <param name="ObjType">对象类型 U表 V视图 P存储过程</param>
/// <returns></returns>
public DataTable GetDBObjColumn(string TableName, string ConnectionStr, string ObjType = "U")
{
DataTable dt = new DataTable();
try
{
#region 获取表结构
Hzjg.Common.DB.DBHelper oProxy = new Hzjg.Common.DB.DBHelper(ConnectionStr, "System.Data.SqlClient");
string sql =@" SELECT 字段名 = a.name,字段序号 = a.colorder,标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键 = 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 '√' else '' end,
类型 = b.name,占用字节数 = a.length,长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,默认值 = isnull(e.text,''),
字段说明 = 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='" + ObjType + @"' 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
where d.name='" + TableName + "'";
DataSet ds = oProxy.EntityBase.ExecuteDataSet(System.Data.CommandType.Text, sql);
if (ds != null && ds.Tables.Count > 0)
dt = ds.Tables[0];
#endregion
}
catch (Exception ex)
{
Hzjg.Common.Utility.Log.fWriterLog("获取表结构出错:", ex);
}
return dt;
}
#endregion