web.config添加配置

  <connectionStrings>
<add name="connStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Example.mdb" providerName="System.Data.OleDb"/>
</connectionStrings>

App_Data文件夹中放

App_Start文件夹放帮助类:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
/// <summary>
/// 数据库操作类
/// </summary>
public class Common
{
private static OleDbConnection conn = new OleDbConnection();
private static OleDbCommand comm = new OleDbCommand();
public Common()
{ }
/// <summary>
/// 打开连接
/// </summary>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
try
{
conn.ConnectionString =System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
comm.Connection = conn;
conn.Open();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
/// <summary>
/// 关闭连接
/// </summary>
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Dispose();
comm.Dispose();
}
/// <summary>
/// 执行一条sql语句
/// </summary>
/// <param name="sqlStr">sql语句</param>
public static void ExecuteSql(string sqlStr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlStr;
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 返回一个数据集
/// </summary>
/// <param name="sqlStr">sql语句</param>
/// <returns></returns>
public static DataSet dataSet(string sqlStr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlStr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}
/// <summary>
/// 返回一个数据视图
/// </summary>
/// <param name="sqlStr">sql语句</param>
/// <returns></returns>
public static DataView dataView(string sqlStr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlStr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
}

或用这个帮助类:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb; /// <summary>
/// DBHelper 的摘要说明
/// </summary>
public class DBHelper
{
public static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public DBHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public DataSet GetDataSet(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
} public bool ExecSql(string sql)
{
bool IsSucceed = false;
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
conn.Close();
IsSucceed = true;
}
catch (Exception e)
{
throw e;
}
return IsSucceed;
} public OleDbDataReader GetReader(string sqlStr)
{
OleDbDataReader dr = null;
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
conn.Open();
try
{
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
conn.Close();
}
return dr;
} public int GetExexScalar(string sqlStr)
{
int ret = ;
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
conn.Open();
try
{
ret = (int)cmd.ExecuteScalar();
}
finally
{
conn.Close();
}
return ret;
} public string GetExexScalarString(string sqlStr)
{
string ret = "";
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
conn.Open();
try
{
ret = (string)cmd.ExecuteScalar();
}
finally
{
conn.Close();
}
return ret;
}
}

test:

        protected string GetTopNews()
{
StringBuilder sb = new StringBuilder();
string sqlStr = "select * from Employee";
DataTable dt = Common.dataSet(sqlStr).Tables[];
//  DataTable dt = new DBHelper().GetDataSet(sqlStr).Tables[0];
if (dt.Rows.Count > )
{
for (int i = ; i < dt.Rows.Count; i++)
{
sb.Append("<li>" + dt.Rows[i]["ID"] + ":" + dt.Rows[i]["EmpAddress"] + "</li>");
}
}
return sb.ToString();
}

linq查询语句转mongodb的更多相关文章

  1. c# linq查询语句详细使用介绍

    本文介绍Linq的使用方法 linq介绍 LINQ只不过是实现IEnumerable和IQueryable接口的类的扩展方法的集合. LINQ可以查询IEnumerable集合或者IQueryable ...

  2. 简单的Linq查询语句

    下面我来我大家介绍几种简单的查询方式. 1.简单语法 这个LINQ语句的第一个关键字是from,from后面加的是范围变量,范围变量后加in,后加上事先实例化的模型,然后点出数据的来源. List是列 ...

  3. C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询

    1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...

  4. C# LINQ查询表达式用法对应Lambda表达式

    C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...

  5. 【总结】LINQ查询基本操作列表

    每个LINQ查询都以from子句开始,from子句包括以下两个功能. 指定查询将采用数据源. 定义一个本地变量,表示数据源中单个元素. string[] values = { "中国&quo ...

  6. MongoDB查询语句(转)

    目录 查询操作 集合查询方法 find() 查询内嵌文档 查询操作符(内含 数组查询) "$gt" ."$gte". "$lt". &quo ...

  7. mongodb的查询语句学习摘要

    看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...

  8. LINQ中的一些查询语句格式

    LINQ的基本格式如下所示:var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式> ...

  9. 浅谈sql 、linq、lambda 查询语句的区别

    浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...

随机推荐

  1. puppet安装

    server  xuesong1     10.152.14.85 client  xuesong      10.152.14.106   系统centos5.8   两台配置都配置 /etc/ho ...

  2. 【C#】3.算法温故而知新 - 快速排序

    快速排序相比冒泡排序,每次交换是跳跃式的.每次排序的时候设置一个基准点,将小于等于基准点的数全部放到基准点的左边,将大于等于基准点的数放到基准点的右边.这样每次交换的时候就不会像冒泡排序一样只能在相邻 ...

  3. 2014 UESTC 暑前集训队内赛(3) 部分解题报告

    B.Battle for Silver 定理:完全图Kn是平面图当且仅当顶点数n<=4. 枚举所有完全图K1,K2,K3,K4,找出最大总权重. 代码: #include <iostrea ...

  4. 怪物彈珠Monster Strike 攻略

    火>水>木>光>暗 1.每天的曜日素材本,周一暗光,周二火,周参水,周四木,周五光乌龟,都是可以打整天的 2.另外补充,升经验用暗乌龟,切忌切记要塞给他随便一只烂宠升等,再吃掉 ...

  5. 每日一语:What is he getting at?

    What is he getting at? 他讲这话是什么意思? 2015-1-12

  6. BFGS方法

    今天看了 Nocedal 写的Numerical Optimization 中关于BFGS方法的介绍. BFGS方法有个近亲,叫做DFP方法.下面先介绍DFP方法. 这个方法的意图是找一种方法对Hes ...

  7. IOS定位服务的应用

    IOS定位服务的应用 一.授权的申请与设置 二.定位服务相关方法 三.定位服务代理的相关方法 四.定位服务获取到的位置对象 五.航标定位得到的航标信息对象 IOS定位服务的应用 一.授权的申请与设置 ...

  8. poj 1050 To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45906   Accepted: 24276 Desc ...

  9. AVR/Arduino定时/计数器、中断入门

    在Arduino中,可以使用AnalogWrite来使用硬件产生490Hz/980Hz的pwm波,并可根据参数来设定占空比.不了解这个的同学可以去AnalogWrite学习下,SecretsOfArd ...

  10. C语言 break跳出循环

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...