当前位置:网站首页>用于 C# 的 SQL 基本语法总结
用于 C# 的 SQL 基本语法总结
2022-06-28 02:34:00 【雪中亮】
「博客搬家」 新地址: 简书
本文目前用于 Visual Studio 2015 平台的 SQLite3 , 其他用法以后总结。
1. 使用 Nuget 添加 SQLite 库
在 Visual Studio 2015 中,选择:工具 -> NuGet 包管理器 -> 管理解决方案的 NuGet 程序包

在此界面下检索「SQLite」, 根据需要选择安装其中一个 SQLite 库,最好是红框部分内容中,二选一。
之后在界面的右侧,选择需要安装到的项目,之后进行安装操作即可。
2. 常用策略
2.1 对异常进行捕获
try
{
//将对数据库进行操作的语句放置在try中
}
catch (Exception ex)
{
//对异常进行处理
}2.2 事务及 SQLite 代码框架
//使用以下语句配合具体的SQL操作语句即可
//注:不主动控制事务时,每次执行SQL指令即会重复开启新的事务,
//会在SQL操作方面花费大量的时间。所以在进行大量插入、修改操作
//时,主动开启事务会极大节省时间
var conn = new SQLiteConnection("Data Source = ./bitkyData.db; Version = 3; ");
conn.Open();
var trans = conn.BeginTransaction(); //开启事务
var cmd = conn.CreateCommand();
try
{
//通过操作cmd,使用具体的SQL操作语句
}
catch (Exception)
{
trans.Rollback(); //事务回滚
//进行具体的异常处理
}
trans.Commit();//事务提交
conn.Close();//连接关闭3. 必备代码块
3.1 必要初始化操作
//指定数据库的地址,以及SQLite版本号
SQLiteConnection conn = new SQLiteConnection("Data Source = ./bitkyData.db; Version = 3; ");3.2 向表中插入指定的条目
void insertData()
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO ElectrodeController(typeA,typeB,typeM) VALUES (1, 2, 3)";
cmd.ExecuteNonQuery();
conn.Close();
}3.3 设置查询的条件,查询指定条目的内容
static void selectData()
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM ElectrodeController where num > 45";
var reader = cmd.ExecuteReader();//获取结果集
if (reader.HasRows)
{
while (reader.Read())
{
Debug.WriteLine("ID: " + reader.GetInt32(0));
}
}
conn.Close();
}3.4 获取数据表中条目的数量
static void getCount()
{
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM ElectrodeController";
var scalar = cmd.ExecuteScalar();\\获取结果集中第一行第一列的那个值
Debug.WriteLine("count: " + Convert.ToInt32(scalar));
conn.Close();
}4. 参考资料
边栏推荐
- 被校园暴力,性格内向的马斯克凄惨而励志的童年
- 音视频技术开发周刊 | 251
- 2022 electrician (elementary) recurrent training question bank and online simulation examination
- Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
- GAMES104 作业2-ColorGrading
- Apache——阿帕奇简介
- What is the best and safest software to download when buying stocks?
- Arm development studio build compilation error
- __getitem__和__setitem__
- RichView TRVStyle ParaStyles
猜你喜欢

Thesis reading: General advantageous transformers

Why is the service implementation class always red

Tencent games released more than 40 products and projects, including 12 new games

2022年R1快開門式壓力容器操作特種作業證考試題庫及答案

剑指 Offer 49. 丑数(三指针法)

Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red

In the digital era, enterprises must do well in user information security

根据Explain查看sql执行计划,对SQL进行优化

CURDATE()和NOW()区别

Dataloader参数collate_fn的使用
随机推荐
【小程序】使用font-awesome字体图标的解决文案(图文)
Reading makes people quiet
Etcd database source code analysis -- network layer server rafthandler between clusters
Is it safe to buy stocks and open an account through the account opening link of the broker manager? Want to open an account for stock trading
剑指 Offer 47. 礼物的最大价值(DP)
Built in functions for MySQL database operations
Win10 如何删除系统盘大文件hiberfil.sys
爱普生L3153打印机如何清洗喷头
剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
在牛客中使用JS编程题【split】
PPT制作小技巧
CI & CD 不可不知!
启牛商学院赠送证券账户是真的吗?开户到底安不安全呢
2022安全员-C证考试题库模拟考试平台操作
基于流的深度生成模型
Thesis reading: General advantageous transformers
Win 10出现bitlocke恢复,蓝屏错误代码0x1600007e
RichView TRVStyle
启牛开的证券账户是安全的吗?如何开账户呢
TensorRT 模型推理优化实现