C# OleDbCommand 和 OleDbDataReader 使用小结
2005-11-05 12:59
507 查看
用代码直接说明好了.
using System.Data;
using System.Data.OleDb;
OleDbConnection thisConnection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("data.mdb"));//获取本地Access
//数据库路径
thisConnection.Open();
string strCom = "select * from account where uid = '" + strUid + "' and pwd = '" + strPwd + "'";
OleDbCommand thisCommand = new OleDbCommand(strCom,thisConnection);
thisCommand.ExecuteReader(); 或者 thisCommand.ExecuteNonQuery();
(这两句都可以执行sql语句)
thisConnection.Close();
将下划线部分替换为:
OleDbDataReader thisReader = thisCommand.ExecuteReader();
if(thisReader.HasRows)
{
thisReader.Read();
string strLevel = thisReader["level"].ToString();
}
或者
if(thisReader.Read())
{
string strLevel = thisReader["level"].ToString();
}
OleDbDataReader 中的bool类型HasRows属性可以判断查询结果是否为空.
thisReader["列名"] 可显示查询出的该列数据.
using System.Data;
using System.Data.OleDb;
OleDbConnection thisConnection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("data.mdb"));//获取本地Access
//数据库路径
thisConnection.Open();
string strCom = "select * from account where uid = '" + strUid + "' and pwd = '" + strPwd + "'";
OleDbCommand thisCommand = new OleDbCommand(strCom,thisConnection);
thisCommand.ExecuteReader(); 或者 thisCommand.ExecuteNonQuery();
(这两句都可以执行sql语句)
thisConnection.Close();
将下划线部分替换为:
OleDbDataReader thisReader = thisCommand.ExecuteReader();
if(thisReader.HasRows)
{
thisReader.Read();
string strLevel = thisReader["level"].ToString();
}
或者
if(thisReader.Read())
{
string strLevel = thisReader["level"].ToString();
}
OleDbDataReader 中的bool类型HasRows属性可以判断查询结果是否为空.
thisReader["列名"] 可显示查询出的该列数据.
相关文章推荐
- C# - 使用 OLEDB读取 excel(不用Excel对象).
- 使用C#连接Excel2010中得Microsoft.ACE.OLEDB.12.0,未注册问题的解决方案
- [VB.NET源码]78_Command和DataReader对象的使用
- [16-11-18]从[DHU教师排课助手]聊一下C#使用Oledb和range Cells[row,col]操作Excel
- C#中ref和out的使用小结
- OleDbCommand 或 OdbcCommand 使用参数要注意的几点。
- C#中ref和out的使用小结
- c#中数据库对象的介绍:dataset,dataadapter,connection,command,datareader
- OleDbCommand 或 OdbcCommand 使用参数要注意的几点。
- C#使用OLEDB读取Excel的一个注意点
- c#的default、using和yield使用小结
- 【c#操作office】--OleDbDataAdapter 与OleDbDataReader方式读取excel,并转换为datatable
- C# 使用 GetOleDbSchemaTable 检索架构信息(表、列、主键等)
- C#中ref和out的使用小结
- C#中使用split分割字符串的几种方法小结
- C# NamePipe使用小结
- C#中SqlDataAdapter的使用小结
- C# 中SqlParameter类的使用方法小结
- C#中ref和out的使用小结版本二
- C#中使用split分割字符串的几种方法小结