2
我已经创建了一个程序,用于从excel电子表格中提取数据并将其推送到foxpro数据库。但是,我遇到了出现重复客户端记录的问题。在使用参数化查询插入之前查看foxpro数据库中存在的记录
这是由于一个客户端与多个其他记录相关联。
我需要知道如何在写入之前检查数据库以查看某个特定记录是否存在,但是我正在绘制一个完整的空白。
我的代码(这个特殊类),因为它代表读起来就像下面:
namespace PropertyImport
{
public class Landlord
{
public void Import()
{
int IDCOUNT = 0;
using (var exportConnection = new OleDbConnection(connectionString: Settings.ImportFrom))
using (var importConnection = new OleDbConnection(connectionString: Settings.ImportTo))
using (OleDbCommand exportCommand = new OleDbCommand(@"SELECT
[clcodel salute],
[clcodel fname],
[clcodel sname]
from [export_test$]"
, exportConnection))
using (OleDbCommand importCommand = new OleDbCommand(@"INSERT INTO CLIENT
(CLCODE,CLCODEDESC,CLCLASS,
FNAME,MNAME,SNAME
)
VALUES
(?,?,?,?,?,?,?,?)",
importConnection))
{
OleDbDataReader exportReader;
//
importCommand.Parameters.AddWithValue("CLCODE", "");
importCommand.Parameters.AddWithValue("CLCODEDESC", "");
//importCommand.Parameters.AddWithValue("CLCLASS", "");
//importCommand.Parameters.AddWithValue("NEGOTIATOR", "");
//importCommand.Parameters.AddWithValue("TITLE", "");
importCommand.Parameters.AddWithValue("FNAME", "");
importCommand.Parameters.AddWithValue("MNAME", "");
importCommand.Parameters.AddWithValue("SNAME", "");
// Open connections to excel sheet and foxpro database
exportConnection.Open();
importConnection.Open();
Console.WriteLine("Visual Foxpro connection open");
Console.WriteLine("Writing to table");
Console.WriteLine("...");
int nLoopCount = 0;
string space = " ";
// Initiate the reader to excel
exportReader = exportCommand.ExecuteReader();
// Start reading
while (exportReader != null && exportReader.Read())
{
//Set parameter values whilst reading from excel
string LandTitle = exportReader.IsDBNull(0)
? string.Empty
: Convert.ToString(exportReader.GetValue(0)).Trim();
string LandFname = exportReader.IsDBNull(1)
? string.Empty
: Convert.ToString(exportReader.GetValue(1)).Trim();
string LandSname = exportReader.IsDBNull(2)
? string.Empty
: Convert.ToString(exportReader.GetValue(2));
string CLCODE = string.Concat(LandFname, space, LandSname, " (P)").Trim();
Console.WriteLine("Working on record {0}, {1}", IDCOUNT, CLCODE);
importCommand.Parameters["CLCODE"].Value = string.Concat(LandFname, space, LandSname, " (P)").Trim();
importCommand.Parameters["CLCODEDESC"].Value = string.Concat(LandTitle, space, LandFname, space, LandSname).Trim();
importCommand.Parameters["TITLE"].Value = LandTitle.Trim();
importCommand.Parameters["FNAME"].Value = LandFname.Trim();
importCommand.Parameters["SNAME"].Value = LandSname.Trim();
try
{
importCommand.ExecuteNonQuery();
}
catch (Exception e)
{
Console.Write("Error Writing to database");
Console.Write(e);
Console.ReadKey();
}
// We must close and re-open the connection after a certain number of records or the OLEDB FoxPro SQL will eventually fail
if (nLoopCount % 100 == 0)
{
importConnection.Close();
importConnection.Open();
}
}
// done
exportConnection.Close();
importConnection.Close();
Console.WriteLine("Landlord Import Complete!");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
if (Settings.ImportPropertyPause) Console.ReadKey();
}
}
}
}
我想要做的事,如:
if (CLCODE exists)
{
Do not not create record
}
else
{
create record
}
只需对该项目执行SELECT操作 - 如果返回结果,则存在,如果不存在,则不存在。 – Tim
有没有一个* single *字段值可以在目标表中检查以查看该记录是否已经存在?如果是这样,做一个“选择*从表where字段='等等'”,看看它是否找到记录。如果需要*多个*字段值来创建检查现有记录所需的唯一性,那么只需包含所有字段名称及其值,然后检查是否存在以下记录:“Select * from Table where field1 ='blah1'和field2 ='blah2'“ – MattSlay
欢呼声,我想我现在正在做些什么,如果我解决了它,我会发布答案 –