我有一个试图插入记录的代码。插入所有记录,但重复。我想停止重复相同的记录。
public int SaveReadingsInCIS(Reading pReading)
{
SqlConnection con = new SqlConnection("Data Source=RND-FAISAL;Initial Catalog=hesco;Integrated Security=True;MultipleActiveResultSets=True;");
int result = -1;
string dt = pReading.XTimeStamp.Replace("T"," ");
string fdt = dt.Remove(dt.Length-6);
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand insert_cmd = new SqlCommand("INSERT INTO [dbo].[billing]([unique_id],[ref_no],[msn],"+
"[kWh1],[kVarh1],[kWh2],[kVarh2],[datetime])VALUES(@uniqueid,@refno,@msn,@kwh1,@kvarh1,@kwh2,@kvarh2,@datetime)",con);
insert_cmd.CommandType = CommandType.Text;
insert_cmd.Parameters.AddWithValue("@uniqueid",pReading.App_no);
insert_cmd.Parameters.AddWithValue("@refno",pReading.Ref_no);
insert_cmd.Parameters.AddWithValue("@msn",pReading.Serial_no);
insert_cmd.Parameters.AddWithValue("@kwh1",pReading.KWH1);
insert_cmd.Parameters.AddWithValue("@kvarh1",pReading.KVARH1);
insert_cmd.Parameters.AddWithValue("@kwh2",pReading.KWH2);
insert_cmd.Parameters.AddWithValue("@kvarh2",pReading.KVARH2);
insert_cmd.Parameters.AddWithValue("@datetime", fdt.ToString());
try
{
result = insert_cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
result = ex.HResult;
}
finally
{
if (con != null && con.State == ConnectionState.Open)
{
con.Close();
}
}
return result;
}
上面的代码中有一个日期时间fdt
。可能有重复的msn
,但日期时间不同。我想停止重复插入相同日期时间和msn
任何帮助将不胜感激