public class DataAccessbase { SqlConnection MyConnection = null; public DataAccessbase(string connectionString) { MyConnection = new SqlConnection(connectionString); if (MyConnection.State == ConnectionState.Closed) { MyConnection.Open(); } } /// <summary> /// 获取数据库中所有表名称 /// </summary> /// <returns></returns> public List<DataBaseInfo> GetAllTable() { DataTable dt = MyConnection.GetSchema("Tables"); List<DataBaseInfo> list = new List<DataBaseInfo>(); foreach (DataRow dr in dt.Rows) { DataBaseInfo entity = new DataBaseInfo(); entity.TableName = (String)dr["TABLE_NAME"]; list.Add(entity); } return list; } public List<TableInfo> GetColumnName(string tableName) { string sql = "Select * From " + tableName; SqlCommand MyCommand = new SqlCommand(sql, MyConnection); SqlDataReader MyReader = MyCommand.ExecuteReader(); DataTable MyTable = MyReader.GetSchemaTable(); int MyCount = 1; List<TableInfo> list = new List<TableInfo>(); foreach (DataRow MyRow in MyTable.Rows) { foreach (DataColumn MyColumn in MyTable.Columns) { TableInfo entity = new TableInfo(); entity.ColumnName = MyColumn.ColumnName.ToString(); entity.ColumnType = MyColumn.DataType.ToString(); list.Add(entity); } MyCount++; } MyReader.Close(); return list; } public void ClostConn() { MyConnection.Close(); } public void DisposeConn() { MyConnection.Dispose(); } }