winform

断开式:

//dgvStudentScore为最下面控件的name属性

//这个表要设置主键

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string conStr = "Data Source=PC-38\\SQLEXPRESS;Initial Catalog=ns;Integrated Security=True";
        SqlConnection con;
        SqlDataAdapter da;
        DataSet ds;

        private void button1_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(conStr);
            da = new SqlDataAdapter("select * from studentscore" , con);
            ds = new DataSet();
            da.Fill(ds, "studentscore");
            DataRow row = ds.Tables["studentscore"].Rows[0];
            string s = string.Format("{0}, {1}", row["name"].ToString().Trim(), row["score"]);
            dgvStudentScore.DataSource = ds.Tables["studentscore"];

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
            DataRow r1 = ds.Tables["studentscore"].NewRow();
            r1[0] = textBox1.Text;
            r1[1] = textBox2.Text;
            r1[2] = textBox3.Text;
            ds.Tables[0].Rows.Add(r1);
            da.Update(ds, "studentscore");
            dgvStudentScore.DataSource = ds.Tables["studentscore"];

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
            DataRowCollection rows = ds.Tables["studentscore"].Rows;
            DataRow row;
            for (int i = 0; i < rows.Count; i++)
            {
                row = rows[i];
                if (row["num"].ToString() == textBox1.Text)
                {
                    row["score"] = Int32.Parse(textBox3.Text);
                    row["name"] = (textBox2.Text);
                }
            }
            dgvStudentScore.DataSource = ds.Tables["studentscore"];
            da.Update(ds, "studentscore");
            

        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(da);
            DataRowCollection rows = ds.Tables["studentscore"].Rows;
            DataRow row;
            for (int i = 0; i < rows.Count; i++)
            {
                row = rows[i];
                if (row["num"].ToString() == textBox1.Text)
                {
                    row.Delete();
                }
            }
            da.Update(ds, "studentscore");
            dgvStudentScore.DataSource = ds.Tables["studentscore"];

        }
    }
}

非断开

//这个表不用设置主键

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int num = 1;
        public int sex;
        public string conString = "Data Source=.;Initial Catalog=ns;Integrated Security=True";

        private void button1_Click(object sender, EventArgs e)
        {
            num--;
            if (num < 1)
            {
                num = 1;
            }
            radioButton1.Checked = true;
            ShowStudentInfo();
        }

        private void ShowStudentInfo()
        {
            SqlConnection conn = new SqlConnection(conString);
            conn.Open();
            string sql = "select * from stu where num = " + num;
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();


            if (reader.Read())
            {
                int sex = reader.GetInt32(2);
                textBox1.Text = reader.GetInt32(0).ToString();
                textBox2.Text = reader.GetString(1);
                if (sex == 1)
                // sex值为1是女
                {
                    radioButton1.Checked = false;
                    radioButton2.Checked = true;
                }

                reader.Close();
            }
                conn.Close();

            }

        private void button2_Click(object sender, EventArgs e)
        {
            num++;
            radioButton1.Checked = true;
            ShowStudentInfo();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int sex = 1;
            if (radioButton2.Checked)
                {
                    sex = 0;
                }

                SqlConnection conn = new SqlConnection(conString);
                SqlCommand cmd = new SqlCommand("insert into stu (num,name,sex) values(@num,@name,@sex)", conn);
                cmd.Parameters.AddWithValue("@num", textBox1.Text);
                cmd.Parameters.AddWithValue("@name", textBox2.Text);
                cmd.Parameters.AddWithValue("@sex", sex);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                }


        private void button4_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("DELETE from stu where num=@num", conn);
            cmd.Parameters.AddWithValue("@num", Convert.ToInt32((textBox1.Text).Trim()));
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }

       
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值