今天小編給大家分享一下C#中Sqlite數據庫如何搭建及使用的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

建立自己的數據庫鏈接:


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

選擇你需要的表和Id,如果需要整個數據庫就全選。

如果你需要添加新的查詢,點擊下面的數據源添加就可以了。



連接數據庫,獲取數據,對datagridview進行數據綁定


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, "操作數據庫出錯!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}這里使用的是SqL Server 數據庫。

代碼其實還有其他的方法

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");//參數1 : DataSet對象;參數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, "操作數據庫出錯!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}使用MySQL也是可以對datagridview進行數據綁定的,對數據庫連接方式改變,并且把那幾個類型前面為My,其他都是一樣的,提示要把命名空間導進去,ALt+ENter快捷導入,如果沒有,導入哪里會叫你下載,下載就好了。

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, "操作數據庫出錯!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}以上就是“C#中Sqlite數據庫如何搭建及使用”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。