using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnAdd_Click(object sender, EventArgs e)

        {

            string stuStuId=textId.Text.Trim();

            string  stuName=textName.Text.Trim();

            string stuGender=textGender.Text.Trim();

            string stuAge=textAge.Text.Trim();

            string stuClass=textClass.Text.Trim();

            string conStr = "server=.;database=Tbstudent;integrated security=true";

            using (SqlConnection connection = new SqlConnection(conStr))

            {

                connection.Open();

                //string sql = "insert into student values('" + stuStuId + "','" + stuName + "','" + stuGender + "','" + stuAge + "','" + stuClass + "'," + stuCourse + ")";

                string sql = "insert into student values('{0}','{1}','{2}','{3}','{4}')";

                sql = String.Format(sql, stuStuId, stuName, stuGender, stuAge, stuClass);

 

                //using (SqlCommand cmd = new SqlCommand(sql,connection))

                using (SqlCommand cmd=new SqlCommand())

                {

                    cmd.CommandText = sql;

                    cmd.Connection = connection;

                    int count =  cmd.ExecuteNonQuery(); //用来增添该查

                    MessageBox.Show(count.ToString());

                }

            }

        }

 

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            string stuStuId = textId.Text;

            string stuName = textName.Text;

            string stuGender = textGender.Text;

            string stuAge = textAge.Text;

            string stuClass = textClass.Text;

              string conStr = "server=.;database=Tbstudent;integrated security=true";

              using (SqlConnection connection = new SqlConnection(conStr))

              {

                  connection.Open();

                  string sql = "update student set stuName='{0}',stuGender='{1}',stuAge='{2}',stuClass='{3}' where stuStuId='{4}'";

                  sql = String.Format(sql,stuName, stuGender, stuAge, stuClass,stuStuId);

                  using (SqlCommand cmd = new SqlCommand(sql, connection))

                  {

                      cmd.ExecuteNonQuery();

                  }

              }

        }

    }

}