C# 窗体 急急急 通过一个ID号可以同时向数据库中插入多条不一致的数据
我现在想做一个小程序,用户通过扫描一个物品的ID号,在数据库添加一条记录。但是,现在要求多人同时扫描这一个物品的ID号向数据库中添加长短不一致的多条数据,
而且已经存在的就不能插入了!在插入时先判断存在就更新,不存在就添加。单一条的我会!
多条数据不会啊!求大神指点!{:1_893:}
如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using Microsoft.VisualBasic;
using System.Data.SqlClient;
namespace RS_扫描
{
public partial class Form2 : Form
{
SQlHelper db = new SQlHelper();
public Form2()
{
//初始化窗体基本信息
InitializeComponent();
}
private void Form2_Load(object sender, System.EventArgs e)
{
//窗体第一次被初始化。(窗体标题/窗体大小/是否允许最大化,最小化)
select();
}
public void select()
{
//SqlConnection con = new SqlConnection("server=.;database=rs;uid=sa;pwd=eslrsdata");
////将连接打开
//con.Open();
////执行con对象的函数,返回一个SqlCommand类型的对象
//SqlDataAdapter sda = new SqlDataAdapter("select * from rsdat order by id desc", con);
//DataSet ds = new DataSet();
//sda.Fill(ds);
//dataGridView1.DataSource = ds.Tables;
string sql = "select * from rsdat order by id desc";
DataTable dt = db.ExcuteQuery("SqlServer", sql);
dataGridView1.DataSource = dt;
System.Drawing.Font font = new System.Drawing.Font("UTF-8", 7);//UTF-8是字体的编码格式,2是字体大小
dataGridView1.Font = font;//此时dataGridView的字体就已经设置完成
int width = 0;
for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
//将每一列都调整为自动适应模式
this.dataGridView1.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
//记录整个DataGridView的宽度
width += this.dataGridView1.Columns.Width;
}
//判断调整后的宽度与原来设定的宽度的关系,如果是调整后的宽度大于原来设定的宽度,
//则将DataGridView的列自动调整模式设置为显示的列即可,
//如果是小于原来设定的宽度,将模式改为填充。
if (width > this.dataGridView1.Size.Width)
{
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
}
else
{
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
dataGridView1.Columns.Width = 35;
}
private void button1_Click(object sender, EventArgs e)
{
string mcs = textBox1.Text;
string jsmds = textBox2.Text;
string sgmcs = textBox3.Text;
string sgs = textBox4.Text;
string dyxmcs = textBox5.Text;
string dyxs = textBox6.Text;
string umdmcs = textBox7.Text;
string umds = textBox8.Text;
string zmdmcs = textBox9.Text;
string zmds = textBox10.Text;
List<Priview> index = new List<Priview>();
if (textBox1.Text.Trim() != "")
{
index.Add(new Priview { column = "mc", columnValue = textBox1.Text });
if (textBox2.Text.Trim() != "")
{
index.Add(new Priview { column = "jsmd", columnValue = textBox2.Text });
}
else if (textBox4.Text.Trim() != "")
{
index.Add(new Priview { column = "sg", columnValue = textBox4.Text });
}
else if (textBox6.Text.Trim() != "")
{
index.Add(new Priview { column = "dyx", columnValue = textBox6.Text });
}
else if (textBox8.Text.Trim() != "")
{
index.Add(new Priview { column = "umd", columnValue = textBox8.Text });
}
else if (textBox10.Text.Trim() != "")
{
index.Add(new Priview { column = "zmd", columnValue = textBox10.Text });
}
}
string columns = string.Empty;
string columnValues = string.Empty;
foreach (var item in index)
{
columns += item.column + ",";
columnValues += "\'" + item.columnValue + "\',";
}
//去掉字符串的最后一个逗号
columns = columns.TrimEnd(',');
columnValues = columnValues.TrimEnd(',');
//insert into rsdat(mc,jsmd) values('','');
string sql = "insert into rsdat(" + columns + ") values(" + columnValues + ")";
bool num = db.ExcuteSql("SqlServer", sql);
if (num)
{
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
}
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
select();
//con.Close();
}
public class Priview
{
public string column { get; set; }
public string columnValue { get; set; }
}
//清空文本框内容
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
textBox10.Text = "";
select();
}
//查询MC号的值并将数据库的值赋值给其他文本框
private void button3_Click(object sender, System.EventArgs e)
{
string mcs = textBox1.Text;
string jsmds = textBox2.Text;
string sgmcs = textBox3.Text;
string sgs = textBox4.Text;
string dyxmcs = textBox5.Text;
string dyxs = textBox6.Text;
string umdmcs = textBox7.Text;
string umds = textBox8.Text;
string zmdmcs = textBox9.Text;
string zmds = textBox10.Text;
#region
//SqlConnection con = new SqlConnection("server=.;database=rs;uid=sa;pwd=eslrsdata");
//con.Open();
//SqlDataAdapter sda = new SqlDataAdapter("select * from rsdat where mc='" + mcs + "'", con);
//DataSet ds = new DataSet();
//sda.Fill(ds);
//dataGridView1.DataSource = ds.Tables;
////赋值
//textBox1.Text = ds.Tables.Rows["mc"].ToString();
//textBox2.Text = ds.Tables.Rows["jsmd"].ToString();
//textBox3.Text = ds.Tables.Rows["sgmc"].ToString();
//textBox4.Text = ds.Tables.Rows["sg"].ToString();
//textBox5.Text = ds.Tables.Rows["dyxmc"].ToString();
//textBox6.Text = ds.Tables.Rows["dyx"].ToString();
//textBox7.Text = ds.Tables.Rows["umdmc"].ToString();
//textBox8.Text = ds.Tables.Rows["umd"].ToString();
//textBox9.Text = ds.Tables.Rows["zmdmc"].ToString();
//textBox10.Text = ds.Tables.Rows["zmd"].ToString();
//con.Close();
#endregion
string sql = "select * from rsdat where mc='" + mcs + "'";
DataTable dt = db.ExcuteQuery("SqlServer", sql);
dataGridView1.DataSource = dt;
textBox1.Text = dt.Rows["mc"].ToString();
textBox2.Text = dt.Rows["jsmd"].ToString();
textBox3.Text = dt.Rows["sgmc"].ToString();
textBox4.Text = dt.Rows["sg"].ToString();
textBox5.Text = dt.Rows["dyxmc"].ToString();
textBox6.Text = dt.Rows["dyx"].ToString();
textBox7.Text = dt.Rows["umdmc"].ToString();
textBox8.Text = dt.Rows["umd"].ToString();
textBox9.Text = dt.Rows["zmdmc"].ToString();
textBox10.Text = dt.Rows["zmd"].ToString();
textBox1.Focus();
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "")
{
MessageBox.Show("信息不能为空,请输入正确的值", "提示");
return;
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Text != "")
{
string sql = "select * from rsdat where mc = '" + textBox1.Text + "'";
DataTable dt = db.ExcuteQuery("SqlServer", sql);
if (dt.Rows.Count > 0)
{
textBox1.Text = dt.Rows["mc"].ToString();
textBox2.Text = dt.Rows["jsmd"].ToString();
textBox3.Text = dt.Rows["sgmc"].ToString();
textBox4.Text = dt.Rows["sg"].ToString();
textBox5.Text = dt.Rows["dyxmc"].ToString();
textBox6.Text = dt.Rows["dyx"].ToString();
textBox7.Text = dt.Rows["umdmc"].ToString();
textBox8.Text = dt.Rows["umd"].ToString();
textBox9.Text = dt.Rows["zmdmc"].ToString();
textBox10.Text = dt.Rows["zmd"].ToString();
textBox1.Focus();
}
else
{
string mcs = textBox1.Text;
textBox2.Focus();
}
}
}
}
}
}
在玩看来您就是大神了,你发的东西我看不懂
页:
[1]