使用Ado.net获取数据库架构信息
界面上的控件有:
textBoxConnString 文本框
btnGet 按钮
dataGridView1 GridView
private void btnGet_Click(object sender, EventArgs e)
{
DataTable dt = GetSqlDbSchemaUsingOleDbConnection(this.textBoxConnString.Text.Trim());
this.dataGridView1.DataSource = dt;
}
private DataTable GetSqlDbSchemaUsingOleDbConnection(string connString)
{
OleDbConnection myConn = new OleDbConnection(connString);
myConn.Open();
DataTable table1 = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, null});
myConn.Close();
return table1;
}
得到的表格中,有一列DATA_TYPE为数据类型,数据为数字,经过分析,对应关系如下:
smallint 2
int/数字 3
real 4
float 5
money,smallmoney/货币 6
bit/是/否 11
sql_variant 12
tinyint 17
bigint 20
uniqueidentifier 72
binary,timestamp,image,varbinary/ole对象 128
char,text,varchar 129
nchar,ntext,nvarchar/文本,备注,超链接 130
numeric 131
smalldatetime,DateTime/日期时间 135
textBoxConnString 文本框
btnGet 按钮
dataGridView1 GridView
private void btnGet_Click(object sender, EventArgs e)
{
DataTable dt = GetSqlDbSchemaUsingOleDbConnection(this.textBoxConnString.Text.Trim());
this.dataGridView1.DataSource = dt;
}
private DataTable GetSqlDbSchemaUsingOleDbConnection(string connString)
{
OleDbConnection myConn = new OleDbConnection(connString);
myConn.Open();
DataTable table1 = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, null});
myConn.Close();
return table1;
}
得到的表格中,有一列DATA_TYPE为数据类型,数据为数字,经过分析,对应关系如下:
smallint 2
int/数字 3
real 4
float 5
money,smallmoney/货币 6
bit/是/否 11
sql_variant 12
tinyint 17
bigint 20
uniqueidentifier 72
binary,timestamp,image,varbinary/ole对象 128
char,text,varchar 129
nchar,ntext,nvarchar/文本,备注,超链接 130
numeric 131
smalldatetime,DateTime/日期时间 135