1.0版本在此:https://www.yuque.com/shuiyun-evwnf/nm9z7v/bng07b
    介绍:将数据库中的某一个内容显示到TextBox中,并且可以通过他来插入新数据。
    1、数据库准备工作(数据库名称为student)
    image.pngimage.png
    2、Form准备工作(数据库提前连接好)
    image.png
    3、首先在程序中引入命名空间
    using System.Data.SqlClient;
    4、插入功能

    1. private void btnInsert_Click(object sender, EventArgs e)
    2. {
    3. SqlConnection conn = new SqlConnection();
    4. conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
    5. SqlCommand cmd = new SqlCommand();
    6. cmd.Connection = conn;
    7. cmd.CommandType = CommandType.Text;
    8. cmd.CommandText = "insert into tbl_student values('"+txtID.Text+"','"+txtname.Text+"')";//两个txtbox分别命名为txtID、txtname
    9. conn.Open();
    10. cmd.ExecuteNonQuery();
    11. conn.Close();
    12. MessageBox.Show("插入成功!");
    13. }

    5、读取功能,将指定的内容(select 。。。where。。。限定条件)显示到TextBox中。
    如果需要显示的内容是string类型,则用下边这个

    1. private void btnRead_Click(object sender, EventArgs e)
    2. {
    3. SqlConnection conn = new SqlConnection();
    4. conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
    5. SqlCommand cmd = new SqlCommand();
    6. cmd.Connection = conn;
    7. cmd.CommandType=CommandType.Text;
    8. cmd.CommandText = "select name from tbl_student where ID='2'";
    9. conn.Open();
    10. string d = Convert.ToString(cmd.ExecuteScalar());//////////这里会更改
    11. conn.Close();
    12. txtname.Text = d.ToString();
    13. }

    如果需要显示的内容是int类型,则用下边这个

    1. private void btnRead_Click(object sender, EventArgs e)
    2. {/*
    3. SqlConnection conn = new SqlConnection();
    4. conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
    5. SqlCommand cmd = new SqlCommand();
    6. cmd.Connection = conn;
    7. cmd.CommandType=CommandType.Text;
    8. cmd.CommandText = "select name from tbl_student where ID='2'";
    9. conn.Open();
    10. string d = Convert.ToString(cmd.ExecuteScalar());
    11. conn.Close();
    12. txtname.Text = d.ToString();
    13. */
    14. SqlConnection conn = new SqlConnection();
    15. conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
    16. SqlCommand cmd = new SqlCommand();
    17. cmd.Connection = conn;
    18. cmd.CommandType = CommandType.Text;
    19. cmd.CommandText = "select ID from tbl_student where name='范云川'";
    20. conn.Open();
    21. int d = Convert.ToInt32(cmd.ExecuteScalar());//////////这里更改了
    22. conn.Close();
    23. txtID.Text = d.ToString();
    24. }

    6、显示内容,将某一列内容显示到listbox中

    1. private void btnshow_Click(object sender, EventArgs e)
    2. {
    3. SqlConnection conn = new SqlConnection();
    4. conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
    5. SqlCommand cmd = new SqlCommand();
    6. cmd.Connection = conn;
    7. cmd.CommandText = "select ID from tbl_student where name='范云川'";
    8. conn.Open();
    9. SqlDataReader dr = cmd.ExecuteReader();
    10. while (dr.Read())
    11. lstName.Items.Add(dr.GetInt32(0)); //如果需要显示的内容是string,则需更改GetInt32,0代表第0列
    12. dr.Close();
    13. conn.Close();
    14. }

    7、DataGridView查询(2020/11/6 12:24 添加)
    image.png

    1. private void btnUserFind_Click(object sender, EventArgs e) //点击查询按钮
    2. {
    3. ArrayList dbRecordsHolder = new ArrayList();
    4. SqlConnection conn = new SqlConnection("data source=.;initial catalog=PLA;integrated security=true;");//需要修改数据库名称
    5. SqlCommand cmd = new SqlCommand("Select * from tbl_UserInfo where 身份证号='" + txtUserIDFind.Text + "' or 姓名='" + txtUserNameFind.Text + "' ", conn);//需要修改表名
    6. conn.Open();
    7. SqlDataReader dr;
    8. dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    9. if (dr.HasRows)
    10. {
    11. foreach (DbDataRecord rec in dr)
    12. {
    13. dbRecordsHolder.Add(rec);
    14. }
    15. }
    16. dr.Close();
    17. dataGridView1.DataSource = dbRecordsHolder;
    18. }

    8、参考教材资料和C#工程将存放在百度云盘里。
    链接:https://pan.baidu.com/s/1AthQjC733I0ZuXAyHIEIjQ
    提取码:540v
    9、一点点拙见,如有错误,请指出,谢谢(^▽^)