C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

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

安丘網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

    1.1讓我們進(jìn)入正題吧

    1.1.2(方法一)使用無(wú)代碼的方式,來(lái)使用數(shù)據(jù)庫(kù)綁定

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    1.1.3新建連接

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

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    1.1.4選擇剛建立的鏈接

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

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    1.1.5表和Id

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

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    1.1.6效果展示

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

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    2.1使用代碼的方式對(duì)DataGridView進(jìn)行數(shù)據(jù)綁定(包含使用MySQL)

    2.1.1 使用代碼有著較多的方式,小編盡力把小編知道的方法寫出來(lái),雙擊button1,生成方法

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

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

    連接數(shù)據(jù)庫(kù),獲取數(shù)據(jù),對(duì)datagridview進(jìn)行數(shù)據(jù)綁定

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

     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ù)。

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    2.1.4 其他方法

    代碼其實(shí)還有其他的方法

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

      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ì)叫你下載,下載就好了。

    C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用

    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);
    
                    }
                }
        }
    }

    以上就是“C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

    當(dāng)前題目:C#中Sqlite數(shù)據(jù)庫(kù)如何搭建及使用
    當(dāng)前路徑:http://muchs.cn/article16/jpicgg.html

    成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司小程序開(kāi)發(fā)、電子商務(wù)、全網(wǎng)營(yíng)銷推廣、網(wǎng)站內(nèi)鏈企業(yè)網(wǎng)站制作

    廣告

    聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

    營(yíng)銷型網(wǎng)站建設(shè)