亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VS連接SQL?server數(shù)據(jù)庫及實現(xiàn)基本CRUD操作

 更新時間:2023年01月13日 11:50:35   作者:song.22  
這篇文章主要給大家介紹了關(guān)于VS連接SQL?server數(shù)據(jù)庫及實現(xiàn)基本CRUD操作的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

連接數(shù)據(jù)庫

打開vs,點擊 視圖,打開sql資源管理器,添加SQL Server

輸入服務(wù)器名稱,用戶名,密碼,進(jìn)行連接。

如圖,就可以看到vs已經(jīng)連接到了自己的數(shù)據(jù)庫,class和song兩個數(shù)據(jù)庫 ??梢钥吹絚lass下面有五個表。

查看其中一個SC表,數(shù)據(jù)顯示正常,證明已連接。

使用dataGridView控件顯示表中的數(shù)據(jù)。

在工具箱中找到dataGridView控件拖入Form1中,如圖:

下面進(jìn)行底層代碼編寫

using System.Data;
using System.Data.SqlClient;
namespace connect_sql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
			Table();
        }
		//數(shù)據(jù)寫入,公共的,所有有表格的地方都用的上
		public void Table()
		{
			SqlConnection sqlcon = new SqlConnection();
			sqlcon.ConnectionString = "Data Source=LAPTOP-HIAIVLQI;Initial Catalog=class;Integrated Security=True";
			sqlcon.Open();
			dataGridView1.Rows.Clear();
			string sql = "select * from sc";
			SqlCommand com = new SqlCommand(sql, sqlcon);                
			SqlDataAdapter ada = new SqlDataAdapter(sql, sqlcon);//建立SQL語句與數(shù)據(jù)庫的連接
			DataSet ds = new DataSet();  //實例化Datatable類
			ada.Fill(ds); //添加SQL并且執(zhí)行                                      
			dataGridView1.DataSource = ds.Tables[0].DefaultView;//顯示數(shù)據(jù)   
		}
 
	}
}

 運行程序,F(xiàn)orm1窗體中已通過dataGridView顯示數(shù)據(jù),且數(shù)據(jù)與源數(shù)據(jù)無誤。

實現(xiàn)基本CRUD操作

創(chuàng)建people表格,打開sql資源管理器,鼠標(biāo)右鍵點擊對應(yīng)數(shù)據(jù)庫下的表,添加新表如下;

填寫相關(guān)sql語句,進(jìn)行建表。

插入以下兩條數(shù)據(jù):

 通過dataGridView顯示數(shù)據(jù),正常,插入成功。

 后續(xù)的CRUD操作不再贅述,都可通過下列代碼嵌入SQL語句進(jìn)行使用。

		public void Table()
		{
			SqlConnection sqlcon = new SqlConnection();
			sqlcon.ConnectionString = "Data Source=LAPTOP-HIAIVLQI;Initial Catalog=song;Integrated Security=True";//連接服務(wù)器
			sqlcon.Open();
			dataGridView1.Rows.Clear();
			string sql = "select * from people";//SQL語句,可自己編寫需要的。
			SqlCommand com = new SqlCommand(sql, sqlcon);                
			SqlDataAdapter ada = new SqlDataAdapter(sql, sqlcon);//建立SQL語句與數(shù)據(jù)庫的連接
			DataSet ds = new DataSet();  //實例化Datatable類
			ada.Fill(ds); //添加SQL并且執(zhí)行                                      
			dataGridView1.DataSource = ds.Tables[0].DefaultView;//顯示數(shù)據(jù)   
		}

那么以上就是VS連接SQL Server 數(shù)據(jù)庫一些基本操作。

如需了解具體代碼,可轉(zhuǎn)至我的gitee倉庫查詢:

https://gitee.com/song-77/vs-connection-sql-server

總結(jié)

到此這篇關(guān)于VS連接SQL server數(shù)據(jù)庫及實現(xiàn)基本CRUD操作的文章就介紹到這了,更多相關(guān)VS連接SQL server數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論