C# Sqlite数据库的搭建及使用实例分析

发布时间:2022-08-05 16:13:40 作者:iii
来源:亿速云 阅读:132

本文小编为大家详细介绍“C# Sqlite数据库的搭建及使用实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“C# Sqlite数据库的搭建及使用实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

1.1让我们进入正题吧

1.1.2(方法一)使用无代码的方式,来使用数据库绑定

C# Sqlite数据库的搭建及使用实例分析

1.1.3新建连接

建立自己的数据库链接:

C# Sqlite数据库的搭建及使用实例分析

C# Sqlite数据库的搭建及使用实例分析

1.1.4选择刚建立的链接

选择你刚刚建立的链接,如果是用密码的要选择是,不然会链接失败,登录不成功

C# Sqlite数据库的搭建及使用实例分析

1.1.5表和Id

选择你需要的表和Id,如果需要整个数据库就全选。

C# Sqlite数据库的搭建及使用实例分析

1.1.6效果展示

如果你需要添加新的查询,点击下面的数据源添加就可以了。

C# Sqlite数据库的搭建及使用实例分析

C# Sqlite数据库的搭建及使用实例分析

2.1使用代码的方式对DataGridView进行数据绑定(包含使用MySQL

2.1.1 使用代码有着较多的方式,小编尽力把小编知道的方法写出来,双击button1,生成方法

C# Sqlite数据库的搭建及使用实例分析

2.1.2 连接数据库

连接数据库,获取数据,对datagridview进行数据绑定

C# Sqlite数据库的搭建及使用实例分析

C# Sqlite数据库的搭建及使用实例分析

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

                }
            }
2.1.3 效果展示

这里使用的是SqL Server 数据库。

C# Sqlite数据库的搭建及使用实例分析

2.1.4 其他方法

代码其实还有其他的方法

C# Sqlite数据库的搭建及使用实例分析

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

                }
2.1.5 对datagridview进行数据绑定

使用MySQL也是可以对datagridview进行数据绑定的,对数据库连接方式改变,并且把那几个类型前面为My,其他都是一样的,提示要把命名空间导进去,ALt+ENter快捷导入,如果没有,导入哪里会叫你下载,下载就好了。

C# Sqlite数据库的搭建及使用实例分析

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数据库的搭建及使用实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. C#连接到SQL Server,SQLite数据库
  2. C#中算法的实例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sqlite

上一篇:Python类与实例如何使用

下一篇:Go编译原理之函数内联怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》