请问一下,我的这段代码,是从ERP数据库内提取产品的入库数量,并保存到指定的另一个生产软件的数据表里面,以做报表用,我们是以“名称(proName)、批号(batchNo)、规格(spec)等这几项为判断依据,若是新数据就正常保存,若是重复数据,就直接跳过不保存,请问以下代码是否正确,敬请指导一下;
private void btnK3_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection conn = new SqlConnection("server=192.168.2.5;database=AIS20150211135927;user=sa;password=zzwr"))
{
conn.Open();
SqlConnection conn1 = new SqlConnection("server=192.168.100.247;database=WHMesInfo;user=sa;password=whyy");
conn1.Open();
// 获取读取到的字段值
DateTime startdate = dateTimePicker1.Value.Date;
DateTime Enddate = dateTimePicker2.Value.Date;
string selectSql = @"select FName,FModel,FHeadSelfA0228,sum(Fauxqty) Fauxqty,sum(FEntrySelfA0242) FEntrySelfA0242 from
(select t2.FDate,t4.FName,t4.FModel,t2.FHeadSelfA0228,t1.Fauxqty,t1.FEntrySelfA0242
from ICStockBillEntry t1
join ICStockBill t2 on t1.FInterID=t2.FInterID
join t_ICItem t4 on t1.FItemID=t4.FItemID ----(产品表,包含产品名称、编码等信息)
join ICTransactionType t5 on t2.FTranType=t5.FID
join t_Stock t6 on t1.FDCStockID=t6.FItemID ---仓库信息
where t2.FTranType=2 --(产品入库单,不含外部入库等信息)
and (t6.FName='有效成品库'or t6.FName='辅料库')
and t2.FDate >= @startDate
and t2.FDate <= @endDate) as subquery
group by FName,FModel,FHeadSelfA0228";
using (SqlCommand selectCommand = new SqlCommand(selectSql, conn))
{
selectCommand.Parameters.AddWithValue("@startdate", startdate);
selectCommand.Parameters.AddWithValue("@Enddate", Enddate);
DataTable dataTable = new DataTable();
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand))
{
dataAdapter.Fill(dataTable);
}
// 遍历DataTable中的每一行,并插入到w_protran表中
foreach (DataRow row in dataTable.Rows)
{
// 将读取ERP数据,插入MES[w_k3tran]数据表之前,先检查是否存在重复记录;
string existingDataSql = @"SELECT COUNT(*) FROM [dbo].[w_k3tran]
WHERE [batchNo] = @batchNo
AND [proname] = @proname
AND [instock] = @instock
AND [spec] = @spec
AND [sample] = @sample";
using (SqlCommand checkCommand = new SqlCommand(existingDataSql, conn1))
{
//checkCommand.Parameters.AddWithValue("@date", row["FDate"]);
checkCommand.Parameters.AddWithValue("@batchNo", row["FHeadSelfA0228"]);
checkCommand.Parameters.AddWithValue("@proname", row["FName"]);
checkCommand.Parameters.AddWithValue("@spec", row["FModel"]);/
checkCommand.Parameters.AddWithValue("@instock", row["Fauxqty"]);
checkCommand.Parameters.AddWithValue("@sample", row["FEntrySelfA0242"]);
int count = Convert.ToInt32(checkCommand.ExecuteScalar());
if (count > 0)
{
//MessageBox.Show("对不起!历史记录有重复,请检查确认!");
continue;//20240603修改为,遇到重复自动跳过;
}
}
//插入到新数据表的对应字段值
string insertSql = @"INSERT INTO [dbo].[w_k3tran]([batchNo],[proname],[spec],[instock],[sample])
VALUES ( @batchNo, @proname,@spec,@instock,@sample)";
using (SqlCommand insertCommand = new SqlCommand(insertSql, conn1))
{
//insertCommand.Parameters.AddWithValue("@date", row["FDate"]);
insertCommand.Parameters.AddWithValue("@batchNo", row["FHeadSelfA0228"]);
insertCommand.Parameters.AddWithValue("@proname", row["FName"]);
insertCommand.Parameters.AddWithValue("@spec", row["FModel"]);
insertCommand.Parameters.AddWithValue("@instock", row["Fauxqty"]);
//insertCommand.Parameters.AddWithValue("@outstock", row["outstock"]);
insertCommand.Parameters.AddWithValue("@sample", row["FEntrySelfA0242"]);
insertCommand.ExecuteNonQuery();
}
}
MessageBox.Show("插入成功");
}
conn.Close();
conn1.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}