加一个 时间戳 TimeStamp 可以解决 重复提交问题 SqlServer

 public partial class Form1 : Form
{
private SqlConnection mCnn = null;
private long TimeStampValue;

public Form1()
{
InitializeComponent();
mCnn = new SqlConnection();
mCnn.ConnectionString = "Data Source=192.168.18.205;Database=" +
"Test;Uid=sa;Pwd=kicpassword";
mCnn.Open();
}

//读取

private void btnReadTimeStamp_Click(object sender, EventArgs e)
{
//使用convert(bigint,FTimeStamp) MyTimeStamp把FTimeStamp转换为bigint整数类型
string strSQL = "Select Top 1 FCaption,convert(bigint,FTimeStamp) MyTimeStamp,FTimeStamp From t_TimeStamp";
SqlCommand Cmd = new SqlCommand(strSQL, mCnn);
SqlDataReader Reader = Cmd.ExecuteReader();
while (Reader.Read())
{
long value = (long)Reader["MyTimeStamp"];
TimeStampValue = value; //在此处把TimeStamp的值赋值给变量,用于更新时判断
txtTimeStamp.Text = value.ToString();
txtTimeStampHex.Text=value.ToString("X16");
}
Reader.Close();
Reader = null;
}

//更新

private void btnUpdateTimeStamp_Click(object sender, EventArgs e)
{
//Where条件加上TimeStamp的判断,必须和读取的一致,用于并发操作的重复提交
string strSQL = "Update t_TimeStamp Set FCaption = '修改记录'" +
" Where FTimeStamp =" + TimeStampValue.ToString();
SqlCommand cmd = new SqlCommand(strSQL, mCnn);
int UpdateCount = cmd.ExecuteNonQuery();
if (UpdateCount <= 0)
{
MessageBox.Show("更新失败!");
}
else
{
MessageBox.Show("更新成功!");
}
}
}

你可能感兴趣的:(sqlserver)