将数据插入 .dbf 文件时收到错误
Receiving an error when insert data into a .dbf file
背景知识
我目前正在做一个项目,需要我创建一个本地 .dbf
文件,然后我必须在该文件中填充一个值。我目前能够在测试目录中创建一个 .dbf
文件并向其添加一列,但是当我稍后尝试向该列添加一个值时,它会出错。
问题
我目前无法写入我创建的 .dbf
文件中的 Public
列。当代码进入 ExecuteNonQuery();
时,它出错并被我的 catch 语句捕获。
工作代码
public static bool CreateDBF()
{
try
{
string dbfDirectory = @"c:\Users\me\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
}
return true;
}
catch (Exception ex)
{
throw;
}
}
工作代码 - 附录:
上面的代码块成功创建了包含我想要的列的 .dbf
文件,如下图所示
无效代码:
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = @"INSERT INTO CustomProperties (Public)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
new OleDbCommand("set null off", connection).ExecuteNonQuery();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
throw;
}
}
更新代码块
工作块:
public static bool CreateDBF()
{
try
{
string dbfDirectory = @"c:\Users\er4505\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false; << I have a breakpoint here
}
}
非工作块:
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = @"INSERT INTO CustomProperties (Public) VALUES (@Public)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false; << breakpoint here
}
}
发现错误:
oledbErrors
Message: Syntax error.
NativeError: 0
Source: Microsoft OLE DB Provider for Visual FoxPro
ClassName: System.Data.OleDb.OleDbException
Message: Syntax error.
public static bool CreateDBF()
{
...
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
...
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF"); // change 1
return true;
}
catch (Exception)
{
throw; //change2
}
}
public static bool InsertDataIntoDBF(string path)
{
try
{
...
}
catch (Exception)
{
throw; //change 3
}
}
这对我有用。试一试
static void Main(string[] args)
{
try
{
CreateDBF();
Console.WriteLine(ReadDB());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static string dbfDirectory = @"c:\Test\dbTest";
private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbfDirectory + ";Extended Properties = dBase IV";
public static bool CreateDBF()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "Create Table CustomProperties ([Public] char(50))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
return true;
}
public static bool InsertDataIntoDBF(string path)
{
string query = @"INSERT INTO CustomProperties ([Public]) VALUES (@Public)";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
public static string ReadDB()
{
string res = string.Empty;
string query = @"SELECT * FROM CustomProperties";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
res = (string)command.ExecuteScalar();
connection.Close();
}
return res;
}
错误纯粹是语法错误,我在以下 link
中找到了一个非常好的示例作为指导
工作代码块
public static bool CreateDBF()
{
try
{
string dbfDirectory = @"c:\Users\er4505\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false;
}
}
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = "INSERT INTO CustomProperties(Public) VALUES (?)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
connection.Open();
OleDbCommand cmdInit = new OleDbCommand("set null off", connection);
cmdInit.ExecuteNonQuery();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbParameter publicStatus = command.Parameters.Add("Public", OleDbType.Char);
publicStatus.Value = "True";
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
log4net.LogManager.GetLogger("EmailLogger").Error(JsonConvert.SerializeObject(ex));
string viewError = JsonConvert.SerializeObject(ex);
return false;
}
}
背景知识
我目前正在做一个项目,需要我创建一个本地 .dbf
文件,然后我必须在该文件中填充一个值。我目前能够在测试目录中创建一个 .dbf
文件并向其添加一列,但是当我稍后尝试向该列添加一个值时,它会出错。
问题
我目前无法写入我创建的 .dbf
文件中的 Public
列。当代码进入 ExecuteNonQuery();
时,它出错并被我的 catch 语句捕获。
工作代码
public static bool CreateDBF()
{
try
{
string dbfDirectory = @"c:\Users\me\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
}
return true;
}
catch (Exception ex)
{
throw;
}
}
工作代码 - 附录:
上面的代码块成功创建了包含我想要的列的 .dbf
文件,如下图所示
无效代码:
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = @"INSERT INTO CustomProperties (Public)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
new OleDbCommand("set null off", connection).ExecuteNonQuery();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
throw;
}
}
更新代码块
工作块:
public static bool CreateDBF()
{
try
{
string dbfDirectory = @"c:\Users\er4505\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false; << I have a breakpoint here
}
}
非工作块:
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = @"INSERT INTO CustomProperties (Public) VALUES (@Public)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false; << breakpoint here
}
}
发现错误:
oledbErrors
Message: Syntax error.
NativeError: 0
Source: Microsoft OLE DB Provider for Visual FoxProClassName: System.Data.OleDb.OleDbException
Message: Syntax error.
public static bool CreateDBF()
{
...
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
...
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF"); // change 1
return true;
}
catch (Exception)
{
throw; //change2
}
}
public static bool InsertDataIntoDBF(string path)
{
try
{
...
}
catch (Exception)
{
throw; //change 3
}
}
这对我有用。试一试
static void Main(string[] args)
{
try
{
CreateDBF();
Console.WriteLine(ReadDB());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static string dbfDirectory = @"c:\Test\dbTest";
private static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbfDirectory + ";Extended Properties = dBase IV";
public static bool CreateDBF()
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "Create Table CustomProperties ([Public] char(50))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
return true;
}
public static bool InsertDataIntoDBF(string path)
{
string query = @"INSERT INTO CustomProperties ([Public]) VALUES (@Public)";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
public static string ReadDB()
{
string res = string.Empty;
string query = @"SELECT * FROM CustomProperties";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(query, connection);
command.Parameters.AddWithValue("@Public", "True");
connection.Open();
res = (string)command.ExecuteScalar();
connection.Close();
}
return res;
}
错误纯粹是语法错误,我在以下 link
中找到了一个非常好的示例作为指导工作代码块
public static bool CreateDBF()
{
try
{
string dbfDirectory = @"c:\Users\er4505\Desktop\New911";
string connectionString = "Provider=VFPOLEDB;Data Source=" + dbfDirectory;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = connection.CreateCommand();
command.CommandText = "create table CustomProperties(Public C(60))";
command.ExecuteNonQuery();
connection.Close();
}
InsertDataIntoDBF(dbfDirectory + "\CustomProperties.DBF");
return true;
}
catch (Exception ex)
{
string viewError = JsonConvert.SerializeObject(ex);
return false;
}
}
public static bool InsertDataIntoDBF(string path)
{
try
{
string strLogConnectionString = "Provider=VFPOLEDB;Data Source=" + path + ";Collating Sequence=machine;Mode=ReadWrite;";
string query = "INSERT INTO CustomProperties(Public) VALUES (?)";
using (OleDbConnection connection = new OleDbConnection(strLogConnectionString))
{
connection.Open();
OleDbCommand cmdInit = new OleDbCommand("set null off", connection);
cmdInit.ExecuteNonQuery();
OleDbCommand command = new OleDbCommand(query, connection);
OleDbParameter publicStatus = command.Parameters.Add("Public", OleDbType.Char);
publicStatus.Value = "True";
command.ExecuteNonQuery();
connection.Close();
}
return true;
}
catch (Exception ex)
{
log4net.LogManager.GetLogger("EmailLogger").Error(JsonConvert.SerializeObject(ex));
string viewError = JsonConvert.SerializeObject(ex);
return false;
}
}