我试图通过从XML文件中读取值并将它们插入dbase文件来插入值。当我执行我的代码时,我只获取每个字段中的值,我没有得到我从XML文件中读取的实际值。下面是代码:
private static void ReadXMLFile()
{
XDocument doc = XDocument.Load(@"tblTest.xml");
DistName dist = new DistName();
string folderPath = @"C:\folder\";
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + folderPath + "; Extended Properties=DBASE III;";
var Rows = from row in doc.Descendants("row")
select new
{
Code = row.Element("code").Value,
Name = row.Element("Name").Value,
Type = row.Element("type").Value,
};
using (OleDbConnection connection = new OleDbConnection(connString))
using (OleDbCommand command = connection.CreateCommand())
{
connection.Open();
foreach (var row in Rows)
{
dist.Code = row.Code;
dist.Name = row.Name;
dist.Type = row.Type;
command.CommandText = insertStatment(dist);
command.ExecuteNonQuery();
}
}
}
我的插入语句如下所示:
StringBuilder sqlstmt = new StringBuilder();
sqlstmt.Append("INSERT INTO tblNAME (CODE, NAME, TYPE )");
sqlstmt.Append(" Values ( '" + dist.Code + "','");
sqlstmt.Append(dist.Name + "','");
sqlstmt.Append(dist.Type "'" + ")");
这是我创建dbf文件的方式
private static void CreateDBFFile()
{
// string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder";
string folderPath = @"C:\folder\";
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + folderPath + "; Extended Properties=DBASE III;";
using (OleDbConnection connection = new OleDbConnection(connString))
using (OleDbCommand command = connection.CreateCommand())
{
connection.Open();
command.CommandText = "CREATE TABLE DISTNAME (CODE TEXT, NAME TEXT, TYPE Text)";
command.ExecuteNonQuery();
}
}
我检查了值并且有实际值,但是当我打开dbf文件时,我只在每个字段中看到。下面是我在表格中得到的值的图像:
任何帮助将不胜感激。