using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SetTools { public partial class FormData : Form { public String ConSql = @"Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Directory.GetCurrentDirectory() + "\\rqdata.mdb"; public int UOP_ID = 0;//操作时记录ID SerialPort sp1 = new SerialPort();//sp1.ReceivedBytesThreshold = 1;//只要有1个字符送达端口时便触发DataReceived事件 public FormData() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //插入到数据库 //定义字段 //string UL = FormMain.UOP_ID;//人员编码ID string UA = textBox1.Text;//测试人员 string UB = textBox2.Text;//磁条编号 string UC = textBox3.Text;//天气 string UD = textBox4.Text;//名称 string UE = textBox5.Text;//型号规格 string UF = textBox6.Text;//测试设备 string UG = textBox7.Text;//注意事项 string UH = textBox8.Text;//联系方式 string UI = textBox9.Text;//材质 string UJ = textBox10.Text;//制造商 string UK = textBox11.Text;//测试地址 string UL = textBox12.Text;//温度 string UM = textBox13.Text;//测试时间 string UN = textBox14.Text;//步长 string UO = textBox15.Text;//运行模式 string UP = textBox16.Text;//备注 //插入数据库 OleDbConnection conn; conn = new OleDbConnection(ConSql); conn.Open(); //建立SQL语句 string sqlstr = ""; sqlstr = @"INSERT INTO 测试信息([测试人员],[磁条编号],[天气],[名称],[型号规格],[测试设备],[注意事项],[联系方式],[材质],[制造商],[测试地址],[温度],[测试时间],[测试编号],[步长],[运行模式],[备注]) VALUES("; sqlstr += "'" + UA + "','" + UB + "','" + UC + "','" + UD + "','" + UE + "','" + UF + "','" + UG + "','" + UH + "','" + UI + "','" + UJ + "','" + UK + "','" + UL + "','" + UM + "','" + UOP_ID + "','" + UN + "','" + UO + "','" + UP + "')"; OleDbCommand cmd = conn.CreateCommand(); cmd.CommandText = sqlstr; cmd.ExecuteNonQuery(); //获取当前插入数据的ID cmd.CommandText = "SELECT @@IDENTITY"; //public int UOP_ID = 0;//操作时记录ID //UOP_ID = (int)cmd.ExecuteScalar(); conn.Close(); //插入数据库 OleDbConnection comn; comn = new OleDbConnection(ConSql); comn.Open(); //建立SQL语句 string cqlstr = ""; cqlstr = @"UPDATE 磁条测试记录 set [磁条编号] = '" + textBox2.Text + "'"; cqlstr += " where [ID] = " + UOP_ID + ""; OleDbCommand smd = comn.CreateCommand(); smd.CommandText = cqlstr; smd.ExecuteNonQuery(); comn.Close(); this.Close(); } private void textBox13_TextChanged(object sender, EventArgs e) { this.textBox13.Text = System.DateTime.Now.ToString("g"); } } }