gpt4 book ai didi

vb.net - 如何使用VFPOLEDB获取DBF信息

转载 作者:行者123 更新时间:2023-12-02 16:35:22 26 4
gpt4 key购买 nike

我可以使用 GetSchemaTableGetXMLSchema 从使用 VFPOLEDB 打开的 Foxpro DBF 中获取有关字段类型、大小等的信息,但无法获取与索引相关的任何信息在表格/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.

但是,这是一些非常有趣的建议,可以让您编写这个小控制台应用程序来获取表中可用的索引。这是通过直接从 SQL 查询架构表来实现的。以下示例位于著名的 Northwind 示例数据库的 Employees 表中。给你,

//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

但是,后来通过删除限制,我设法克服了 此提供商不支持该方法。 错误,并最终将其总结为这个较短的解决方案,

//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 - 如何使用VFPOLEDB获取DBF信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com