国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

手把手教你用SQLServer連接Visual Studio2019并編寫一個學(xué)生信息管理頁面

這篇具有很好參考價值的文章主要介紹了手把手教你用SQLServer連接Visual Studio2019并編寫一個學(xué)生信息管理頁面。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

安裝SQLServer

創(chuàng)建新項目

建數(shù)據(jù)庫建表

窗體設(shè)計

代碼實現(xiàn)

?整體效果

?


安裝SQLServer

用SQLServer連接Visual Studio,首先需要下載SQLServer app。

下載教程,我之前寫過,可以點擊如下鏈接先下載安裝SQLServer:

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

SQL Server(express)安裝教程

創(chuàng)建新項目

安裝好SQL之后,打開VisualStudio2019,新建一個window項目

,步驟如下:選擇創(chuàng)建新項目

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

選擇Windows窗體應(yīng)用(.NET Framework)? 點擊下一步。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

給文件命名為:WinFormsApplicationStudentManagement ,點擊下一步

?vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?點擊創(chuàng)建,就建好了

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

建數(shù)據(jù)庫建表

項目建好之后我們先連接數(shù)據(jù)庫。

1.找到服務(wù)器資源管理器

2.找到數(shù)據(jù)連接,右鍵,添加連接

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

數(shù)據(jù)源選擇Microsoft SQL Server數(shù)據(jù)庫文件(SqlClient)

數(shù)據(jù)庫文件名輸入StudentManagement 這是我們創(chuàng)建的數(shù)據(jù)庫的名稱

登錄方式身份驗證,選Windows身份驗證

點擊確定

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

點擊這樣就建好了數(shù)據(jù)庫

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?下面我們新建兩個表。

一個student表,存儲學(xué)號(主鍵)、姓名、年齡、分數(shù)。

一個college表,存儲專業(yè)號(主鍵),和專業(yè)名。

按照步驟:

選擇剛建好的數(shù)據(jù)庫,雙擊打開。

打開后找到表,右鍵,選擇添加新表。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?添加新表后,會出現(xiàn)如下界面:

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

按照我這樣設(shè)計表,設(shè)計完成后,點擊更新? 注意要更改表名。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫 點擊更新后,會出現(xiàn)如下界面:點擊更新數(shù)據(jù)庫即可。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

點擊更新數(shù)據(jù)庫按鈕。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?這是刷新之后的樣子,能看到剛才建立的student表。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?同樣方法,我們新建一個college表。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?這是college表建好之后的樣子。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

窗體設(shè)計

表建好了,我們開始設(shè)計窗體。

打開Form1.cs[Design]

設(shè)計如下頁面

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?布局如下:

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

組件在工具箱中添加;

組件name text 事件在屬性中編輯

左部:name=groupbox1 text=學(xué)生列表

中間:name=groupbox2 text=學(xué)生詳情

右邊:name=groupbox3 text=操作

下邊:richtextbox name=rtxtStudentList

寫完是這樣的。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?再來設(shè)計里面的界面:

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

設(shè)計好之后

label改text 分別改成 學(xué)號 姓名 年齡 分數(shù)

textbox改name 分別是txtId txtName txtAge txtScore

button改name和text

name分別改成 btnnew btnInsert btnUpdate btnDelete btnSave

text分別改成 新建 插入 更新 刪除 保存

ComboBox改name改成cboColleges

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?忘了,學(xué)生列表里還要建一個listbox 名字是listStudent

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?布局部分完成了。

開始寫代碼了。

代碼實現(xiàn)

我們使用的是Sqlconnection連接數(shù)據(jù)庫,所以要新增引用

using System.Data.SqlClient;

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

找到數(shù)據(jù)庫,右鍵屬性,找到連接字符串,全選復(fù)制到代碼中。

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

?我這是連好的:

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

之后的代碼如下:

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;
        }
    }
}
//單表改

?整體效果

vs和sql做一個學(xué)生管理系統(tǒng),C#,數(shù)據(jù)庫原理,c#,sqlserver,visual studio,數(shù)據(jù)庫

源碼下載:

鏈接:https://pan.baidu.com/s/1AksyhybGOjPy_3sgX_YqYw
提取碼:5ae1

用我的代碼 改一下數(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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 手把手教你用Python編寫郵箱腳本引擎

    手把手教你用Python編寫郵箱腳本引擎

    版權(quán)聲明:原創(chuàng)不易,本文禁止抄襲、轉(zhuǎn)載需附上鏈接,侵權(quán)必究! 郵箱是傳輸信息方式之一,個人,企業(yè)等都在使用,朋友之間發(fā)消息,注冊/登錄信息驗證,訂閱郵箱,企業(yè)招聘,向客戶發(fā)送消息等都是郵箱的使用場景;郵箱有兩個較重要的協(xié)議:SMTP和POP3,均位于OSI7層

    2024年02月06日
    瀏覽(104)
  • 手把手教你用 Jenkins 自動部署 SpringBoot

    手把手教你用 Jenkins 自動部署 SpringBoot

    CI/CD 是一種通過在應(yīng)用開發(fā)階段引入自動化來頻繁向客戶交付應(yīng)用的方法。 CI/CD 的核心概念可以總結(jié)為三點: 持續(xù)集成 持續(xù)交付 持續(xù)部署 CI/CD 主要針對在集成新代碼時所引發(fā)的問題(俗稱\\\"集成地獄\\\")。 為什么會有集成地獄這個“雅稱”呢?大家想想我們一個項目部署的

    2024年02月02日
    瀏覽(30)
  • 手把手教你用jmeter做壓力測試(詳圖)

    手把手教你用jmeter做壓力測試(詳圖)

    壓力測試是每一個Web應(yīng)用程序上線之前都需要做的一個測試,他可以幫助我們發(fā)現(xiàn)系統(tǒng)中的瓶頸問題,減少發(fā)布到生產(chǎn)環(huán)境后出問題的幾率;預(yù)估系統(tǒng)的承載能力,使我們能根據(jù)其做出一些應(yīng)對措施。所以壓力測試是一個非常重要的步驟,下面我?guī)Т蠹襾硎褂靡豢顗毫y試工

    2024年02月02日
    瀏覽(29)
  • 手把手教你用Git——詳解git merge

    關(guān)于本教程的編寫環(huán)境 本文基于 Windows10系統(tǒng) , Mac 系統(tǒng)的小伙伴可以嘗試 Homebrew 。由于本人手里并沒有搭載 MacOS 的電腦,因此 Homebrew 相關(guān)的使用請自行嘗試。 對于使用 Windows11系統(tǒng) 的小伙伴,本文的教程是通用的,不過一些細節(jié)可能略有不同,這點希望小伙伴們注意一下

    2024年02月05日
    瀏覽(27)
  • 手把手教你用UNet做醫(yī)學(xué)圖像分割系統(tǒng)

    手把手教你用UNet做醫(yī)學(xué)圖像分割系統(tǒng)

    兄弟們好呀,這里是肆十二,這轉(zhuǎn)眼間寒假就要過完了,相信大家的畢設(shè)也要準備動手了吧,作為一名大作業(yè)區(qū)的UP主,也該蹭波熱度了,之前關(guān)于圖像分類和目標檢測我們都出了相應(yīng)的教程,所以這期內(nèi)容我們搞波新的,我們用Unet來做醫(yī)學(xué)圖像分割。我們將會以皮膚病的數(shù)

    2024年02月03日
    瀏覽(92)
  • 手把手教你用MindSpore訓(xùn)練一個AI模型!

    手把手教你用MindSpore訓(xùn)練一個AI模型!

    首先我們要先了解深度學(xué)習(xí)的概念和AI計算框架的角色( https://zhuanlan.zhihu.com/p/463019160 ),本篇文章將演示怎么利用MindSpore來訓(xùn)練一個AI模型。和上一章的場景一致,我們要訓(xùn)練的模型是用來對手寫數(shù)字圖片進行分類的LeNet5模型 請參考( http://yann.lecun.com/exdb/lenet/ )。 圖1 M

    2024年02月04日
    瀏覽(34)
  • 手把手教你用git上傳項目到GitHub

    手把手教你用git上傳項目到GitHub

    github的官方網(wǎng)址:https://github.com?,如果沒有賬號,趕緊注冊一個。 點擊Sign in進入登錄界面,輸入賬號和密碼登入github。 創(chuàng)建成功可以看到自己的倉庫地址,如此,我的遠程免費的倉庫就創(chuàng)建了。它還介紹了github倉庫的常用指令。這個指令需要在本地安裝git客戶端。 Git是目

    2024年01月18日
    瀏覽(29)
  • 手把手教你用Python實現(xiàn)2048小游戲

    手把手教你用Python實現(xiàn)2048小游戲

    感覺好久沒有寫小游戲玩了,今天恰巧有空.這次我來用Python做個2048小游戲吧.廢話不多說,文中有非常詳細的代碼示例,需要的朋友可以參考下 目錄 一、開發(fā)環(huán)境 二、環(huán)境搭建 三、原理介紹 四、效果圖 Python版本:3.6.4 相關(guān)模塊: pygame模塊; 以及一些Python自帶的模塊。 安裝

    2024年04月28日
    瀏覽(94)
  • 手把手教你用 Docker 部署 Vue3 項目

    手把手教你用 Docker 部署 Vue3 項目

    用 docker 可以幫我們快速部署前端項目,本文介紹了如何用 docker 快速部署 vue3 項目。請準備好一臺云服務(wù)器并安裝好 docker,然后開始閱讀本教程。 執(zhí)行 npm run build 打包后項目目錄中會多出一個 dist 文件夾 利用 docker 拉取 nginx 鏡像 在服務(wù)器中創(chuàng)建工作目錄,這里我放在 /ho

    2024年02月05日
    瀏覽(34)
  • 爬蟲實戰(zhàn)|手把手教你用Python爬蟲(附詳細源碼)

    爬蟲實戰(zhàn)|手把手教你用Python爬蟲(附詳細源碼)

    實踐來源于理論,做爬蟲前肯定要先了解相關(guān)的規(guī)則和原理,要知道互聯(lián)網(wǎng)可不是法外之地,你一頓爬蟲騷操作搞不好哪天就… ?首先,咱先看下爬蟲的定義:網(wǎng)絡(luò)爬蟲(又稱為網(wǎng)頁蜘蛛,網(wǎng)絡(luò)機器人,在FOAF社區(qū)中間,更經(jīng)常的稱為網(wǎng)頁追逐者),是一種按照一定的規(guī)則,自

    2024年02月02日
    瀏覽(102)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包