工具:
1.Visual Studio 2019
2.SQL Server 数据库 (我使用的 2008)
操作:
1. 打开 SQL Server, 打开后会看到数据库的初始链接界面.(如下图)
2.. 复制上图中的 "服务器名称", 然后点击 "连接", 进入数据库.
3. 打开 vs, 创建好自己要用的项目, 我写的项目名称叫做:'finnal_test', 要做的是数据库综合实习关于奖学金评定的管理系统
4. 工具 -> 连接到数据库 -> 在服务器名里面, 粘贴复制的服务器名
5. 在下面选择自己要连接的数据库名称 (也可以手动输入, 我连接的是我自己创建的数据库: shaohui), 确定
6. 打开 "服务器资源管理器", 会看到有下图信息, 点击 "表" 可以看到数据库里面创建的数据表
连接代码:
完成上述操作后只是把数据库添加到了 vs 里, 要想在项目里对数据库进行编辑, 还需要写一些代码.
1. 打开自己的项目, 选择项目 -> 添加类
类名自己起, 我这里是 SQLServerDataBase
2. 打开类文件, 写入以下代码.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.Threading.Tasks;// 必要的命名空间
- namespace finnal_test
- {
- class SQLServerDataBase
- {
- //MySqlCon 部分, 每个人不相同, 后面我会进行说明, 下面的是我计算机相应的配置
- private string MySqlCon = "Data Source=DESKTOP-8LDERGD\\SQLEXPRESS;Initial Catalog = shaohui; Integrated Security = True";
- public DataTable ExecuteQuery(string sqlStr)// 用于查询; 其实是相当于提供一个可以传参的函数, 到时候写一个 sql 语句, 存在 string 里, 传给这个函数, 就会自动执行.
- {
- SqlConnection con = new SqlConnection(MySqlCon);
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sqlStr;
- DataTable dt = new DataTable();
- SqlDataAdapter msda = new SqlDataAdapter(cmd);
- msda.Fill(dt);
- con.Close();
- return dt;
- }
- public int ExecuteUpdate(string sqlStr)// 用于增删改;
- {
- SqlConnection con = new SqlConnection(@MySqlCon);
- con.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = con;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = sqlStr;
- int iud = 0;
- iud = cmd.ExecuteNonQuery();
- con.Close();
- return iud;
- }
- }
- }
3. 修改代码里的 MySqlCon, 这一步用来连接到数据库, 至关重要.
在 "服务器资源管理" 处选中数据库, 然后可以在 "属性" 窗口找到 "连接字符串", 复制其内容, 赋给 MySqlCon. 比如我修改后是:
MySqlCon = "Data Source=DESKTOP-8LDERGD\\SQLEXPRESS;Initial Catalog = shaohui; Integrated Security = True";
完成这些操作后, 就可以在 form 里写代码来修改数据库了.
增删改查:
增删改查的实现都是 sql 语句, 把写好的 sql 语句赋给字符串, 然后执行. 这里需要注意的是, 增删改是用上面的 ExecuteUpdate() 函数, 而查询是用的 ExecuteQuery() 函数. 接下来以我的代码进行举例:
1. 查询, 不显示查询结果 (数据表名称为 DBuser)
- // 查询操作, 验证身份
- string my_user = username.Text;// 账号
- string my_pass = password.Text;// 密码
- string my_id = authority.Text;// 身份
- SQLServerDataBase SQLConn = new SQLServerDataBase();
- DataTable d1 = new DataTable();
- string sql = "select * from DBuser where username ='"+ my_user + "'and password ='"+ my_pass + "'and id ='"+my_id+"'";
- d1 = SQLConn.ExecuteQuery(sql);
- if(d1!=null&&d1.Rows.Count>0)
- {
- this.Hide();
- if(my_id=="教师")
- {
- HomeForm homeForm = new HomeForm();
- homeForm.Show();
- }
- else if(my_id == "管理员")
- {
- Administrator administrator = new Administrator();
- administrator.Show();
- }
- else if(my_id == "学生")
- {
- Student student = new Student(my_user);
- student.Show();
- }
- }
- else
- {
- MessageBox.Show("账号或密码错误!!");
- username.Text = "";
- password.Text = "";
- }
2. 查询, 显示查询结果, 我这里是将查询到的结果放在了 datagridview 控件中
- private void match2DB(string my_sql)
- {
- SQLServerDataBase SQLConn = new SQLServerDataBase();
- dataGridView_match.Rows.Clear();// 清空 datagridview 中数据
- int rank = 1;// 记录排名
- DataTable d1 = new DataTable();
- string sql = my_sql;
- d1 = SQLConn.ExecuteQuery(sql);
- if (d1 != null && d1.Rows.Count> 0)
- {
- for (int n = 0; n < d1.Rows.Count; n++)
- {
- dataGridView_match.Rows.Add(1);
- dataGridView_match.Rows[n].Cells["num"].Value = rank.ToString();
- dataGridView_match.Rows[n].Cells["class2"].Value = d1.Rows[n]["class"];
- dataGridView_match.Rows[n].Cells["no2"].Value = d1.Rows[n]["no"];
- dataGridView_match.Rows[n].Cells["name2"].Value = d1.Rows[n]["name"];
- dataGridView_match.Rows[n].Cells["match_name2"].Value = d1.Rows[n]["match_name"];
- dataGridView_match.Rows[n].Cells["match_grade2"].Value = d1.Rows[n]["match_grade"];
- rank++;
- }
- // 偶数行颜色设置
- for (int i = 0; i < dataGridView_match.RowCount; i++)
- {
- if (i % 2 == 1)
- dataGridView_match.Rows[i].DefaultCellStyle.BackColor = Color.Silver;
- }
- }
- }
3. 除了查询操作之外, 增加, 修改, 删除都是使用 ExecuteUpdate 函数, 使用方法类似, 下面只用增加作为例子
- SQLServerDataBase SQLConn = new SQLServerDataBase();
- string sql = "insert into DBuser values('" + username + "','" + password + "','" + id + "')";
- int result = SQLConn.ExecuteUpdate(sql);// 执行后会有返回值, 是 int 类型, 如果执行失败会返回 0;
- if (result != 0)
- {
- MessageBox.Show("添加成功", "添加结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("添加失败! 请重新添加!", "添加结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
4. 代码中 SQLServerDataBase SQLConn = new SQLServerDataBase(); 主要就是创建了一个连接数据库类的对象, 由于对数据库操作时多次重复使用, 所以建议可以在每一个窗口定义为全局变量
来源: http://www.bubuko.com/infodetail-3360334.html