今天小编给大家分享一下C#中Sqlite数据库如何搭建及使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
安丘网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联公司自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。

建立自己的数据库链接:


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

选择你需要的表和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数据库如何搭建及使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。