CustomerForm.cs
资源名称:BookStore.rar [点击查看]
上传用户:xyl529207
上传日期:2022-08-03
资源大小:935k
文件大小:7k
源码类别:
行业应用
开发平台:
SQL
- 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;
- using System.Configuration;
- namespace BookStoreMan
- {
- public partial class CustomerForm : Form
- {
- protected SqlConnection conn;
- protected SqlCommand cmd;
- public CustomerForm()
- {
- InitializeComponent();
- }
- private void CustomerForm_Load(object sender, EventArgs e)
- {
- try
- {
- conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreMan.Properties.Settings.BookStoreConnectionString"].ConnectionString);
- cmd = new SqlCommand();
- cmd.Connection = conn;
- conn.Open();
- this.InitData();
- }
- catch (Exception exp)
- {
- MessageBox.Show("无法建立数据连接:" + exp.Message);
- this.Close();
- }
- }
- private void CustomerForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (conn != null && conn.State != ConnectionState.Closed)
- conn.Close();
- }
- private void btnQuery_Click(object sender, EventArgs e)
- {
- if (cmbCustomerID.Text == "")
- return;
- cmd.CommandText = "SELECT * FROM [Customer] WHERE [ID]=" + cmbCustomerID.Text;
- SqlDataReader reader1 = null;
- try
- {
- reader1 = cmd.ExecuteReader();
- if (reader1.Read())
- {
- tbName.Text = reader1["Name"].ToString();
- nudLevels.Value = (short)reader1["Levels"];
- if ((bool)reader1["Gender"])
- rabMan.Checked = true;
- else
- rabWoman.Checked = true;
- dtpBirthday.Value = (DateTime)reader1["Birthday"];
- cmbDegree.Text = reader1["Degree"].ToString();
- tbAddress.Text = reader1["Address"].ToString();
- tbTelephone.Text = reader1["Telephone"].ToString();
- tbEmail.Text = reader1["Email"].ToString();
- nudScore.Value = (int)reader1["Score"];
- }
- else
- {
- MessageBox.Show("没有符合条件的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- catch (Exception exp)
- {
- MessageBox.Show("数据访问错误:" + exp.Message + exp.StackTrace, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- if (reader1 != null && !reader1.IsClosed)
- reader1.Close();
- }
- }
- private void btnSave_Click(object sender, EventArgs e)
- {
- if (cmbCustomerID.Text == "")
- return;
- if (tbName.Text == "")
- {
- MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbName.Focus();
- return;
- }
- if (MessageBox.Show("确认保存所进行的修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
- return;
- StringBuilder sb1 = new StringBuilder("UPDATE [Customer] SET [Name]='");
- sb1.Append(tbName.Text);
- sb1.Append("', [Levels]=");
- sb1.Append(nudLevels.Value);
- sb1.Append(", [Gender]=");
- if(rabMan.Checked)
- sb1.Append("1");
- else
- sb1.Append("0");
- sb1.Append(", [Birthday]='");
- sb1.Append(dtpBirthday.Value.ToShortDateString());
- sb1.Append("', [Degree]='");
- sb1.Append(cmbDegree.Text);
- sb1.Append("', [Address]='");
- sb1.Append(tbAddress.Text);
- sb1.Append("', [Telephone]='");
- sb1.Append(tbTelephone.Text);
- sb1.Append("', [Email]='");
- sb1.Append(tbEmail.Text);
- sb1.Append("', [Score]=");
- sb1.Append(nudScore.Value);
- sb1.Append(" WHERE [ID]=");
- sb1.Append(cmbCustomerID.Text);
- cmd.CommandText = sb1.ToString();
- try
- {
- if(cmd.ExecuteNonQuery() > 0)
- MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- else
- MessageBox.Show("未修改任何记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- catch (Exception exp)
- {
- MessageBox.Show("数据访问错误:" + exp.Message);
- }
- }
- private void btnNew_Click(object sender, EventArgs e)
- {
- if (tbName.Text == "")
- {
- MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- tbName.Focus();
- return;
- }
- if (MessageBox.Show("确认增加新会员吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
- return;
- StringBuilder sb1 = new StringBuilder("INSERT INTO [Customer] ([Name], [Levels], [Gender], [Birthday], [Degree], [Address], [Telephone], [Email], [Score]) VALUES('");
- sb1.Append(tbName.Text);
- sb1.Append("', ");
- sb1.Append(nudLevels.Value);
- sb1.Append(", ");
- if (rabMan.Checked)
- sb1.Append("1");
- else
- sb1.Append("0");
- sb1.Append(", '");
- sb1.Append(dtpBirthday.Value.ToShortDateString());
- sb1.Append("', '");
- sb1.Append(cmbDegree.Text);
- sb1.Append("', '");
- sb1.Append(tbAddress.Text);
- sb1.Append("', '");
- sb1.Append(tbTelephone.Text);
- sb1.Append("', '");
- sb1.Append(tbEmail.Text);
- sb1.Append("', ");
- sb1.Append(nudScore.Value);
- sb1.Append(")");
- cmd.CommandText = sb1.ToString();
- try
- {
- if (cmd.ExecuteNonQuery() > 0)
- {
- MessageBox.Show("增加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- this.InitData();
- }
- else
- {
- MessageBox.Show("未增加任何记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- catch (Exception exp)
- {
- MessageBox.Show("数据访问错误:" + exp.Message);
- }
- }
- protected void InitData()
- {
- cmd.CommandText = "SELECT DISTINCT [ID] FROM [Customer] ORDER BY [ID]";
- SqlDataReader reader1 = cmd.ExecuteReader();
- cmbCustomerID.Items.Clear();
- while (reader1.Read())
- cmbCustomerID.Items.Add(reader1[0]);
- reader1.Close();
- }
- }
- }