如何在将特定数据插入到 SQL 数据库 table 之前检查文本文件中是否存在特定数据?
How to check if a specific data exist in a text file before insert it to SQL database table?
我有一个员工文本文件 employee.txt
,其中包含一些数据。
例如:
empID, FirstName, LastNme
我想读取文件并从中获取 empId
并将其传递给函数以检查 SQL 数据库中是否已经存在 empId
table.
这是我的函数:
public bool DoesEmployeeExist(int empId)
{
bool ret;
try
{
string sql = "Select empId from Employees where empId = '" + empId + "'";
Object obj = _idac.GetSingleAnswer(sql);
if (obj != null)
{
ret = true;
}
else
{
ret = false;
}
}
catch (Exception)
{
throw;
}
return ret;
}
如何从文件中获取 empId
?
如果 emptId
还没有,如何将文件插入 SQL 数据库 table?
我是否应该将 employee.txt
文件作为字符串传递给这样的函数并使用批量插入将数据添加到数据库中:
public void InsertOrUpdateEmployee(string employees)
{
string sql = "BULK INSERT Employee FROM '" + employees + "'" +
" WITH(FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')";
}
或将其作为
List<Employees> employee = new <Employees>();
根据您的问题,如果
您希望将文件中的 empId 插入数据库
empId 不在数据库中。
您可以使用以下代码获取。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Employee> list = new List<Employee>();
var result = File.ReadAllLines("D:\new.txt");
string[] s = { "," };
foreach (var item in result)
{
string[] arr = item.Split(s, StringSplitOptions.RemoveEmptyEntries);
list.Add(new Employee { EmpId = Convert.ToInt32(arr[0]), FirstName = arr[1], LastName = arr[2] });
}
foreach (Employee item in list)
{
bool t = DoesEmployeeExist(item.EmpId);
if(t==true)
{
MessageBox.Show("Database already has the record "+item.EmpId);
}
else
{
string sql = string.Format("insert into Employees (empID,FirstName,LastName)values({0},'{1}','{2}')", item.EmpId, item.FirstName, item.LastName);
string connstring = @"";
SqlConnection connection = new SqlConnection(connstring);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
connection.Close();
}
}
MessageBox.Show("Test");
}
public bool DoesEmployeeExist(int empId)
{
try
{
string sql = "Select empID from Employees where empID = '" + empId + "'";
string connstring = @"";
SqlConnection connection = new SqlConnection(connstring);
connection.Open();
SqlCommand command = new SqlCommand(sql,connection);
SqlDataReader dr = command.ExecuteReader();
if(dr.HasRows)
{
return true;
}
else
{
return false;
}
}
catch (Exception)
{
throw;
}
}
}
public class Employee
{
public int EmpId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
下一个是我的txt文件:
接下来是我的数据库记录:
我有一个员工文本文件 employee.txt
,其中包含一些数据。
例如:
empID, FirstName, LastNme
我想读取文件并从中获取 empId
并将其传递给函数以检查 SQL 数据库中是否已经存在 empId
table.
这是我的函数:
public bool DoesEmployeeExist(int empId)
{
bool ret;
try
{
string sql = "Select empId from Employees where empId = '" + empId + "'";
Object obj = _idac.GetSingleAnswer(sql);
if (obj != null)
{
ret = true;
}
else
{
ret = false;
}
}
catch (Exception)
{
throw;
}
return ret;
}
如何从文件中获取 empId
?
如果 emptId
还没有,如何将文件插入 SQL 数据库 table?
我是否应该将 employee.txt
文件作为字符串传递给这样的函数并使用批量插入将数据添加到数据库中:
public void InsertOrUpdateEmployee(string employees)
{
string sql = "BULK INSERT Employee FROM '" + employees + "'" +
" WITH(FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')";
}
或将其作为
List<Employees> employee = new <Employees>();
根据您的问题,如果
您希望将文件中的 empId 插入数据库empId 不在数据库中。
您可以使用以下代码获取。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Employee> list = new List<Employee>();
var result = File.ReadAllLines("D:\new.txt");
string[] s = { "," };
foreach (var item in result)
{
string[] arr = item.Split(s, StringSplitOptions.RemoveEmptyEntries);
list.Add(new Employee { EmpId = Convert.ToInt32(arr[0]), FirstName = arr[1], LastName = arr[2] });
}
foreach (Employee item in list)
{
bool t = DoesEmployeeExist(item.EmpId);
if(t==true)
{
MessageBox.Show("Database already has the record "+item.EmpId);
}
else
{
string sql = string.Format("insert into Employees (empID,FirstName,LastName)values({0},'{1}','{2}')", item.EmpId, item.FirstName, item.LastName);
string connstring = @"";
SqlConnection connection = new SqlConnection(connstring);
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
connection.Close();
}
}
MessageBox.Show("Test");
}
public bool DoesEmployeeExist(int empId)
{
try
{
string sql = "Select empID from Employees where empID = '" + empId + "'";
string connstring = @"";
SqlConnection connection = new SqlConnection(connstring);
connection.Open();
SqlCommand command = new SqlCommand(sql,connection);
SqlDataReader dr = command.ExecuteReader();
if(dr.HasRows)
{
return true;
}
else
{
return false;
}
}
catch (Exception)
{
throw;
}
}
}
public class Employee
{
public int EmpId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
下一个是我的txt文件:
接下来是我的数据库记录: