简单三层增删该查

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 三层CURD.App_Code.Model
{
    [Serializable]
    public class Person
    {
        public Int16 PID { get; set; }
        public string PName { get; set; }
        public Int16 PAge { get; set; }
        public string PSex { get; set; }
        public string PJob { get; set; }
    }
}

Dal:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using 三层CURD.App_Code.Model;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace 三层CURD.App_Code.Dal
{
    public class DBHelper
    {
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["Person"].ConnectionString;

        public static List<Person> ExcetList(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    List<Person> list = new List<Person>();
                    cmd.CommandText = sql;
                    if (parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {

                        while (reader.Read())
                        {

                            list.Add(new Person()
                            {
                                PID = Convert.ToInt16(reader["PID"]),
                                PName = reader.GetString(reader.GetOrdinal("PName")),
                                PAge = Convert.ToInt16(reader["PAge"]),
                                PJob = reader.GetString(reader.GetOrdinal("PJob")),
                                PSex = reader.GetString(reader.GetOrdinal("PSex"))
                            });


                        }
                        return list;
                    }
                }
            }
        }
        public static void ExecuteNoneQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    if (parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    cmd.ExecuteNonQuery();
                }
            }
        }
        public static Person GetPersonByPID(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    if (parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    Person p = new Person();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {

                        if (reader.Read())
                        {
                            p.PID = Convert.ToInt16(reader["PID"]);
                            p.PName = reader.GetString(reader.GetOrdinal("PName"));
                            p.PAge = Convert.ToInt16(reader["PAge"]);
                            p.PJob = reader.GetString(reader.GetOrdinal("PJob"));
                            p.PSex = reader.GetString(reader.GetOrdinal("PSex"));

                        }
                        return p;
                    }
                }
            }
        }
    }
}

BLL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 三层CURD.App_Code.Model;
using 三层CURD.App_Code.Dal;
using System.Data.SqlClient;

namespace 三层CURD.App_Code.Bal
{
    public class PersonManager
    {
        public List<Person> GetList(string Pname)
        {
            return DBHelper.ExcetList(@"SELECT   PID, PName, PSex, PAge, PJob
FROM      Person where PName like @PName", new SqlParameter("@PName","%"+ Pname+"%"));
        }
        public Person Find(int PID)
        {
            return DBHelper.GetPersonByPID(@"SELECT   PID, PName, PSex, PAge, PJob
FROM   Person where PID=@PID", new SqlParameter("@PID", PID));
        }

        public void Delete(int PID)
        {
            DBHelper.ExecuteNoneQuery("delete Person where PID=@PID", new SqlParameter("@PID", PID));
        }
        public void Insert(Person p)
        {
            DBHelper.ExecuteNoneQuery("insert into Person(PName,PSex,PAge,PJob) values(@PName,@PSex,@PAge,@PJob)", new SqlParameter("@PName", p.PName), new SqlParameter("@PSex", p.PSex), new SqlParameter("@PAge", p.PAge), new SqlParameter("@PJob", p.PJob));
        }
        public void Update(Person p)
        {
            DBHelper.ExecuteNoneQuery("update  Person set PName=@PName,PSex=@PSex,PAge=@PAge,PJob=@PJob where PID=@PID", new SqlParameter("@PName", p.PName), new SqlParameter("@PSex", p.PSex), new SqlParameter("@PAge", p.PAge), new SqlParameter("@PJob", p.PJob), new SqlParameter("@PID", p.PID));
        }
    }
}

Form1.Designer

namespace 三层CURD
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.PID = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.PName = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Psex = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.PAge = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.PJob = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.btnUpdate = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnDelete = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.btnQuery = new System.Windows.Forms.Button();
            this.panel3 = new System.Windows.Forms.Panel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel4 = new System.Windows.Forms.Panel();
            this.btnExp = new System.Windows.Forms.Button();
            this.btnImp = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.panel2.SuspendLayout();
            this.panel3.SuspendLayout();
            this.panel1.SuspendLayout();
            this.panel4.SuspendLayout();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.PID,
            this.PName,
            this.Psex,
            this.PAge,
            this.PJob});
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(642, 261);
            this.dataGridView1.TabIndex = 0;
            // 
            // PID
            // 
            this.PID.DataPropertyName = "PID";
            this.PID.HeaderText = "编号";
            this.PID.Name = "PID";
            this.PID.ReadOnly = true;
            // 
            // PName
            // 
            this.PName.DataPropertyName = "PName";
            this.PName.HeaderText = "姓名";
            this.PName.Name = "PName";
            this.PName.ReadOnly = true;
            // 
            // Psex
            // 
            this.Psex.DataPropertyName = "PSex";
            this.Psex.HeaderText = "性别";
            this.Psex.Name = "Psex";
            this.Psex.ReadOnly = true;
            // 
            // PAge
            // 
            this.PAge.DataPropertyName = "PAge";
            this.PAge.HeaderText = "年龄";
            this.PAge.Name = "PAge";
            this.PAge.ReadOnly = true;
            // 
            // PJob
            // 
            this.PJob.DataPropertyName = "PJob";
            this.PJob.HeaderText = "职业";
            this.PJob.Name = "PJob";
            this.PJob.ReadOnly = true;
            // 
            // btnUpdate
            // 
            this.btnUpdate.Anchor = System.Windows.Forms.AnchorStyles.Left;
            this.btnUpdate.Location = new System.Drawing.Point(195, 18);
            this.btnUpdate.Name = "btnUpdate";
            this.btnUpdate.Size = new System.Drawing.Size(64, 28);
            this.btnUpdate.TabIndex = 3;
            this.btnUpdate.Text = "修改";
            this.btnUpdate.UseVisualStyleBackColor = true;
            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
            // 
            // btnAdd
            // 
            this.btnAdd.Anchor = System.Windows.Forms.AnchorStyles.Left;
            this.btnAdd.Location = new System.Drawing.Point(106, 18);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(64, 28);
            this.btnAdd.TabIndex = 2;
            this.btnAdd.Text = "添加";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btnDelete
            // 
            this.btnDelete.Anchor = System.Windows.Forms.AnchorStyles.Left;
            this.btnDelete.Location = new System.Drawing.Point(12, 18);
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(64, 28);
            this.btnDelete.TabIndex = 1;
            this.btnDelete.Text = "删除";
            this.btnDelete.UseVisualStyleBackColor = true;
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnImp);
            this.panel2.Controls.Add(this.btnExp);
            this.panel2.Controls.Add(this.textBox1);
            this.panel2.Controls.Add(this.btnQuery);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(642, 58);
            this.panel2.TabIndex = 0;
            // 
            // textBox1
            // 
            this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Right;
            this.textBox1.BackColor = System.Drawing.Color.White;
            this.textBox1.ForeColor = System.Drawing.Color.DarkGray;
            this.textBox1.Location = new System.Drawing.Point(277, 21);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(150, 21);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "请输入姓名";
            this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
            this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
            // 
            // btnQuery
            // 
            this.btnQuery.Anchor = System.Windows.Forms.AnchorStyles.Right;
            this.btnQuery.Location = new System.Drawing.Point(433, 19);
            this.btnQuery.Name = "btnQuery";
            this.btnQuery.Size = new System.Drawing.Size(64, 23);
            this.btnQuery.TabIndex = 0;
            this.btnQuery.Text = "查找";
            this.btnQuery.UseVisualStyleBackColor = true;
            this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.btnUpdate);
            this.panel3.Controls.Add(this.btnAdd);
            this.panel3.Controls.Add(this.btnDelete);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel3.Location = new System.Drawing.Point(0, 319);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(642, 70);
            this.panel3.TabIndex = 1;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.panel4);
            this.panel1.Controls.Add(this.panel3);
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(642, 389);
            this.panel1.TabIndex = 1;
            // 
            // panel4
            // 
            this.panel4.Controls.Add(this.dataGridView1);
            this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel4.Location = new System.Drawing.Point(0, 58);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(642, 261);
            this.panel4.TabIndex = 2;
            // 
            // btnExp
            // 
            this.btnExp.Anchor = System.Windows.Forms.AnchorStyles.Right;
            this.btnExp.Location = new System.Drawing.Point(503, 19);
            this.btnExp.Name = "btnExp";
            this.btnExp.Size = new System.Drawing.Size(64, 23);
            this.btnExp.TabIndex = 2;
            this.btnExp.Text = "导出";
            this.btnExp.UseVisualStyleBackColor = true;
            this.btnExp.Click += new System.EventHandler(this.btnExp_Click);
            // 
            // btnImp
            // 
            this.btnImp.Anchor = System.Windows.Forms.AnchorStyles.Right;
            this.btnImp.Location = new System.Drawing.Point(573, 19);
            this.btnImp.Name = "btnImp";
            this.btnImp.Size = new System.Drawing.Size(64, 23);
            this.btnImp.TabIndex = 3;
            this.btnImp.Text = "导入";
            this.btnImp.UseVisualStyleBackColor = true;
            this.btnImp.Click += new System.EventHandler(this.btnImp_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(642, 389);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.panel3.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel4.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        public System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn PID;
        private System.Windows.Forms.DataGridViewTextBoxColumn PName;
        private System.Windows.Forms.DataGridViewTextBoxColumn Psex;
        private System.Windows.Forms.DataGridViewTextBoxColumn PAge;
        private System.Windows.Forms.DataGridViewTextBoxColumn PJob;
        public System.Windows.Forms.Button btnUpdate;
        public System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Button btnDelete;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button btnQuery;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel4;
        private System.Windows.Forms.Button btnImp;
        private System.Windows.Forms.Button btnExp;
    }
}

Form1.cs

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.Configuration;
using System.Data.SqlClient;
using 三层CURD.App_Code.Bal;
using 三层CURD.App_Code.Model;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;

namespace 三层CURD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        PersonManager person = new PersonManager();
        public static bool falg;
        private void Form1_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        private void DataBind()
        {
            this.dataGridView1.DataSource = person.GetList("");
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                this.textBox1.Text = "请输入姓名";
                textBox1.ForeColor = Color.DarkGray;
            }
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "请输入姓名")
            {
                this.textBox1.Text = "";
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || this.textBox1.Text == "请输入姓名")
            {
                DataBind();
            }
            else
            {
                this.dataGridView1.DataSource = person.GetList(textBox1.Text);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                person.Delete(Convert.ToInt16(this.dataGridView1.CurrentRow.Cells["PID"].Value));
                DataBind();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            falg = true;
            AddForm add = new AddForm();
            add.userRefer = this.DataBind;
            add.ShowDialog();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                falg = false;
                AddForm add = new AddForm();
                add.userRefer = this.DataBind;
                Person p = person.Find(Convert.ToInt16(this.dataGridView1.CurrentRow.Cells["PID"].Value));
                add.txtPID.Text = p.PID.ToString();
                add.txtName.Text = p.PName;
                add.cmbSex.Text = p.PSex;
                add.txtAge.Text = p.PAge.ToString();
                add.cmbJob.Text = p.PJob;
                add.ShowDialog();
            }
        }

        private void btnExp_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog file = new SaveFileDialog())
            {
                file.Title = "请选择路径";
                file.Filter = "表格文件|*.xls";
                if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                    excel.SheetsInNewWorkbook = 1;
                    excel.Workbooks.Add();
                    Worksheet sheet = excel.ActiveWorkbook.Worksheets[1] as Worksheet;
                    sheet.Name = "person";
                    for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
                    {
                        excel.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;
                    }
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        for (int j = 0; j < this.dataGridView1.Columns.Count; j++)
                        {
                            excel.Cells[i + 2, j + 1] = this.dataGridView1.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                    excel.Visible = true;
                    excel.ActiveWorkbook.SaveAs(file.FileName, XlFileFormat.xlWorkbookNormal);
                    excel.ActiveWorkbook.Close();
                    excel.Quit();
                }
            }
        }

        private void btnImp_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog file = new OpenFileDialog())
            {
                file.Title = "请选择文件";
                file.Filter = "文件|*.xls";
                if(file.ShowDialog()== System.Windows.Forms.DialogResult.OK){
                string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file.FileName + ";" + "Extended Properties='Excel 8.0';";
                using (OleDbConnection conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    string sql = "select * from [person$]";
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn))
                    {
                        DataSet ds = new DataSet();
                        adapter.Fill(ds, "person");
                        System.Data.DataTable tables = ds.Tables["person"];
                        foreach (DataRow rows in tables.Rows)
                        {
                            person.Insert(new Person()
                            {
                                PID=Convert.ToInt16(rows[0]),
                                PName = rows[1].ToString(),
                                PSex = rows[3].ToString(),
                                PAge = Convert.ToInt16(rows[2]),
                                PJob = rows[4].ToString()
                            });
                        }
                        this.DataBind();
                    }}
                }
            }
        }
    }
}

AddForm.Designer

namespace 三层CURD
{
    partial class AddForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txtPID = new System.Windows.Forms.TextBox();
            this.cmbJob = new System.Windows.Forms.ComboBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.cmbSex = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.txtAge = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.lblSex = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.btnSubmit = new System.Windows.Forms.Button();
            this.lblName = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // txtPID
            // 
            this.txtPID.Location = new System.Drawing.Point(92, 13);
            this.txtPID.Name = "txtPID";
            this.txtPID.Size = new System.Drawing.Size(142, 21);
            this.txtPID.TabIndex = 10;
            this.txtPID.Visible = false;
            // 
            // cmbJob
            // 
            this.cmbJob.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbJob.FormattingEnabled = true;
            this.cmbJob.Location = new System.Drawing.Point(92, 179);
            this.cmbJob.Name = "cmbJob";
            this.cmbJob.Size = new System.Drawing.Size(142, 20);
            this.cmbJob.TabIndex = 9;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.txtPID);
            this.groupBox1.Controls.Add(this.cmbJob);
            this.groupBox1.Controls.Add(this.cmbSex);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.txtAge);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.lblSex);
            this.groupBox1.Controls.Add(this.txtName);
            this.groupBox1.Controls.Add(this.btnSubmit);
            this.groupBox1.Controls.Add(this.lblName);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(261, 261);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "groupBox1";
            // 
            // cmbSex
            // 
            this.cmbSex.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbSex.FormattingEnabled = true;
            this.cmbSex.Location = new System.Drawing.Point(92, 90);
            this.cmbSex.Name = "cmbSex";
            this.cmbSex.Size = new System.Drawing.Size(140, 20);
            this.cmbSex.TabIndex = 8;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(40, 182);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 7;
            this.label3.Text = "职业:";
            // 
            // txtAge
            // 
            this.txtAge.Location = new System.Drawing.Point(92, 133);
            this.txtAge.Name = "txtAge";
            this.txtAge.Size = new System.Drawing.Size(142, 21);
            this.txtAge.TabIndex = 6;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(40, 136);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 5;
            this.label2.Text = "年龄:";
            // 
            // lblSex
            // 
            this.lblSex.AutoSize = true;
            this.lblSex.Location = new System.Drawing.Point(40, 90);
            this.lblSex.Name = "lblSex";
            this.lblSex.Size = new System.Drawing.Size(41, 12);
            this.lblSex.TabIndex = 3;
            this.lblSex.Text = "姓别:";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(92, 40);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(142, 21);
            this.txtName.TabIndex = 2;
            // 
            // btnSubmit
            // 
            this.btnSubmit.Location = new System.Drawing.Point(159, 216);
            this.btnSubmit.Name = "btnSubmit";
            this.btnSubmit.Size = new System.Drawing.Size(75, 23);
            this.btnSubmit.TabIndex = 1;
            this.btnSubmit.Text = "添加";
            this.btnSubmit.UseVisualStyleBackColor = true;
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            // 
            // lblName
            // 
            this.lblName.AutoSize = true;
            this.lblName.Location = new System.Drawing.Point(40, 43);
            this.lblName.Name = "lblName";
            this.lblName.Size = new System.Drawing.Size(41, 12);
            this.lblName.TabIndex = 0;
            this.lblName.Text = "姓名:";
            // 
            // AddForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(261, 261);
            this.Controls.Add(this.groupBox1);
            this.MaximizeBox = false;
            this.Name = "AddForm";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "AddForm";
            this.Load += new System.EventHandler(this.AddForm_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        public System.Windows.Forms.TextBox txtPID;
        public System.Windows.Forms.ComboBox cmbJob;
        public System.Windows.Forms.GroupBox groupBox1;
        public System.Windows.Forms.ComboBox cmbSex;
        private System.Windows.Forms.Label label3;
        public System.Windows.Forms.TextBox txtAge;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label lblSex;
        public System.Windows.Forms.TextBox txtName;
        public System.Windows.Forms.Button btnSubmit;
        private System.Windows.Forms.Label lblName;
    }
}

AddForm.cs

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.Text.RegularExpressions;
using 三层CURD.App_Code.Bal;
using 三层CURD.App_Code.Model;

namespace 三层CURD
{
    public partial class AddForm : Form
    {
        public AddForm()
        {
            InitializeComponent();
        }
        public delegate void ReferUser();
        public ReferUser userRefer;
        PersonManager person = new PersonManager();
        private void DataBind(ComboBox cmb, params string[] parameters)
        {
            if (parameters.Length > 0)
            {
                List<string> list = new List<string>();
                for (int i = 0; i < parameters.Length; i++)
                {
                    list.Add(parameters[i]);
                }
                cmb.DataSource = list;
            }
        }
        private void OperterType(bool falg)
        {
            if (falg)
            {
                this.groupBox1.Text = "添加";
                this.btnSubmit.Text = "添加";
            }
            else
            {
                this.groupBox1.Text = "修改";
                this.btnSubmit.Text = "修改";
            }
        }
        private void AddForm_Load(object sender, EventArgs e)
        {
            DataBind(cmbSex, "", "");
            DataBind(cmbJob, "作家", "艺术家", "思想家");
            OperterType(Form1.falg);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            Regex reg = new Regex(@"\d{2}|\d{1}");
            if (reg.IsMatch(txtAge.Text))
            {
                if (this.groupBox1.Text == "添加")
                {

                    person.Insert(
                        new Person()
                        {
                            PName = txtName.Text,
                            PAge = Convert.ToInt16(txtAge.Text),
                            PSex = cmbSex.SelectedValue.ToString(),
                            PJob = cmbJob.SelectedValue.ToString()
                        }
                        );
                }
                else
                {
                    person.Update(new Person()
                    {
                        PName = txtName.Text,
                        PAge = Convert.ToInt16(txtAge.Text),
                        PSex = cmbSex.SelectedValue.ToString(),
                        PJob = cmbJob.SelectedValue.ToString(),
                        PID = Convert.ToInt16(txtPID.Text)
                    });
                }
                userRefer();
                this.Close();
            }
            else
            {
                MessageBox.Show("不是整数");
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/LoVeSW/articles/3420256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值