编程序
后面的分別改為txtname spec price
雙擊保存
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;//連接數據
using System.Configuration;
namespace ERPforms
{
public partial class Form1 : Form
{
//定義錯誤信息
string[] strMessage = new string[]{
"編號不能為空!","物料不能為空!","價格或輸入格式不對!"
};
public Form1()
{
InitializeComponent();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//保存數據(新增物料)
private void btmsave_Click(object sender, EventArgs e)
{
if (Validation() != -1)
MessageBox.Show(strMessage[Validation()]);
else
{
AddItems();
}
}
//添加物料信息
private void AddItems()
{
//連接數據庫
string connString=ConfigurationManager.ConnectionStrings["connString"].ToString();
SqlConnection conn=new SqlConnection(connString);//實例化連接對象
string sql="insert into Items values('"+txtNo.Text+"',"+"'"+txtName.Text+"','"+txtSpec.Text+"',"+txtPrice.Text+")";
//數據庫操作
conn.Open();//打開連接
try
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
conn.Close();
}
}
//數據驗證
private int Validation()
{
int price = 0;
int status = -1;
if (txtNo.Text == "")//驗證編號
status = 0;
else if (txtName.Text == "")
status = 1;
else if (txtPrice.Text == "" &&
int.TryParse(txtPrice.Text, out price))
status = 2;
return status;
}
}
}
總結
- 上一篇: Edit button clicked
- 下一篇: when is completed fi