|
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 Catering
{
public partial class Manager : Form
{
public Manager()
{
InitializeComponent();
}
String button = "";
DataSet ds = new DataSet();
BindingSource bd = new BindingSource();
SqlConnection cn = new SqlConnection();
private void Manager_Load(object sender, EventArgs e)
{
cn.ConnectionString = @"Data Source=./;Initial Catalog=Catering;Integrated Security=True;";
cn.Open();
SqlCommand cm = new SqlCommand("SELECT * FROM Cookbook", cn);
SqlDataAdapter da = new SqlDataAdapter(cm);
da.Fill(ds, "Cookbook");
bd.DataSource = ds.Tables["Cookbook"];
this.CookDataGridView.DataSource = bd;
this.idtext.DataBindings.Add("Text", bd, "Cookid");
this.nametext.DataBindings.Add("Text", bd, "Cookname");
this.pricetext.DataBindings.Add("Text", bd, "Cookprice");
this.foodidtext.DataBindings.Add("Text", bd, "FoodId");
}
private void preserve()
{
idtext.ReadOnly = true;
nametext.ReadOnly = true;
pricetext.ReadOnly = true;
foodidtext.ReadOnly = true;
Add.Enabled = true;
Delete.Enabled = true;
Mofity.Enabled = true;
Confirm.Enabled = false;
Cancel.Enabled = true;
Reset.Enabled = false;
}
private void clear()
{
idtext.ReadOnly = false;
nametext.ReadOnly = false;
pricetext.ReadOnly = false;
foodidtext.ReadOnly = false;
Add.Enabled = false;
Delete.Enabled = false;
Mofity.Enabled = false;
Confirm.Enabled = true;
Cancel.Enabled = true;
Reset.Enabled = true;
}
private void insert()//插入数据方法
{
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "inserttable";
cm.Parameters.Add("@Cookid", SqlDbType.Int).Value = this.idtext.Text;
cm.Parameters.Add("@Cookname", SqlDbType.NVarChar).Value = this.nametext.Text;
cm.Parameters.Add("@Cookprice", SqlDbType.Int).Value = this.pricetext.Text;
cm.Parameters.Add("@FoodId", SqlDbType.Int).Value = this.foodidtext.Text;
cm.ExecuteNonQuery();
MessageBox.Show("添加记录成功!", "记录添加", MessageBoxButtons.OK);
}
private void update()//更新数据方法
{
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "updatetable";
cm.Parameters.Add("@Cookid", SqlDbType.Int).Value = this.idtext.Text;
cm.Parameters.Add("@Cookname", SqlDbType.NVarChar).Value = this.nametext.Text;
cm.Parameters.Add("@Cookprice", SqlDbType.Int).Value = this.pricetext.Text;
cm.Parameters.Add("@FoodId", SqlDbType.Int).Value = this.foodidtext.Text;
cm.ExecuteNonQuery();
MessageBox.Show("修改记录成功!", "记录修改", MessageBoxButtons.OK);
}
private void delete()//删除数据方法
{
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deletetable";
cm.Parameters.Add("@Cookid", SqlDbType.Int).Value = this.idtext.Text;
cm.Parameters.Add("@Cookname", SqlDbType.NVarChar).Value = this.nametext.Text;
cm.Parameters.Add("@Cookprice", SqlDbType.Int).Value = this.pricetext.Text;
cm.Parameters.Add("@FoodId", SqlDbType.Int).Value = this.foodidtext.Text;
cm.ExecuteNonQuery();
MessageBox.Show("删除记录成功!", "记录删除", MessageBoxButtons.OK);
}
private void Add_Click(object sender, EventArgs e)
{
button = "Add";
clear();
allclear();
}
private void Delete_Click(object sender, EventArgs e)
{
delete();
ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[bd.Position]);
}
private void Mofity_Click(object sender, EventArgs e)
{
button = "Mofity";
clear();
}
private void Confirm_Click(object sender, EventArgs e)
{
switch (button)
{
case "Add":
insert();
ds.Tables[0].RejectChanges();
DataRow r;
try
{
r = ds.Tables["Cookbook"].NewRow();
r["Cookid"] = this.idtext.Text.ToString();
r["Cookname"] = this.nametext.Text.ToString();
r["Cookprice"] = this.pricetext.Text.ToString();
r["FoodId"] = this.foodidtext.Text.ToString();
ds.Tables["Coolbook"].Rows.Add(r);
}
catch (Exception ex)
{
}
preserve();
break;
case "Mofity":
update();
break;
}
ds.Tables[0].Rows[bd.Position].AcceptChanges();
}
private void Cancel_Click(object sender, EventArgs e)
{
ds.RejectChanges();
preserve();
}
private void Reset_Click(object sender, EventArgs e)
{
allclear();
}
private void allclear()
{
nametext.Clear();
foodidtext.Clear();
idtext.Clear();
pricetext.Clear();
}
}
}
|
|