Backgournd
我目前正在开发一个项目,需要我创建一个本地.dbf
文件,然后我必须填充一个值。我目前能够在测试目录中创建一个.dbf
文件并为其添加一列,但是当我稍后尝试向该列添加一个值时,它会出错。
问题
我目前无法写入我创建的Public
文件中的列.dbf
。当代码进入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
消息:语法错误。
NativeError:0
来源:Visual FoxPro的Microsoft OLE DB提供程序ClassName:System.Data.OleDb.OleDbException
消息:语法错误。
答案 0 :(得分:1)
这对我有用。试试吧
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;
}
答案 1 :(得分:0)
错误纯粹是语法,我在以下链接中找到了一个非常好的例子
工作代码块
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;
}
}
答案 2 :(得分:-1)
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
}
}