当前位置:网站首页>C# Sqlite database construction and use skills
C# Sqlite database construction and use skills
2022-08-04 17:06:00 【IC00】
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情
前言
前面我们用了,类库对象进行数据绑定,不懂的童鞋可以去找博主上一篇文章,今天给大家演示使用数据库对datadridview进行数据绑定,其实也很简单啦,让我们卷起来.
每日一遍,防止颓废
1.1让我们进入正题吧
1.1.2(方法一)使用无代码的方式,来使用数据库绑定
1.1.3新建连接,建立自己的数据库链接
1.1.4选择你刚刚建立的链接,如果是用密码的要选择是,不然会链接失败,登录不成功
1.1.5选择你需要的表和Id,如果需要整个数据库就全选.
1.1.6效果展示,如果你需要添加新的查询,点击下面的数据源添加就可以了.
2.1使用代码的方式对DataGridView进行数据绑定(包含使用MySQL)
2.1.1 使用代码有着较多的方式,小编尽力把小编知道的方法写出来,双击button1,生成方法
2.1.2 连接数据库,获取数据,对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);
}
}
123456789101112131415161718192021222324
复制代码
2.1.3 效果展示,博主使用的是SqL Server 数据库.
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");//参数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);
}
1234567891011121314151617181920212223242526272829
复制代码
2.1.5 使用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);
}
}
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
复制代码
总结
DataGridView 这个控件博主基本写完了,在写文章中,学到很多东西,对自己的知识进行巩固,并且学习到了新的东西,datagirdview还有一个数据源是“服务”,博主没有学习过这方面的知识,只知道如果你要用到类似阿里云上面的数据库,你可以用第三方数据库软件连接再嵌入,哈哈哈哈,当然这个方法可能有点傻但是博主只知道这些,或者你去学习WEf服务.创作不易希望大家,点赞,评论,关注.谢谢啦!!!
边栏推荐
- 一张图片怎么旋转90度。利用ps
- 越来越火的图数据库到底能做什么?
- el-date-picker 设置时间范围
- response的contentType 几种类型
- 消灭异步回调,还得是async-await
- Compose 类型稳定性注解:@Stable & @Immutable
- 罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
- Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
- R语言使用cov函数计算矩阵或者dataframe数据变量之间的协方差、cor函数计算相关性、cor函数通过method参数指定相关性、相关性计算方法Pearson,Spearman, Kendall
- 两个对象相同数据赋值
猜你喜欢
随机推荐
jMeter Transaction Controller 学习笔记
浅谈运用低代码技术如何实现物流企业的降本增效
吃透Chisel语言.32.Chisel进阶之硬件生成器(一)——Chisel中的参数化
谷粒商城笔记
response的contentType 几种类型
智慧场馆的功能有哪些
【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
华硕win11安全启动如何开启
How to convert an int attribute into a string in the json format returned by the Go language gin framework?
《分布式云最佳实践》分论坛,8月11日深圳见
WPF 光标初始化的时候 temp 文件夹满了无法创建
(1), the sequential storage structure of linear table chain storage structure
Qt自动补全之QCompleter使用
CSDN21天学习挑战赛——程序流程控制(02)
浙江数码代工M301H 免拆通刷_卡刷固件包(语音OK)
el-date-picker 设置时间范围
不需要服务器,教你仅用30行代码搞定实时健康码识别
正则过滤字符串中 script 标签
适配器模式
SAP ABAP SteammPunk 蒸汽朋克的最新进展 - 嵌入式蒸汽朋克