当前位置:网站首页>用于 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. 参考资料
边栏推荐
- 被校园暴力,性格内向的马斯克凄惨而励志的童年
- 您的物联网安全性是否足够强大?
- Why is the service implementation class always red
- 国泰君安证券靠谱吗?开证券账户安全吗?
- 项目实战!手把手教你 Jmeter 性能测试
- 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
- Built in functions for MySQL database operations
- A16z: metauniverse unlocks new opportunities in game infrastructure
- 腾讯游戏发布40多款产品与项目 其中12款为新游戏
- 视频编解码性能优化与实现
猜你喜欢

TensorRT 模型推理优化实现

Flow based depth generation model

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

導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅

More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!

R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision

ETCD数据库源码分析——集群间网络层服务端RaftHandler

如何获取GC(垃圾回收器)的STW(暂停)时间?

【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘

idea自动生成代码
随机推荐
Thesis reading: General advantageous transformers
【iptables&icmp】iptables默认策略中关于icmp协议的说明
Is it better for a novice to open a securities account? Is it safe to open a stock trading account
音视频技术开发周刊 | 251
s32ds跳转到DefaultISR
Tips for visiting the website: you are not authorized to view the recovery method of this page
嵌入式DSP音频开发
Built in functions for MySQL database operations
Arm development studio build compilation error
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
栈的基本操作(C语言实现)
"Everyday Mathematics" serial 53: February 21
What are the good practices of cloud cost optimization?
CURDATE()和NOW()区别
如何获取GC(垃圾回收器)的STW(暂停)时间?
Is online stock investment exchange group safe? Is it reliable to open an account for free?
抓包整理外篇fiddler————了解工具栏[一]
新手开哪家的证券账户是比较好?股票开户安全吗
[iptables & ICMP] description of ICMP Protocol in iptables default policy
Build your own website (17)