C#?Sqlite數(shù)據(jù)庫的搭建及使用技巧
前言
前面我們用了,類庫對象進(jìn)行數(shù)據(jù)綁定,不懂的童鞋可以去找博主上一篇文章,今天給大家演示使用數(shù)據(jù)庫對datadridview進(jìn)行數(shù)據(jù)綁定,其實也很簡單啦,讓我們卷起來。
1.1讓我們進(jìn)入正題吧
1.1.2(方法一)使用無代碼的方式,來使用數(shù)據(jù)庫綁定

1.1.3新建連接
建立自己的數(shù)據(jù)庫鏈接:


1.1.4選擇剛建立的鏈接
選擇你剛剛建立的鏈接,如果是用密碼的要選擇是,不然會鏈接失敗,登錄不成功

1.1.5表和Id
選擇你需要的表和Id,如果需要整個數(shù)據(jù)庫就全選。

1.1.6效果展示
如果你需要添加新的查詢,點擊下面的數(shù)據(jù)源添加就可以了。


2.1使用代碼的方式對DataGridView進(jìn)行數(shù)據(jù)綁定(包含使用MySQL)
2.1.1 使用代碼有著較多的方式,小編盡力把小編知道的方法寫出來,雙擊button1,生成方法

2.1.2 連接數(shù)據(jù)庫
連接數(shù)據(jù)庫,獲取數(shù)據(jù),對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 '編號',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ù)庫出錯!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}2.1.3 效果展示
這里使用的是SqL Server 數(shù)據(jù)庫。

2.1.4 其他方法
代碼其實還有其他的方法

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 '編號',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對象;參數(shù)2:名,自定義的名字,不需要和查詢的表名必須-致
//方法一使用時注解其他方法
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ù)庫出錯!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
} 2.1.5 對datagridview進(jìn)行數(shù)據(jù)綁定
使用MySQL也是可以對datagridview進(jìn)行數(shù)據(jù)綁定的,對數(shù)據(jù)庫連接方式改變,并且把那幾個類型前面為My,其他都是一樣的,提示要把命名空間導(dǎo)進(jìn)去,ALt+ENter快捷導(dǎo)入,如果沒有,導(dǎo)入哪里會叫你下載,下載就好了。

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 '編號',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ù)庫出錯!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}總結(jié)
DataGridView 這個控件博主基本寫完了,在寫文章中,學(xué)到很多東西,對自己的知識進(jìn)行鞏固,并且學(xué)習(xí)到了新的東西,datagirdview還有一個數(shù)據(jù)源是“服務(wù)”,博主沒有學(xué)習(xí)過這方面的知識,只知道如果你要用到類似阿里云上面的數(shù)據(jù)庫,可以用第三方數(shù)據(jù)庫軟件連接再嵌入
到此這篇關(guān)于C# Sqlite數(shù)據(jù)庫的搭建及使用技巧的文章就介紹到這了,更多相關(guān)C# Sqlite數(shù)據(jù)庫搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#中使用SQLite數(shù)據(jù)庫的方法介紹
- C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
- C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
- C#操作SQLite數(shù)據(jù)庫之讀寫數(shù)據(jù)庫的方法
- c#幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入(SqlServer、Oracle、SQLite和MySql)
- C#操作SQLite數(shù)據(jù)庫幫助類詳解
- C#連接加密的Sqlite數(shù)據(jù)庫的方法
- C#操作SQLite數(shù)據(jù)庫方法小結(jié)
相關(guān)文章
C#無法打開計算機“.”上的 IISADMIN 服務(wù)的解決方法
在使用c#進(jìn)行控制IIS服務(wù)啟動停止的時候,提示:無法打開計算機“.”上的 IISADMIN 服務(wù)2015-01-01

