string myConnString = "Provider=SQLOLEDB;Data Source=server;Initial Catalog=Duwamish7;Integrated Security=SSPI;Connect Timeout=30";
OleDbConnectionconn = new OleDbConnection(myConnString);
DataSetds = new DataSet();
using (conn)
{
conn.Open();
//取表
DataTable dataTalbeschema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[]{ null, null, null,"TABLE" } );
//取表结构
foreach (DataRow tableRow in dataTalbeschema.Rows)
{
String tableName = tableRow["Table_Name"].ToString();
string sql = "select * from " + tableName ;
OleDbDataAdapter oda= new OleDbDataAdapter(sql,conn);
oda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
oda.FillSchema(ds,SchemaType.Mapped,tableName);
}
//添加Relation
DataTable dtfk = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Foreign_Keys,null );
foreach (DataRow fkRow in dtfk.Rows)
{
String fkName = fkRow["Fk_Name"].ToString();
DataColumn parentColumn = ds.Tables[fkRow["Pk_Table_Name"].ToString()].Columns[fkRow["Pk_Column_Name"].ToString()];
DataColumn childColumn = ds.Tables[fkRow["Fk_Table_Name"].ToString()].Columns[fkRow["Fk_Column_Name"].ToString()];
ds.Relations.Add(fkName,parentColumn,childColumn);
}
conn.Close();
}
详细了解请参看MSDN "GetOleDbSchemaTable"帮助