如何使用 C# 创建 .dbf 文件?

发布于 2024-09-17 08:07:43 字数 583 浏览 5 评论 0原文

我正在尝试使用此方法进行选择,并且效果很好

string str = 
    "SELECT * FROM FREE RIGHT JOIN TestTest ON FREE.DOCNO = TestTest.DOCNO";
DataTable dt = new DataTable();
OdbcDataAdapter da = new OdbcDataAdapter(str, odbconn);
da.Fill(dt);

我正在尝试使用此方法创建 .dbf 并得到此 OdbcException :

string str0 = "Create Table Persons (Name char(50), City char(50), Phone char(20), Zip decimal(5))";
OdbcCommand cmd = new OdbcCommand(str0, odbconn);
cmd.ExecuteNonQuery();

错误 [42000] [Microsoft][ODBC dBase 驱动程序] 字段定义中存在语法错误。

I am trying this to make a select and it works just fine

string str = 
    "SELECT * FROM FREE RIGHT JOIN TestTest ON FREE.DOCNO = TestTest.DOCNO";
DataTable dt = new DataTable();
OdbcDataAdapter da = new OdbcDataAdapter(str, odbconn);
da.Fill(dt);

I am trying this to create a .dbf and i get this OdbcException :

string str0 = "Create Table Persons (Name char(50), City char(50), Phone char(20), Zip decimal(5))";
OdbcCommand cmd = new OdbcCommand(str0, odbconn);
cmd.ExecuteNonQuery();

ERROR [42000] [Microsoft][ODBC dBase Driver] Syntax error in field definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-09-24 08:07:43

您的问题是由 Zip Decimal(5) 引起的,因为 ODBC dBase 驱动程序不喜欢它。在我快速谷歌之后,我无法想出它可以容忍的语法。如果您使用 OleDb 提供程序,它确实会非常愉快地接受它,如下所示:

using (var dBaseConnection = new OleDbConnection(
    @"Provider=Microsoft.Jet.OLEDB.4.0; " +
    @" Data Source=C:\Users\RobertWray\Documents\dBase; " +
    @"Extended Properties=dBase IV"))
{
    dBaseConnection.Open();

    string createTableSyntax = 
        "Create Table Person " +
        "(Name char(50), City char(50), Phone char(20), Zip decimal(5))";
    var cmd = new OleDbCommand(createTableSyntax, dBaseConnection);
    cmd.ExecuteNonQuery();
}

但有一个问题:确定要将 Zip 列创建为小数吗?我不是美国居民,我对这些信息不是 100% 有信心,但是,根据维基百科 邮政编码可以以 0 开头。将它们存储为数字数据类型将不允许您准确地表示它。

我通过 ODBC 创建表的代码:

using (var dBaseConnection = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=C:\Users\RobertWray\Documents\dBase;"))
{
    dBaseConnection.Open();

    string str0 = "Create Table Person2 (Name char(50), City char(50), Phone char(20))";
    var cmd = new OdbcCommand(str0, dBaseConnection);
    cmd.ExecuteNonQuery();
}

Your problem is caused by Zip decimal(5), as the ODBC dBase driver doesn't like it. Off the top of my head, and after a quick google, I couldn't come up with a syntax it would tolerate. It does accept it quite merrily if you use the OleDb provider instead, as follows:

using (var dBaseConnection = new OleDbConnection(
    @"Provider=Microsoft.Jet.OLEDB.4.0; " +
    @" Data Source=C:\Users\RobertWray\Documents\dBase; " +
    @"Extended Properties=dBase IV"))
{
    dBaseConnection.Open();

    string createTableSyntax = 
        "Create Table Person " +
        "(Name char(50), City char(50), Phone char(20), Zip decimal(5))";
    var cmd = new OleDbCommand(createTableSyntax, dBaseConnection);
    cmd.ExecuteNonQuery();
}

One question though: Are you sure that you want to create the Zip column as a decimal? Not being a US resident I'm not 100% confident on this information, but, according to Wikipedia ZIP codes can start with a 0. Storing them as a numeric datatype won't allow you to accurately represent that.

My code for creating the table via ODBC:

using (var dBaseConnection = new OdbcConnection(@"Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=C:\Users\RobertWray\Documents\dBase;"))
{
    dBaseConnection.Open();

    string str0 = "Create Table Person2 (Name char(50), City char(50), Phone char(20))";
    var cmd = new OdbcCommand(str0, dBaseConnection);
    cmd.ExecuteNonQuery();
}
无法言说的痛 2024-09-24 08:07:43

使用 vfpoledb 提供程序和字段类型 NUMERIC 而不是 DECIMAL

string ConnStr1 = @"Provider =vfpoledb; Data Source = C:\MyDataSource; Collating Sequence =machine;

Use vfpoledb provider and field type NUMERIC instead of DECIMAL.

string ConnStr1 = @"Provider =vfpoledb; Data Source = C:\MyDataSource; Collating Sequence =machine;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文