c#.net数据库连接 查询,删除 ,插入 ,更新

public partial class btndelete : Form
    {
        public btndelete()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                MessageBox.Show("请重新输入");
                txtnumber.Text = "";
            }
        }
         private string getcheckboxlist(CheckedListBox clb )
            {
                string parry="";
                for(int i=0;i<clb.CheckedItems.Count;i++)
                {
                    parry += clb.CheckedItems[i].ToString();
                   
                }
                return parry;
            }
        private void btn_Click(object sender, EventArgs e)
        {
           int s_number;
            string s_name,s_sex;
            string s_marry;
            string s_parry;
            string s_age;

            s_number =int.Parse(txtnumber.Text);
            s_name = txtname.Text;
            s_sex = null;
            if (rbt.Checked)
            {
                s_sex = rbt.Text;
            }
            else
            {
                s_sex = rbt1.Text;
            }
            s_age = txtage.Text;
            s_marry = this.cbomarry.Text;
            s_parry = getcheckboxlist(ckdparry).ToString();
            MessageBox.Show(s_name+"\t学号: "+s_number+"\t性别: "+s_sex+"\t年龄: "+s_age+"\t婚否: "+s_marry+"\t政治面貌:"+s_parry);
            SqlConnection connection = new SqlConnection("Data Source=y06;Initial Catalog=student;Persist Security Info=True;User ID=sa;pwd=qingniao");
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand("insert into student values (" + s_number + ",'" + s_name + "','" + s_sex + "')",connection);
                command.ExecuteNonQuery();
                MessageBox.Show("插入成功");
          
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                connection.Close();
                MessageBox.Show("关闭数据库");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int s_number;
            s_number = int.Parse(txtnumber.Text);
            SqlConnection connection = new SqlConnection("Data Source=y06;Initial Catalog=student;Persist Security Info=True;User ID=sa;pwd=qingniao");
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand("delete from student where sno=" + s_number +" ",connection);
                command.ExecuteNonQuery();
                MessageBox.Show("删除成功");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
            finally
            {
                connection.Close();
                MessageBox.Show("关闭数据库");
            }
        }

        private void btngengxin_Click(object sender, EventArgs e)
        {

        }

        private void btnchaxun_Click(object sender, EventArgs e)
        {
            int s_number;
            string s_name = null;
          
            string s_age=null;

            s_number = int.Parse(txtnumber.Text);
    
            SqlConnection connection = new SqlConnection("Data Source=y06;Initial Catalog=student;Persist Security Info=True;User ID=sa;pwd=qingniao");
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand("select * from student where sno=" + s_number + "", connection);
               SqlDataReader data= command.ExecuteReader();
                MessageBox.Show("查询成功");
                while (data.Read())
                {
                    s_name = (string)data[1];
                    s_age = (string)data[2];
               
               
               
                }
                MessageBox.Show(s_name + "\t学号: " + s_number + "\t年龄: " + s_age);
                this.txtage.Text=s_age;
                this.txtname.Text=s_name;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                connection.Close();
                MessageBox.Show("关闭数据库");
            }
        }

       }


你可能感兴趣的:(exception,数据库,object,String,Security,command)