ADO.Net操作数据库

 public partial class Form1 : Form
    {
        string constr;
        public Form1()
        {
            InitializeComponent();
            constr = "Data Source=MGLI217RZIKKH6B;Initial Catalog=Itcast2014;Integrated Security=True";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int r = 0;
            using (SqlConnection conn = new SqlConnection(constr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    bool gender = false;
                    if (txtGender.Text == "")
                    {
                        gender = true;
                    }

                    string sql = string.Format("insert into TblPerson(uName,age,height,gender)values('{0}','{1}','{2}','{3}')", txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToInt32(txtHeight.Text), gender);
                    cmd.CommandText = sql;
                    conn.Open();
                    r = cmd.ExecuteNonQuery();
                }
            }
            MessageBox.Show("成功增加" + r + "条数据");
            LoadData();
        }
        public void LoadData()
        {
            List<Person> ls = new List<Person>();
            using (SqlConnection conn = new SqlConnection(constr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {

                    string sql = "select * from TblPerson";
                    cmd.CommandText = sql;
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                int autoId = reader.GetInt32(0);
                                string name = reader.GetString(1);
                                int age = reader.GetInt32(2);
                                int height = reader.IsDBNull(3) ? 150 : reader.GetInt32(3);
                                bool isBoy = reader.IsDBNull(4) ? true : reader.GetBoolean(4);
                                string gender = "";
                                if (isBoy == true)
                                {
                                    gender = "";
                                }
                                else
                                {
                                    gender = "";
                                }
                                ls.Add(new Person(autoId,name, age, height, gender));
                            }
                        }
                    }
                }
            }
            dataGridView1.DataSource = ls;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();

        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            Person p = row.DataBoundItem as Person;
            txtNameE.Text = p.Name;
            txtAgeE.Text = p.Age.ToString();
            txtHeightE.Text = p.Height.ToString();
            txtGenderE.Text = p.Gender;
            LabelSelectTd.Text = p.Id.ToString();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    bool Isboy = false;
                    if (txtGenderE.Text == "")
                    {
                        Isboy = true;
                    }
                    string sql = string.Format("update TblPerson set uName='{0}',age='{1}',height='{2}',gender='{3}' where autoId='{4}'", txtNameE.Text, Convert.ToInt32(txtAgeE.Text), Convert.ToInt32(txtHeightE.Text), Isboy, Convert.ToInt32(LabelSelectTd.Text));
                    cmd.CommandText = sql;
                    conn.Open();
                    int num=cmd.ExecuteNonQuery();
                }
            }
       MessageBox.Show("成功修改"+num+"条数据");
LoadData(); }
private void button1_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("确定要删除吗?", "操作提示", MessageBoxButtons.OKCancel); if (result == System.Windows.Forms.DialogResult.OK) { using (SqlConnection conn = new SqlConnection(constr)) { string sql = string.Format("delete from TblPerson where autoId={0}", Convert.ToInt32(LabelSelectTd.Text)); using (SqlCommand cmd = new SqlCommand(sql, conn)) { conn.Open(); cmd.ExecuteNonQuery(); } } LoadData(); MessageBox.Show("删除成功"); } } } }

 

namespace 第一个练习
{
    class Person
    {
        public int Id { set; get; }
        public string Name { set; get; }
        public int Age { set; get; }
        public int Height { set; get; }
        public string  Gender { set; get; }
        public Person(int id,string name,int age,int height,string gender) {
            this.Id = id;
            this.Name = name;
            this.Age = age;
            this.Height = height;
            this.Gender = gender;
        }
    }
}

 

posted @ 2014-02-26 20:24  我叫小菜  阅读(280)  评论(3编辑  收藏  举报