代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace YiNeng
{
public partial class CommonSaleForm : Form
{
public CommonSaleForm()
{
InitializeComponent();
}
private void CommonSaleForm_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“yiNengDataSet.普通销售单”中。您可以根据需要移动或移除它。
this.普通销售单TableAdapter.Fill(this.YiNengDataSet.普通销售单);
}
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
String MySQLConnectionString = global::YiNeng.Properties.Settings.Default.YiNengConnectionString;
string MySQL = "INSERT INTO 普通销售单(普通销售单号,顾客姓名,商品编号,商品名称,单位,颜色,单价,数量,实收金额,经办人,日期,备注)VALUES('";
MySQL += this.普通销售单号textBox.Text + "','";
MySQL += this.顾客姓名textBox.Text + "','";
MySQL += this.商品编号textBox.Text + "','";
MySQL += this.商品名称textBox.Text + "','";
MySQL += this.单位textBox.Text + "','";
MySQL += this.颜色textBox.Text + "','";
MySQL += this.单价textBox.Text + "','";
MySQL += this.数量textBox.Text + "','";
MySQL += this.实收金额textBox.Text + "','";
MySQL += this.经办人textBox.Text + "','";
MySQL += this.日期dateTimePicker1.Value.ToString() + "','";
MySQL += this.备注textBox.Text + "');";
SqlConnection MyConnection = new SqlConnection(MySQLConnectionString);
MyConnection.Open();
SqlCommand MyCommand = MyConnection.CreateCommand();
MyCommand.CommandText = MySQL;
MyCommand.ExecuteNonQuery();
if (MyConnection.State == ConnectionState.Open)
{
MyConnection.Close();
}
}
}
}
SQL销售单主键设置为商品编号
运行可打开主界面
但是当按了保存button后,
C#提示如下
违反了 PRIMARY KEY 约束 'PK_普通销售单'。不能在对象 '普通销售单' 中插入重复键。
语句已终止。
刚刚入行的菜鸟。。。请各位大侠多多帮忙。。。先谢