C#?Sqlite數(shù)據(jù)庫(kù)的搭建及使用技巧
前言
前面我們用了,類庫(kù)對(duì)象進(jìn)行數(shù)據(jù)綁定,不懂的童鞋可以去找博主上一篇文章,今天給大家演示使用數(shù)據(jù)庫(kù)對(duì)datadridview進(jìn)行數(shù)據(jù)綁定,其實(shí)也很簡(jiǎn)單啦,讓我們卷起來(lái)。
1.1讓我們進(jìn)入正題吧
1.1.2(方法一)使用無(wú)代碼的方式,來(lái)使用數(shù)據(jù)庫(kù)綁定
1.1.3新建連接
建立自己的數(shù)據(jù)庫(kù)鏈接:
1.1.4選擇剛建立的鏈接
選擇你剛剛建立的鏈接,如果是用密碼的要選擇是,不然會(huì)鏈接失敗,登錄不成功
1.1.5表和Id
選擇你需要的表和Id,如果需要整個(gè)數(shù)據(jù)庫(kù)就全選。
1.1.6效果展示
如果你需要添加新的查詢,點(diǎn)擊下面的數(shù)據(jù)源添加就可以了。
2.1使用代碼的方式對(duì)DataGridView進(jìn)行數(shù)據(jù)綁定(包含使用MySQL)
2.1.1 使用代碼有著較多的方式,小編盡力把小編知道的方法寫出來(lái),雙擊button1,生成方法
2.1.2 連接數(shù)據(jù)庫(kù)
連接數(shù)據(jù)庫(kù),獲取數(shù)據(jù),對(duì)datagridview進(jìn)行數(shù)據(jù)綁定
private void button1_Click(object sender, EventArgs e) { string connString = "server=.;database= StudentDB; User ID = sa; Pwd=123456"; SqlConnection conn = new SqlConnection(connString); SqlCommand comm = new SqlCommand(); comm.Connection = conn; string sql = String.Format("select course_id '編號(hào)',course_name '課程名',teacher_name '教師姓名' from T_course"); try { conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, connString); DataSet ds = new DataSet(); da.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作數(shù)據(jù)庫(kù)出錯(cuò)!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
2.1.3 效果展示
這里使用的是SqL Server 數(shù)據(jù)庫(kù)。
2.1.4 其他方法
代碼其實(shí)還有其他的方法
string connString = "server=.;database= StudentDB; User ID = sa; Pwd=123456"; SqlConnection conn = new SqlConnection(connString); SqlCommand comm = new SqlCommand(); comm.Connection = conn; // string sql = String.Format("select course_id '編號(hào)',course_name '課程名',teacher_name '教師姓名' from T_course"); try { conn.Open(); string sql = "select course_id ,course_name,teacher_name from T_course"; SqlDataAdapter da = new SqlDataAdapter(sql, connString); DataSet ds = new DataSet(); da.Fill(ds,"studens");da. Fill (ds, "students");//參數(shù)1 : DataSet對(duì)象;參數(shù)2:名,自定義的名字,不需要和查詢的表名必須-致 //方法一使用時(shí)注解其他方法 dataGridView1.DataSource = ds; dataGridView1.DataMember ="studens"; //方法二 dataGridView1.DataSource = ds.Tables["studens"]; //方法三 DataTable dt = ds.Tables["studens"]; dataGridView1.DataSource = dt.DefaultView; conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作數(shù)據(jù)庫(kù)出錯(cuò)!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
2.1.5 對(duì)datagridview進(jìn)行數(shù)據(jù)綁定
使用MySQL也是可以對(duì)datagridview進(jìn)行數(shù)據(jù)綁定的,對(duì)數(shù)據(jù)庫(kù)連接方式改變,并且把那幾個(gè)類型前面為My,其他都是一樣的,提示要把命名空間導(dǎo)進(jìn)去,ALt+ENter快捷導(dǎo)入,如果沒(méi)有,導(dǎo)入哪里會(huì)叫你下載,下載就好了。
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; using MySql.Data.MySqlClient; namespace student { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string connString = "server=localhost; database=student; uid=root; pwd=88888888;Character Set=utf8;"; MySqlConnection conn = new MySqlConnection(connString); MySqlCommand comm = new MySqlCommand(); comm.Connection = conn; // string sql = String.Format("select course_id '編號(hào)',course_name '課程名',teacher_name '教師姓名' from T_course"); try { conn.Open(); string sql = "select course_id ,course_name,teacher_naem from T_course"; MySqlDataAdapter da = new MySqlDataAdapter(sql, connString); DataSet ds = new DataSet(); da.Fill(ds,"studens"); //方法一 dataGridView1.DataSource = ds; dataGridView1.DataMember ="studens"; //方法二 /* dataGridView1.DataSource = ds.Tables["studens"]; //方法三 DataTable dt = ds.Tables["studens"]; dataGridView1.DataSource = dt.DefaultView;*/ conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作數(shù)據(jù)庫(kù)出錯(cuò)!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } }
總結(jié)
DataGridView 這個(gè)控件博主基本寫完了,在寫文章中,學(xué)到很多東西,對(duì)自己的知識(shí)進(jìn)行鞏固,并且學(xué)習(xí)到了新的東西,datagirdview還有一個(gè)數(shù)據(jù)源是“服務(wù)”,博主沒(méi)有學(xué)習(xí)過(guò)這方面的知識(shí),只知道如果你要用到類似阿里云上面的數(shù)據(jù)庫(kù),可以用第三方數(shù)據(jù)庫(kù)軟件連接再嵌入
到此這篇關(guān)于C# Sqlite數(shù)據(jù)庫(kù)的搭建及使用技巧的文章就介紹到這了,更多相關(guān)C# Sqlite數(shù)據(jù)庫(kù)搭建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#中使用SQLite數(shù)據(jù)庫(kù)的方法介紹
- C#簡(jiǎn)單訪問(wèn)SQLite數(shù)據(jù)庫(kù)的方法(安裝,連接,查詢等)
- C#操作SQLite數(shù)據(jù)庫(kù)方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
- C#操作SQLite數(shù)據(jù)庫(kù)之讀寫數(shù)據(jù)庫(kù)的方法
- c#幾種數(shù)據(jù)庫(kù)的大數(shù)據(jù)批量插入(SqlServer、Oracle、SQLite和MySql)
- C#操作SQLite數(shù)據(jù)庫(kù)幫助類詳解
- C#連接加密的Sqlite數(shù)據(jù)庫(kù)的方法
- C#操作SQLite數(shù)據(jù)庫(kù)方法小結(jié)
相關(guān)文章
C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能
這篇文章主要為大家詳細(xì)介紹了C#控件Picturebox實(shí)現(xiàn)鼠標(biāo)拖拽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09C# Socket實(shí)現(xiàn)簡(jiǎn)單控制臺(tái)案例
這篇文章主要為大家分享了C# Socket簡(jiǎn)單的控制臺(tái)案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
這篇文章主要介紹了Unity 實(shí)現(xiàn)給物體替換材質(zhì)球的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04基于C#實(shí)現(xiàn)的三層架構(gòu)實(shí)例
這篇文章主要介紹了基于C#實(shí)現(xiàn)的三層架構(gòu)實(shí)例,非常實(shí)用,需要的朋友可以參考下2014-08-08c# WPF中System.Windows.Interactivity的使用
這篇文章主要介紹了c# WPF中System.Windows.Interactivity的使用,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03詳解如何選擇使用ArrayList、HashTable、List、Dictionary數(shù)組
本文詳細(xì)介紹了ArrayList、HashTable、List、Dictionary的用法,以及什么情況選用該數(shù)組,以便提高開發(fā)效率。希望對(duì)大家有所幫助2016-11-11