如何使用VFPOLEDB获取DBF信息
How to use VFPOLEDB to get DBF information
我可以使用 GetSchemaTable
和 GetXMLSchema
从使用 VFPOLEDB 打开的 Foxpro DBF 中获取有关字段类型、大小等的信息,但无法获取有关 tables/CDX.
我不想使用索引,只是建立索引的标准来帮助我生成 SQL 命令以在 SQL 服务器上创建表并导入数据.
我可以将 DISPLAY STRUCTURE
输出到所有表上的文本文件并在 VB.NET 中解析它,但我希望有一些我忽略的东西,因为我不太熟悉VB.NET/OLEDB 语法还没有。
一些研究给出了这些结果。我已经开始查看 ForeignKey.FKTableSchema
属性,不幸的是不是 .NET 属性。后来当我发现 OleDbSchemaGuid.Indexes Field 时一切看起来都很好,直到我 运行 应用程序并获得 此提供程序不支持该方法。 终于有下面这篇文章点亮了,
GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Indexes - How to access included columns on index
和这个发现
The OleDb and Odbc providers do not provide a built-in catalog method
that will return non-key ("Included") index columns.
然而,一些非常有趣的建议让您可以编写这个小控制台应用程序来获取 table 中可用的索引。这是通过直接从 SQL 查询架构 table 来实现的。以下示例来自著名的 Northwind
示例数据库的 Employees
table。给你,
//Open a connection to the SQL Server Northwind database.
var connectionString =
"Provider=SQLOLEDB;Data Source=SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;Encrypt=False;TrustServerCertificate=False";
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
var select = "SELECT " +
" T.name AS TABLE_NAME" +
" , IX.name AS INDEX_NAME" +
" , IC.index_column_id AS IX_COL_ID" +
" , C.name AS COLUMN_NAME" +
" , IC.is_included_column AS INCLUDED_NONKEY" +
" " +
"FROM " +
" sys.tables T " +
" INNER JOIN sys.indexes IX" +
" ON T.object_id = IX.object_id " +
" INNER JOIN sys.index_columns IC" +
" ON IX.object_id = IC.object_id " +
" AND IX.index_id = IC.index_id " +
" INNER JOIN sys.columns C" +
" ON IC.object_id = C.object_id " +
" AND IC.column_id = C.column_id " +
" " +
"WHERE T.name = 'Employees'" +
"ORDER BY IC.index_column_id";
OleDbCommand cmd = new OleDbCommand(@select, connection);
cmd.CommandType = CommandType.Text;
var outputTable = new DataSet("Table");
var my = new OleDbDataAdapter(cmd).Fill(outputTable);
foreach (DataTable table in outputTable.Tables)
{
foreach (DataRow myField in table.Rows)
{
//For each property of the field...
foreach (DataColumn myProperty in table.Columns)
{
//Display the field name and value.
Console.WriteLine(myProperty.ColumnName + " = " +
myField[myProperty].ToString());
}
Console.WriteLine();
}
}
}
Console.ReadLine();
终于有了结果,
TABLE_NAME = Employees
INDEX_NAME = PK_Employees
IX_COL_ID = 1
COLUMN_NAME = EmployeeID
INCLUDED_NONKEY = False
TABLE_NAME = Employees
INDEX_NAME = LastName
IX_COL_ID = 1
COLUMN_NAME = LastName
INCLUDED_NONKEY = False
TABLE_NAME = Employees
INDEX_NAME = PostalCode
IX_COL_ID = 1
COLUMN_NAME = PostalCode
INCLUDED_NONKEY = False
但是,后来通过删除限制,我设法克服了 Method is not supported by this provider. 错误并结束总结起来就像这个较短的解决方案,
//Open a connection to the SQL Server Northwind database.
var connectionString =
"Provider=SQLOLEDB;Data Source=SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;Encrypt=False;TrustServerCertificate=False";
using (OleDbConnection cnn = new OleDbConnection(connectionString))
{
cnn.Open();
DataTable schemaIndexess = cnn.GetSchema("Indexes",
new string[] {null, null, null});
DataTable schemaIndexes = cnn.GetOleDbSchemaTable(
OleDbSchemaGuid.Indexes,
new object[] {null, null, null});
foreach (DataRow myField in schemaIndexes.Rows)
{
//For each property of the field...
foreach (DataColumn myProperty in schemaIndexes.Columns)
{
//Display the field name and value.
Console.WriteLine(myProperty.ColumnName + " = " +
myField[myProperty].ToString());
}
Console.WriteLine();
}
Console.ReadLine();
}
这会导致较长的输出需要整理,但其中一部分是
TABLE_CATALOG = Northwind
TABLE_SCHEMA = dbo
TABLE_NAME = Employees
INDEX_CATALOG = Northwind
INDEX_SCHEMA = dbo
INDEX_NAME = LastName
PRIMARY_KEY = False
UNIQUE = False
CLUSTERED = False
TYPE = 1
FILL_FACTOR = 0
INITIAL_SIZE =
NULLS =
SORT_BOOKMARKS = False
AUTO_UPDATE = True
NULL_COLLATION = 4
ORDINAL_POSITION = 1
COLUMN_NAME = LastName
COLUMN_GUID =
COLUMN_PROPID =
COLLATION = 1
CARDINALITY =
PAGES = 1
FILTER_CONDITION =
INTEGRATED = False
完美!
我需要提取的一切。因为它是在 vb.net 部分我发布了我的粗略代码,所以我过滤了返回的字段所以我可以在这里列出一些。
它 returns 所有与索引相关的信息,甚至是用表达式创建的复杂信息。
将返回连接字符串中提供的具有 CDX 索引的路径中的所有表。
TABLE_NAME = schematest
INDEX_NAME = char3ascen
NULLS = 1
EXPRESSION = char3ascen
TABLE_NAME = schematest
INDEX_NAME = expressn
NULLS = 2
EXPRESSION = LEFT(char1null,4)+SUBSTR(char2,4,2)
TABLE_NAME = schematest
INDEX_NAME = multifld
NULLS = 2
EXPRESSION = char1null+char2
TABLE_NAME = customer
INDEX_NAME = zip
NULLS = 1
EXPRESSION = zip
Private Sub GetIndexInfo_Click(sender As Object, e As EventArgs) Handles GetIndexInfo.Click
Dim cnnOLEDB As New OleDbConnection
Dim SchemaTable As DataTable
Dim myField As DataRow
Dim myProperty As DataColumn
Dim ColumnNames As New List(Of String)
Dim strConnectionString = "Provider=vfpoledb;Data Source=D:\ACW\;Collating Sequence=general;DELETED=False"
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()
ColumnNames.Add("TABLE_NAME")
columnnames.Add("INDEX_NAME")
columnnames.Add("NULLS")
columnnames.Add("TYPE")
columnnames.Add("EXPRESSION")
SchemaTable = cnnOLEDB.GetSchema("Indexes")
'For Each myProperty In SchemaTable.Columns
For Each myField In SchemaTable.Rows
For Each myProperty In SchemaTable.Columns
If ColumnNames.Contains(myProperty.ColumnName) Then
Console.WriteLine(myProperty.ColumnName & " = " & myField(myProperty).ToString)
End If
Next
Console.WriteLine()
Next
Console.ReadLine()
DGVSchema.DataSource = SchemaTable
End Sub
我可以使用 GetSchemaTable
和 GetXMLSchema
从使用 VFPOLEDB 打开的 Foxpro DBF 中获取有关字段类型、大小等的信息,但无法获取有关 tables/CDX.
我不想使用索引,只是建立索引的标准来帮助我生成 SQL 命令以在 SQL 服务器上创建表并导入数据.
我可以将 DISPLAY STRUCTURE
输出到所有表上的文本文件并在 VB.NET 中解析它,但我希望有一些我忽略的东西,因为我不太熟悉VB.NET/OLEDB 语法还没有。
一些研究给出了这些结果。我已经开始查看 ForeignKey.FKTableSchema
属性,不幸的是不是 .NET 属性。后来当我发现 OleDbSchemaGuid.Indexes Field 时一切看起来都很好,直到我 运行 应用程序并获得 此提供程序不支持该方法。 终于有下面这篇文章点亮了,
GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Indexes - How to access included columns on index
和这个发现
The OleDb and Odbc providers do not provide a built-in catalog method that will return non-key ("Included") index columns.
然而,一些非常有趣的建议让您可以编写这个小控制台应用程序来获取 table 中可用的索引。这是通过直接从 SQL 查询架构 table 来实现的。以下示例来自著名的 Northwind
示例数据库的 Employees
table。给你,
//Open a connection to the SQL Server Northwind database.
var connectionString =
"Provider=SQLOLEDB;Data Source=SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;Encrypt=False;TrustServerCertificate=False";
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
var select = "SELECT " +
" T.name AS TABLE_NAME" +
" , IX.name AS INDEX_NAME" +
" , IC.index_column_id AS IX_COL_ID" +
" , C.name AS COLUMN_NAME" +
" , IC.is_included_column AS INCLUDED_NONKEY" +
" " +
"FROM " +
" sys.tables T " +
" INNER JOIN sys.indexes IX" +
" ON T.object_id = IX.object_id " +
" INNER JOIN sys.index_columns IC" +
" ON IX.object_id = IC.object_id " +
" AND IX.index_id = IC.index_id " +
" INNER JOIN sys.columns C" +
" ON IC.object_id = C.object_id " +
" AND IC.column_id = C.column_id " +
" " +
"WHERE T.name = 'Employees'" +
"ORDER BY IC.index_column_id";
OleDbCommand cmd = new OleDbCommand(@select, connection);
cmd.CommandType = CommandType.Text;
var outputTable = new DataSet("Table");
var my = new OleDbDataAdapter(cmd).Fill(outputTable);
foreach (DataTable table in outputTable.Tables)
{
foreach (DataRow myField in table.Rows)
{
//For each property of the field...
foreach (DataColumn myProperty in table.Columns)
{
//Display the field name and value.
Console.WriteLine(myProperty.ColumnName + " = " +
myField[myProperty].ToString());
}
Console.WriteLine();
}
}
}
Console.ReadLine();
终于有了结果,
TABLE_NAME = Employees
INDEX_NAME = PK_Employees
IX_COL_ID = 1
COLUMN_NAME = EmployeeID
INCLUDED_NONKEY = False
TABLE_NAME = Employees
INDEX_NAME = LastName
IX_COL_ID = 1
COLUMN_NAME = LastName
INCLUDED_NONKEY = False
TABLE_NAME = Employees
INDEX_NAME = PostalCode
IX_COL_ID = 1
COLUMN_NAME = PostalCode
INCLUDED_NONKEY = False
但是,后来通过删除限制,我设法克服了 Method is not supported by this provider. 错误并结束总结起来就像这个较短的解决方案,
//Open a connection to the SQL Server Northwind database.
var connectionString =
"Provider=SQLOLEDB;Data Source=SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;Encrypt=False;TrustServerCertificate=False";
using (OleDbConnection cnn = new OleDbConnection(connectionString))
{
cnn.Open();
DataTable schemaIndexess = cnn.GetSchema("Indexes",
new string[] {null, null, null});
DataTable schemaIndexes = cnn.GetOleDbSchemaTable(
OleDbSchemaGuid.Indexes,
new object[] {null, null, null});
foreach (DataRow myField in schemaIndexes.Rows)
{
//For each property of the field...
foreach (DataColumn myProperty in schemaIndexes.Columns)
{
//Display the field name and value.
Console.WriteLine(myProperty.ColumnName + " = " +
myField[myProperty].ToString());
}
Console.WriteLine();
}
Console.ReadLine();
}
这会导致较长的输出需要整理,但其中一部分是
TABLE_CATALOG = Northwind
TABLE_SCHEMA = dbo
TABLE_NAME = Employees
INDEX_CATALOG = Northwind
INDEX_SCHEMA = dbo
INDEX_NAME = LastName
PRIMARY_KEY = False
UNIQUE = False
CLUSTERED = False
TYPE = 1
FILL_FACTOR = 0
INITIAL_SIZE =
NULLS =
SORT_BOOKMARKS = False
AUTO_UPDATE = True
NULL_COLLATION = 4
ORDINAL_POSITION = 1
COLUMN_NAME = LastName
COLUMN_GUID =
COLUMN_PROPID =
COLLATION = 1
CARDINALITY =
PAGES = 1
FILTER_CONDITION =
INTEGRATED = False
完美! 我需要提取的一切。因为它是在 vb.net 部分我发布了我的粗略代码,所以我过滤了返回的字段所以我可以在这里列出一些。 它 returns 所有与索引相关的信息,甚至是用表达式创建的复杂信息。 将返回连接字符串中提供的具有 CDX 索引的路径中的所有表。
TABLE_NAME = schematest
INDEX_NAME = char3ascen
NULLS = 1
EXPRESSION = char3ascen
TABLE_NAME = schematest
INDEX_NAME = expressn
NULLS = 2
EXPRESSION = LEFT(char1null,4)+SUBSTR(char2,4,2)
TABLE_NAME = schematest
INDEX_NAME = multifld
NULLS = 2
EXPRESSION = char1null+char2
TABLE_NAME = customer
INDEX_NAME = zip
NULLS = 1
EXPRESSION = zip
Private Sub GetIndexInfo_Click(sender As Object, e As EventArgs) Handles GetIndexInfo.Click
Dim cnnOLEDB As New OleDbConnection
Dim SchemaTable As DataTable
Dim myField As DataRow
Dim myProperty As DataColumn
Dim ColumnNames As New List(Of String)
Dim strConnectionString = "Provider=vfpoledb;Data Source=D:\ACW\;Collating Sequence=general;DELETED=False"
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()
ColumnNames.Add("TABLE_NAME")
columnnames.Add("INDEX_NAME")
columnnames.Add("NULLS")
columnnames.Add("TYPE")
columnnames.Add("EXPRESSION")
SchemaTable = cnnOLEDB.GetSchema("Indexes")
'For Each myProperty In SchemaTable.Columns
For Each myField In SchemaTable.Rows
For Each myProperty In SchemaTable.Columns
If ColumnNames.Contains(myProperty.ColumnName) Then
Console.WriteLine(myProperty.ColumnName & " = " & myField(myProperty).ToString)
End If
Next
Console.WriteLine()
Next
Console.ReadLine()
DGVSchema.DataSource = SchemaTable
End Sub