1.0版本在此:https://www.yuque.com/shuiyun-evwnf/nm9z7v/bng07b
介绍:将数据库中的某一个内容显示到TextBox中,并且可以通过他来插入新数据。
1、数据库准备工作(数据库名称为student)
2、Form准备工作(数据库提前连接好)
3、首先在程序中引入命名空间
using System.Data.SqlClient;
4、插入功能
private void btnInsert_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into tbl_student values('"+txtID.Text+"','"+txtname.Text+"')";//两个txtbox分别命名为txtID、txtname
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("插入成功!");
}
5、读取功能,将指定的内容(select 。。。where。。。限定条件)显示到TextBox中。
如果需要显示的内容是string类型,则用下边这个
private void btnRead_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText = "select name from tbl_student where ID='2'";
conn.Open();
string d = Convert.ToString(cmd.ExecuteScalar());//////////这里会更改
conn.Close();
txtname.Text = d.ToString();
}
如果需要显示的内容是int类型,则用下边这个
private void btnRead_Click(object sender, EventArgs e)
{/*
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText = "select name from tbl_student where ID='2'";
conn.Open();
string d = Convert.ToString(cmd.ExecuteScalar());
conn.Close();
txtname.Text = d.ToString();
*/
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select ID from tbl_student where name='范云川'";
conn.Open();
int d = Convert.ToInt32(cmd.ExecuteScalar());//////////这里更改了
conn.Close();
txtID.Text = d.ToString();
}
6、显示内容,将某一列内容显示到listbox中
private void btnshow_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;initial catalog=student;integrated security=true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select ID from tbl_student where name='范云川'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
lstName.Items.Add(dr.GetInt32(0)); //如果需要显示的内容是string,则需更改GetInt32,0代表第0列
dr.Close();
conn.Close();
}
7、DataGridView查询(2020/11/6 12:24 添加)
private void btnUserFind_Click(object sender, EventArgs e) //点击查询按钮
{
ArrayList dbRecordsHolder = new ArrayList();
SqlConnection conn = new SqlConnection("data source=.;initial catalog=PLA;integrated security=true;");//需要修改数据库名称
SqlCommand cmd = new SqlCommand("Select * from tbl_UserInfo where 身份证号='" + txtUserIDFind.Text + "' or 姓名='" + txtUserNameFind.Text + "' ", conn);//需要修改表名
conn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
foreach (DbDataRecord rec in dr)
{
dbRecordsHolder.Add(rec);
}
}
dr.Close();
dataGridView1.DataSource = dbRecordsHolder;
}
8、参考教材资料和C#工程将存放在百度云盘里。
链接:https://pan.baidu.com/s/1AthQjC733I0ZuXAyHIEIjQ
提取码:540v
9、一点点拙见,如有错误,请指出,谢谢(^▽^)