目錄
安裝SQLServer
創(chuàng)建新項目
建數(shù)據(jù)庫建表
窗體設(shè)計
代碼實現(xiàn)
?整體效果
?
安裝SQLServer
用SQLServer連接Visual Studio,首先需要下載SQLServer app。
下載教程,我之前寫過,可以點擊如下鏈接先下載安裝SQLServer:
SQL Server(express)安裝教程
創(chuàng)建新項目
安裝好SQL之后,打開VisualStudio2019,新建一個window項目
,步驟如下:選擇創(chuàng)建新項目
選擇Windows窗體應(yīng)用(.NET Framework)? 點擊下一步。
給文件命名為:WinFormsApplicationStudentManagement ,點擊下一步
?
?點擊創(chuàng)建,就建好了
建數(shù)據(jù)庫建表
項目建好之后我們先連接數(shù)據(jù)庫。
1.找到服務(wù)器資源管理器
2.找到數(shù)據(jù)連接,右鍵,添加連接
數(shù)據(jù)源選擇Microsoft SQL Server數(shù)據(jù)庫文件(SqlClient)
數(shù)據(jù)庫文件名輸入StudentManagement 這是我們創(chuàng)建的數(shù)據(jù)庫的名稱
登錄方式身份驗證,選Windows身份驗證
點擊確定
點擊是 這樣就建好了數(shù)據(jù)庫
?下面我們新建兩個表。
一個student表,存儲學(xué)號(主鍵)、姓名、年齡、分數(shù)。
一個college表,存儲專業(yè)號(主鍵),和專業(yè)名。
按照步驟:
選擇剛建好的數(shù)據(jù)庫,雙擊打開。
打開后找到表,右鍵,選擇添加新表。
?添加新表后,會出現(xiàn)如下界面:
按照我這樣設(shè)計表,設(shè)計完成后,點擊更新? 注意要更改表名。
點擊更新后,會出現(xiàn)如下界面:點擊更新數(shù)據(jù)庫即可。
點擊更新數(shù)據(jù)庫按鈕。
?這是刷新之后的樣子,能看到剛才建立的student表。
?同樣方法,我們新建一個college表。
?這是college表建好之后的樣子。
窗體設(shè)計
表建好了,我們開始設(shè)計窗體。
打開Form1.cs[Design]
設(shè)計如下頁面
?布局如下:
組件在工具箱中添加;
組件name text 事件在屬性中編輯
左部:name=groupbox1 text=學(xué)生列表
中間:name=groupbox2 text=學(xué)生詳情
右邊:name=groupbox3 text=操作
下邊:richtextbox name=rtxtStudentList
寫完是這樣的。
?再來設(shè)計里面的界面:
設(shè)計好之后
給label改text 分別改成 學(xué)號 姓名 年齡 分數(shù)
給textbox改name 分別是txtId txtName txtAge txtScore
給button改name和text
name分別改成 btnnew btnInsert btnUpdate btnDelete btnSave
text分別改成 新建 插入 更新 刪除 保存
給ComboBox改name改成cboColleges
?忘了,學(xué)生列表里還要建一個listbox 名字是listStudent
?布局部分完成了。
開始寫代碼了。
代碼實現(xiàn)
我們使用的是Sqlconnection連接數(shù)據(jù)庫,所以要新增引用
using System.Data.SqlClient;
找到數(shù)據(jù)庫,右鍵屬性,找到連接字符串,全選復(fù)制到代碼中。
?我這是連好的:
之后的代碼如下:
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 WindowsFormsApplicationNonetSql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadDatabase();
BindStudentList();
BindCollegeList();
}
string connectionString = @"Data Source=pc\sqlexpress;Initial Catalog=ailintext;Integrated Security=True";
DataSet dataSet = new DataSet();
DataTable students;
DataTable colleges;
private void BindStudentList()
{
listStudent.SelectedIndexChanged -= listStudent_SelectedIndexChanged;
listStudent.DataSource = students;
listStudent.DisplayMember = "student_name";
listStudent.ValueMember = "student_id";
listStudent.SelectedIndexChanged += listStudent_SelectedIndexChanged;
}
private void BindCollegeList()
{
listStudent.SelectedIndexChanged -= cboColleges_SelectedIndexChanged;
cboColleges.DataSource = colleges;
cboColleges.DisplayMember = "college_name";
cboColleges.ValueMember = "college_id";
listStudent.SelectedIndexChanged += cboColleges_SelectedIndexChanged;
}
private void LoadDatabase()
{
SqlConnection connection =new SqlConnection(connectionString);
string selectCommand = "select * from student";
SqlCommand command = new SqlCommand();
command.CommandText = selectCommand;
command.Connection = connection;
//適配器
SqlDataAdapter studentAdapter = new SqlDataAdapter();
studentAdapter.SelectCommand = command;
//讀入數(shù)據(jù) 適配器填充數(shù)據(jù)
studentAdapter.Fill(dataSet, "student");
students = dataSet.Tables["student"];
/*command.CommandText = "select * from college";*/
/* SqlDataAdapter collegeAdapter = new SqlDataAdapter(command);
collegeAdapter.Fill(dataSet, "college");
colleges = dataSet.Tables["college"];*/
students.PrimaryKey = new DataColumn[]
{
students.Columns["student_id"]
};
selectCommand = "select * from college";
command.CommandText = selectCommand;
SqlDataAdapter collegeAdapter = new SqlDataAdapter();
collegeAdapter.SelectCommand = command;
collegeAdapter.Fill(dataSet, "college");
colleges = dataSet.Tables["college"];
colleges.PrimaryKey = new DataColumn[]
{
colleges.Columns["college_id"]
};
DataRelation dr = new DataRelation(
"FK_student_college",
colleges.Columns["college_id"],
students.Columns["college_id"]);
dataSet.Relations.Add(dr);
}
private void listStudent_SelectedIndexChanged(object sender, EventArgs e)
{
//點擊學(xué)生姓名,返回學(xué)生信息
if (listStudent.SelectedIndex < 0)
{
return ;
}
//拿到信息
DataRow student = students.Rows.Find(listStudent.SelectedValue);
//顯示信息
txtId.Text = student["student_id"].ToString();
txtName.Text = student["student_name"].ToString();
txtAge.Text = student["student_age"].ToString();
txtScore.Text = student["student_score"].ToString();
cboColleges.SelectedValue = student["college_id"];
}
private void btnnew_Click(object sender, EventArgs e)
{
txtId.Text = string.Empty;
txtName.Text = string.Empty;
txtAge.Text = string.Empty;
txtScore.Text = string.Empty;
}
private void btnInsert_Click(object sender, EventArgs e)
{
DataRow newStudent = students.NewRow();
newStudent["student_id"] = txtId.Text;
newStudent["student_name"] = txtName.Text;
newStudent["student_age"] = txtAge.Text;
newStudent["student_score"] = txtScore.Text;
students.Rows.Add(newStudent);
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtId.Text))
{
return;
}
listStudent.SelectedIndexChanged -= listStudent_SelectedIndexChanged;
DataRow oldStudent = students.Rows.Find(txtId.Text);
oldStudent["student_id"] = txtId.Text;
oldStudent["student_name"] = txtName.Text;
oldStudent["student_age"] = txtAge.Text;
oldStudent["student_score"] = txtScore.Text;
if (cboColleges.SelectedIndex >= 0)
{
oldStudent["college_id"] = cboColleges.SelectedValue;
}
listStudent.SelectedIndexChanged += listStudent_SelectedIndexChanged;
}
private void btnSave_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
string selectCommand = "select * from student";
SqlCommand command = new SqlCommand();
//command.CommandText = "select * from student";
command.CommandText = selectCommand;
command.Connection = connection;
//適配器
SqlDataAdapter studentAdapter = new SqlDataAdapter();
studentAdapter.SelectCommand = command;
//
SqlCommandBuilder studentCommandBuilder = new SqlCommandBuilder();
studentCommandBuilder.DataAdapter = studentAdapter;
studentAdapter.Update(students);
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtId.Text))
{
return;
}
DataRow oldStudent = students.Rows.Find(txtId.Text);
oldStudent.Delete();
}
private void cboColleges_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboColleges.SelectedIndex < 0)
{
return;
}
DataRow college = colleges.Rows.Find(cboColleges.SelectedValue);
string s = string.Empty;
foreach(var item in college.GetChildRows(dataSet.Relations["FK_student_college"]))
{
s += item["student_id"] + "\t" + item["student_name"];
}
rtxtStudentList.Text = s;
}
}
}
//單表改
?整體效果
源碼下載:
鏈接:https://pan.baidu.com/s/1AksyhybGOjPy_3sgX_YqYw
提取碼:5ae1文章來源:http://www.zghlxwxcb.cn/news/detail-519444.html
用我的代碼 改一下數(shù)據(jù)庫連接字符串即可。仍有問題可以私信我,看到就回~文章來源地址http://www.zghlxwxcb.cn/news/detail-519444.html
到了這里,關(guān)于手把手教你用SQLServer連接Visual Studio2019并編寫一個學(xué)生信息管理頁面的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!