【发布时间】:2017-01-23 20:48:06
【问题描述】:
我创建了一个程序,它从 excel 电子表格中提取数据并将其推送到 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 - 如果返回结果,则它存在,否则,它不存在。
-
是否有一个 single 字段值可以在目标表中检查以查看记录是否已存在?如果是这样,请执行“Select * from Table where field = 'blah'”并查看它是否找到记录。如果需要 多个 字段值来创建检查现有记录所需的唯一性,则只需包含所有字段名称及其值并检查是否已存在记录:" Select * from Table where field1 = 'blah1' and field2 = 'blah2' "
-
干杯,我想我现在正在做一些事情,如果我解决它会发布答案
-
您甚至不需要 SELECT。只需将
WHERE子句添加到您的INSERT。
标签: c# visual-foxpro