请教个问题,下面是我的数据库更新语句,但在运行中,只要改一条数据项,保存成功,该数据表内,所有客户的对应数据项就全改了,这个需要增加一个where id的条件吗,请指导一下;
```c#
private void btnupsave_Click(object sender, EventArgs e)
{
string whudate = dateTimePicker1.Text;
//string whudate = dateTimePicker1.Value.ToString("yyyy-mm-dd");
string whubatchNo = whbatchNo.Text;
string whuprocess = comuprocess.Text;
string whuname = comupname.Text;
string whulh = whupulh.Text;
string whnote = whupnote.Text;
//SqlTransaction transaction = conn.BeginTransaction();
try
{
conn = new SqlConnection("server=192.168.100.247;database= Whmesinfo;user=sa;password=whyy@2021 ");
conn.Open();
string SqlString = @"update [dbo].[w_workerhour]
set date=@date,batchNo=@batchNo,process=@process,name=@name,dulh=@dulh,note=@note ";
SqlCommand comm = new SqlCommand(SqlString, conn);//需要加上回滚事务;
comm.Parameters.AddWithValue("@date",DateTime.Parse(whudate)); // 替换为实际的日期值,以下相同;
comm.Parameters.AddWithValue("@batchNo", whubatchNo);
comm.Parameters.AddWithValue("@process", whuprocess);
comm.Parameters.AddWithValue("@name", whuname);
comm.Parameters.AddWithValue("@dulh", whulh);
comm.Parameters.AddWithValue("@note", whnote);
comm.ExecuteNonQuery();
//transaction.Commit();
MessageBox.Show("数据保存成功!");
}
catch (Exception)
{
MessageBox.Show("更新失败");
//transaction.rollback();
}
finally { conn.Close(); }
}
```
update 后面不跟where条件表示是全部数据的更新,跟上where 比如 id=xxx 这样是针对一条数据的更新
直接在24行的语句里加就行了,不过你为什么要这么麻烦的用Parameters,直接写在sql语句里不行吗?还是你要要保存的数据里有图片之类的?
【相关推荐】
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;
public class IdGenerator {
//选择日期格式
static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
//封装时间戳加随机数方法
public static String timestamp() {
//获取当前时间
LocalDateTime now = LocalDateTime.now();
//使用指定日期格式
String format = now.format(formatter);
//返回当前时间戳+三位随机数策略
return format + (ThreadLocalRandom.current().nextInt(999) + 1);
}
//遍历,测试(效果同下)
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
String format = now.format(formatter);
for (int i = 0; i < 10; i++) {
System.out.println(format + (ThreadLocalRandom.current().nextInt(999) + 1));
}
System.out.println("=======================================");
//使用静态方法调用封装方法
for (int j=0;j<10;j++){
System.out.println(IdGenerator.timestamp());
}
}
}
测试结果
多测几次可见,其特点是可以生成20位以内的id,而且基本趋于增势,而且是整数id,安全性一般,但是在性能上还是有一定保证,出现不足20位的原因是秒值和毫秒值有可能出现为0的情况,所以只能说基本趋于增势,在并发量不是超高的情况下还是可以考虑的,但是如果要求id位数固定,就要自行改造了,就要补0或者换一种时间戳实现,特别是如果是税务要求固定20位的时候。其数据库字段可以对应bigint。可修改为以下方法可满足: