linq查询语句转mongodb
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的更多相关文章
- c# linq查询语句详细使用介绍
本文介绍Linq的使用方法 linq介绍 LINQ只不过是实现IEnumerable和IQueryable接口的类的扩展方法的集合. LINQ可以查询IEnumerable集合或者IQueryable ...
- 简单的Linq查询语句
下面我来我大家介绍几种简单的查询方式. 1.简单语法 这个LINQ语句的第一个关键字是from,from后面加的是范围变量,范围变量后加in,后加上事先实例化的模型,然后点出数据的来源. List是列 ...
- C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询
1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...
- C# LINQ查询表达式用法对应Lambda表达式
C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...
- 【总结】LINQ查询基本操作列表
每个LINQ查询都以from子句开始,from子句包括以下两个功能. 指定查询将采用数据源. 定义一个本地变量,表示数据源中单个元素. string[] values = { "中国&quo ...
- MongoDB查询语句(转)
目录 查询操作 集合查询方法 find() 查询内嵌文档 查询操作符(内含 数组查询) "$gt" ."$gte". "$lt". &quo ...
- mongodb的查询语句学习摘要
看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...
- LINQ中的一些查询语句格式
LINQ的基本格式如下所示:var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式> ...
- 浅谈sql 、linq、lambda 查询语句的区别
浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...
随机推荐
- 【VB超简单入门】三、开始编程
接下来要进入正题了!同学们要认真看咯~ 第一步:安装VB开发IDE 在这里我推荐大家安装的是VB迷你版,现在大多数同学使用win7,这个版本可以在win7上运行的妥妥的~ 下载链接:http://pa ...
- 【VB超简单入门】二、知识准备
在开始编程之前,需要先熟悉一下各种操作和术语,以后学习编程才能得心应手. 首先最重要的操作当然就是-电脑的开机关机啦~(开个玩笑哈哈),必须掌握软件的安装和卸载,还有能编写批处理程序对平时的使用也是很 ...
- CentOS下一键安装Openstack
CentOS下一键安装Openstack 系统环境:Oracle VirtualBox 4.38CentOS-6.5-x86_64-bin-DVD1.iso 安装前需要修改 /etc/hosts文件, ...
- c++ Windows Socket实现最简单的C/S网络通信(TCP)
1.服务器端代码: #include<iostream> #include<WinSock2.h> #pragma comment(lib, "ws2_32.lib& ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- python中怎么查看当前工作目录和更改工作目录
查询当前目录:os.getcwd() 更改当前目录:os.chdir()
- UESTC 424 AreYouBusy --混合背包
混合三种背包问题. 定义:dp[i][k]表示体积为k的时候,在前i堆里拿到的最大价值. 第一类,至少选一项,dp初值全赋为负无穷,这样才能保证不会出现都不选的情况.dp[i][k] = max(dp ...
- Redis安全访问
限制IP(只能一个) # If you want you can bind a single interface, if the bind option is not # specified all ...
- UVA 12382 Grid of Lamps --贪心+优先队列
题意:给出每行每列至少有的灯泡数,问最少有的灯泡数. 解法:要使灯泡数尽量小,说明要使交叉点尽量多,这样即抵了行,又抵了列,为最优的.所以可以用行来消去列,也可以用列来消去行,我这里是列来消去行.首先 ...
- java7-4 继承的练习
1.Override和Overload的区别,Overload能改变返回值类型吗? 答: Override就是方法重写:在子类中,出现和父类中一模一样的方法声明的现象 Overload就是方法重载:在 ...