ADO.NET之command录入数据(视图版)

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;

namespace UseADO
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection(//创建数据库连接字段
@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\db_TomeTwo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            ControlInfo(false);//停用TextBox控件
        }

        private void tbADD_Click(object sender, EventArgs e)
        {
            ControlInfo(true);//启用TextBox控件
            this.textBox1.Enabled = false;//停用文本框
            con.Open();//打开数据库连接
            using (SqlCommand cmd =//创建数据库命令对象
                new SqlCommand("select Max(员工编号) from 员工表", con))
            {
                if (Convert.ToString(cmd.ExecuteScalar()) != "")
                {
                    string strID = Convert.ToString(cmd.ExecuteScalar());//得到员工编号
                    this.textBox1.Text =//得到加1的员工编号
                        strID.Substring(0, 1) + Convert.ToString(Convert.ToUInt32(strID.Substring(1)) + 1);
                }
                else
                {
                    this.textBox1.Text = "P1001";//得到员工编号
                }
            }
            con.Close();//关闭数据库连接
        }
        private void ControlInfo(Boolean B)
        {
            foreach (Control ct in this.groupBox1.Controls)
            {
                if (ct is TextBox)//判断是否是TextBox控件
                {
                    if (B)
                    {
                        ct.Enabled = true;//启用控件
                    }
                    else
                    {
                        ct.Enabled = false;//停用控件
                    }
                }
            }
        }

 


        private void tbSave_Click(object sender, EventArgs e)
        {
            con.Open();//打开数据库连接
            using (SqlCommand command = new SqlCommand("INSERT INTO 员工表 " +//创建数据库命令对象
               "VALUES (@员工编号, @员工姓名,@基本工资,@工作评价,@所属部门)", con))
            {
                command.Parameters.Add("@员工编号",//添加参数并赋值
                    SqlDbType.VarChar, 50, "员工编号").Value = this.textBox1.Text;
                command.Parameters.Add("@员工姓名",//添加参数并赋值
                    SqlDbType.VarChar, 50, "员工姓名").Value = this.textBox2.Text;
                command.Parameters.Add("@基本工资",//添加参数并赋值
                    SqlDbType.Float, 8, "基本工资").Value = Convert.ToString(this.textBox4.Text);
                command.Parameters.Add("@工作评价",//添加参数并赋值
                    SqlDbType.VarChar, 50, "工作评价").Value = this.textBox5.Text;
                command.Parameters.Add("@所属部门",//添加参数并赋值
                    SqlDbType.VarChar, 50, "所属部门").Value = "";
                command.ExecuteNonQuery();//执行SQL语句
                MessageBox.Show("添加数据成功");//弹出消息对话框
            }
            con.Close();//关闭数据库连接
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值