CustomerForm.cs
上传用户:xyl529207
上传日期:2022-08-03
资源大小:935k
文件大小:7k
源码类别:

行业应用

开发平台:

SQL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. namespace BookStoreMan
  11. {
  12.     public partial class CustomerForm : Form
  13.     {
  14.         protected SqlConnection conn;
  15.         protected SqlCommand cmd;
  16.         public CustomerForm()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         private void CustomerForm_Load(object sender, EventArgs e)
  21.         {
  22.             try
  23.             {
  24.                 conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreMan.Properties.Settings.BookStoreConnectionString"].ConnectionString);
  25.                 cmd = new SqlCommand();
  26.                 cmd.Connection = conn;
  27.                 conn.Open();
  28.                 this.InitData();
  29.             }
  30.             catch (Exception exp)
  31.             {
  32.                 MessageBox.Show("无法建立数据连接:" + exp.Message);
  33.                 this.Close();
  34.             }
  35.         }
  36.         private void CustomerForm_FormClosing(object sender, FormClosingEventArgs e)
  37.         {
  38.             if (conn != null && conn.State != ConnectionState.Closed)
  39.                 conn.Close();
  40.         }
  41.         private void btnQuery_Click(object sender, EventArgs e)
  42.         {
  43.             if (cmbCustomerID.Text == "")
  44.                 return;
  45.             cmd.CommandText = "SELECT * FROM [Customer] WHERE [ID]=" + cmbCustomerID.Text;
  46.             SqlDataReader reader1 = null;
  47.             try
  48.             {
  49.                 reader1 = cmd.ExecuteReader();
  50.                 if (reader1.Read())
  51.                 {
  52.                     tbName.Text = reader1["Name"].ToString();
  53.                     nudLevels.Value = (short)reader1["Levels"];
  54.                     if ((bool)reader1["Gender"])
  55.                         rabMan.Checked = true;
  56.                     else
  57.                         rabWoman.Checked = true;
  58.                     dtpBirthday.Value = (DateTime)reader1["Birthday"];
  59.                     cmbDegree.Text = reader1["Degree"].ToString();
  60.                     tbAddress.Text = reader1["Address"].ToString();
  61.                     tbTelephone.Text = reader1["Telephone"].ToString();
  62.                     tbEmail.Text = reader1["Email"].ToString();
  63.                     nudScore.Value = (int)reader1["Score"];
  64.                 }
  65.                 else
  66.                 {
  67.                     MessageBox.Show("没有符合条件的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  68.                 }
  69.             }
  70.             catch (Exception exp)
  71.             {
  72.                 MessageBox.Show("数据访问错误:" + exp.Message + exp.StackTrace, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  73.             }
  74.             finally
  75.             {
  76.                 if (reader1 != null && !reader1.IsClosed)
  77.                     reader1.Close();
  78.             }
  79.         }
  80.         private void btnSave_Click(object sender, EventArgs e)
  81.         {
  82.             if (cmbCustomerID.Text == "")
  83.                 return;
  84.             if (tbName.Text == "")
  85.             {
  86.                 MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  87.                 tbName.Focus();
  88.                 return;
  89.             }
  90.             if (MessageBox.Show("确认保存所进行的修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  91.                 return;
  92.             StringBuilder sb1 = new StringBuilder("UPDATE [Customer] SET [Name]='");
  93.             sb1.Append(tbName.Text);
  94.             sb1.Append("', [Levels]=");
  95.             sb1.Append(nudLevels.Value);
  96.             sb1.Append(", [Gender]=");
  97.             if(rabMan.Checked)
  98.                 sb1.Append("1");
  99.             else
  100.                 sb1.Append("0");
  101.             sb1.Append(", [Birthday]='");
  102.             sb1.Append(dtpBirthday.Value.ToShortDateString());
  103.             sb1.Append("', [Degree]='");
  104.             sb1.Append(cmbDegree.Text);
  105.             sb1.Append("', [Address]='");
  106.             sb1.Append(tbAddress.Text);
  107.             sb1.Append("', [Telephone]='");
  108.             sb1.Append(tbTelephone.Text);
  109.             sb1.Append("', [Email]='");
  110.             sb1.Append(tbEmail.Text);
  111.             sb1.Append("', [Score]=");
  112.             sb1.Append(nudScore.Value);
  113.             sb1.Append(" WHERE [ID]=");
  114.             sb1.Append(cmbCustomerID.Text);
  115.             cmd.CommandText = sb1.ToString();
  116.             try
  117.             {
  118.                 if(cmd.ExecuteNonQuery() > 0)
  119.                     MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  120.                 else
  121.                     MessageBox.Show("未修改任何记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  122.             }
  123.             catch (Exception exp)
  124.             {
  125.                 MessageBox.Show("数据访问错误:" + exp.Message);
  126.             }
  127.         }
  128.         private void btnNew_Click(object sender, EventArgs e)
  129.         {
  130.             if (tbName.Text == "")
  131.             {
  132.                 MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  133.                 tbName.Focus();
  134.                 return;
  135.             }
  136.             if (MessageBox.Show("确认增加新会员吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
  137.                 return;
  138.             StringBuilder sb1 = new StringBuilder("INSERT INTO [Customer] ([Name], [Levels], [Gender], [Birthday], [Degree], [Address], [Telephone], [Email], [Score]) VALUES('");
  139.             sb1.Append(tbName.Text);
  140.             sb1.Append("', ");
  141.             sb1.Append(nudLevels.Value);
  142.             sb1.Append(", ");
  143.             if (rabMan.Checked)
  144.                 sb1.Append("1");
  145.             else
  146.                 sb1.Append("0");
  147.             sb1.Append(", '");
  148.             sb1.Append(dtpBirthday.Value.ToShortDateString());
  149.             sb1.Append("', '");
  150.             sb1.Append(cmbDegree.Text);
  151.             sb1.Append("', '");
  152.             sb1.Append(tbAddress.Text);
  153.             sb1.Append("', '");
  154.             sb1.Append(tbTelephone.Text);
  155.             sb1.Append("', '");
  156.             sb1.Append(tbEmail.Text);
  157.             sb1.Append("', ");
  158.             sb1.Append(nudScore.Value);
  159.             sb1.Append(")");
  160.             cmd.CommandText = sb1.ToString();
  161.             try
  162.             {
  163.                 if (cmd.ExecuteNonQuery() > 0)
  164.                 {
  165.                     MessageBox.Show("增加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  166.                     this.InitData();
  167.                 }
  168.                 else
  169.                 {
  170.                     MessageBox.Show("未增加任何记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  171.                 }
  172.             }
  173.             catch (Exception exp)
  174.             {
  175.                 MessageBox.Show("数据访问错误:" + exp.Message);
  176.             }
  177.         }
  178.         protected void InitData()
  179.         {
  180.             cmd.CommandText = "SELECT DISTINCT [ID] FROM [Customer] ORDER BY [ID]";
  181.             SqlDataReader reader1 = cmd.ExecuteReader();
  182.             cmbCustomerID.Items.Clear();
  183.             while (reader1.Read())
  184.                 cmbCustomerID.Items.Add(reader1[0]);
  185.             reader1.Close();
  186.         }
  187.     }
  188. }