问题描述
我正在使用 VS2019,创建一个 Windows 窗体应用程序。通常我只更新 DBF 文件,但这一次,我需要插入一些数据。
我已经看过 SO、MSDN 和其他网站上的示例。
DBF 文件“transport”包含一些字段,包括 transid,一个长度为 10 的字符字段。
这是我的片段:
using System.Data.OleDb;
private static readonly string CONNECTION = @"Provider=VFPOLEDB;Data Source=C:\<path_to_existing_dbf-file>";
private void test()
{
using (OleDbConnection conn = new OleDbConnection(CONNECTION))
{
conn.open();
using (OleDbCommand cmd = new OleDbCommand("INSERT INTO transport (transid) VALUES (?)",conn))
{
cmd.Parameters.AddWithValue("@transid","0123456789");
new OleDbCommand("set null off",conn).ExecuteNonQuery();
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
有人知道我哪里出错了吗? 在 Visual FoxPro9.0 中,我可以毫无问题地执行命令: SELECT * FROM 运输 INSERT INTO 传输(传输)值('0123456789')。
我的错误信息:
Der Befehl enthielt mindestens einen Fehler。
该命令至少包含一个错误
异常的堆栈跟踪:
在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBParaMS dbParams,Object&executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior 行为,Object&executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,String 方法)
在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
在 C:
string viewError = JsonConvert.SerializeObject(ex);
返回:
{"oledbErrors":[{"Message":"Der Befehl enthielt mindestens einen Fehler.","NativeError":0,"Source":"Microsoft OLE DB Provider for Visual FoxPro","sqlState":"" }],"ClassName":"System.Data.OleDb.OleDbException","Message":"Der Befehl enthielt mindestens einen Fehler.","Data":null,"InnerException":null,"HelpURL":null," StackTraceString":" bei System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)\r\n bei System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBParaMS dbParams,Object& executeResult)\r\n bei System.OleDb.Data. OleDbCommand.ExecuteCommandText(Object&executeResult)\r\n 在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior 行为,Object&executeResult)\r\n 在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,String 方法)\ r\n bei System.Data.OleDb.OleDbCommand.ExecuteNonQuery()\r\n bei .ErstelleTransportauftrag(ARKPlatz quelle,String lkeyQuelle,ARKPlatz ziel,String lkeyZiel,S C:\VFPInteraktion.cs:Zeile 254.","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nExecuteCommandTextErrorHandling\nSystem.Data,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089\nSystem.Data.OleDb.OleDbCommand\nVoid ExecuteCommandTextErrorHandling(System.Data.OleDb.OleDbHResult)","HResult":-2147217900,"Source","Microsoft FoxPro 提供程序" "WatsonBuckets":null}
解决方法
好的,解决了。
这是一个非常愚蠢的错误。 我的目录中有一个 .(dot)。
这以某种方式破坏了 INSERT 和 UPDATE 查询。 但不是 SELECT 查询。 链接到 tek-tips,他也帮助了我,并从 GriffMG 那里得到了一个最低限度的工作示例: (https://www.tek-tips.com/viewthread.cfm?qid=1808490)
低于最小工作示例。另外还有一个字段不可为空,这是在该过程中发现的。抱歉耽误了大家的时间。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private static readonly string CONNECTION = @"Provider=VFPOLEDB;Data Source=d:\$incoming\finedata\";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(CONNECTION))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("INSERT INTO transport1 (transid) VALUES (?)",conn))
{
cmd.Parameters.AddWithValue("@transid","0123456789");
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
}