c#数据绑定之向查询中添加参数(.Net连接外部数据库)
时间:2024-01-25 00:59:03
在ACCESS数据库中可以用MSSQL的形式定义操作字符串,也可以采用OLEDB的形式。
MSSQL 形式
string sqlText = @"SELECT * FROM [User] WHERE UserName= @name";
OLEDB的形式
string sqlText = @"SELECT * FROM [User] WHERE UserName= ?";
下一步是通过 OleDbCommand执行操作。
OleDbCommand dataAction = new OleDbCommand(sqlText,linkDB);
给参数赋值语句采用的方法为AddWithValue:
dataAction.Parameters.AddWithValue("@name","wangyong");
完整代码如下:
using (OleDbConnection linkDB = new OleDbConnection(@" Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\AiMeiLi.accdb"))
{
linkDB.Open();
string sqlText = @"SELECT * FROM [User] WHERE UserName= @name";
OleDbCommand dataAction = new OleDbCommand(sqlText,linkDB);
dataAction.Parameters.AddWithValue("@name","wangyong");
try
{
OleDbDataReader scanItems = dataAction.ExecuteReader();
if (scanItems.HasRows)
{
while (scanItems.Read())
{
MessageBox.Show(scanItems[1].ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show("Failure" + ex.Message);
}
}
标签:c#,数据绑定,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用pandas的DataFrame的plot方法绘制图像的实例
2023-07-02 08:33:52
![](https://img.aspxhome.com/file/2023/1/107661_0s.jpg)
vue 自定义指令directives及其常用钩子函数说明
2024-05-09 10:43:47
Python使用xlrd读取Excel格式文件的方法
2022-12-27 16:54:51
mysql对binlog的处理说明
2024-01-14 18:34:49
Python实现灰色关联分析与结果可视化的详细代码
2023-08-03 15:01:56
![](https://img.aspxhome.com/file/2023/3/61703_0s.png)
python3中join和格式化的用法小结
2022-03-02 01:45:22
Python中内置数据类型list,tuple,dict,set的区别和用法
2022-09-10 05:59:46
Vue.js中v-bind指令的用法介绍
2024-04-30 10:18:30
![](https://img.aspxhome.com/file/2023/3/130293_0s.jpg)
python迭代dict的key和value的方法
2021-11-03 16:04:17
Python input输入超时选择默认值自动跳过问题
2023-02-22 07:22:40
![](https://img.aspxhome.com/file/2023/5/112995_0s.png)
go语言 全局变量和局部变量实例
2024-04-26 17:18:56
![](https://img.aspxhome.com/file/2023/2/134262_0s.jpg)
Golang工作池的使用实例讲解
2024-05-08 10:53:14
Ubuntu16安装Python3.9的实现步骤
2021-01-23 14:04:53
![](https://img.aspxhome.com/file/2023/7/92397_0s.jpg)
Python 常用模块 re 使用方法详解
2021-02-23 13:10:19
golang的串行处理和并行处理区别
2024-02-19 18:33:11
Python+Turtle绘制幸运草的示例代码
2023-05-20 13:41:19
![](https://img.aspxhome.com/file/2023/0/77020_0s.png)
简单实现python数独游戏
2023-06-21 15:24:22
Python requests上传文件实现步骤
2023-09-25 15:14:50
FLASH 全屏播放
2008-07-19 11:36:00
Python机器学习工具scikit-learn的使用笔记
2021-08-30 10:28:50
![](https://img.aspxhome.com/file/2023/4/100914_0s.png)