C#批量写入SQL的问题
用的listView导入了所有数据库表数据做了修改和添加
在listView插入SQL的时候出现主键重复问题,我想把重复的主键值做一个判断
判断主键值的信息是否被修改,有改动就更新数据
主键值没改动,就跳过继续执行下一条数据写入
具体要怎么写呀!!!
现在写入SQL的部分已经写好了,就是不知道判断如何写,重哪插入
private void button8_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(TBConnection.Text);
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
foreach (ListViewItem Item in listView1.Items)
{
string simNo = Item.SubItems[0].Text;
int startHour = int.Parse(Item.SubItems[1].Text);
int startMinute = int.Parse(Item.SubItems[2].Text);
int endHour = int.Parse(Item.SubItems[3].Text);
int endMinute = int.Parse(Item.SubItems[4].Text);
int retSpan = int.Parse(Item.SubItems[5].Text);
int retSpanA = int.Parse(Item.SubItems[6].Text);
int retSpanB = int.Parse(Item.SubItems[7].Text);
= string.Format("INSERT into Mobile(SimNo,StartHour,StartMinute,EndHour,EndMinute,RetSpan,RetSpan_A,RetSpan_B) values('{0}',{1},{2},{3},{4},{5},{6},{7})", simNo, startHour, startMinute, endHour, endMinute, retSpan,retSpanA,retSpanB);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (connection != null && connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}