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 临时变量 ...
随机推荐
- python 标准库和第3方库的介绍
忘了从哪里来的了~~~~ Tkinter———— Python默认的图形界面接口.Tkinter是一个和Tk接口的模块,Tkinter库提供了对Tk API的接口,它属于Tcl/Tk的GUI工具组.T ...
- jQuery 获取页面元素的属性值
获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度 ...
- centos7下docker 部署javaweb
LXC linux container 百度百科:http://baike.baidu.com/link?url=w_Xy56MN9infb0hfYObib4PlXm-PW02hzTlCLLb1W2d ...
- MFC双缓冲绘图实例
本人之前一直了解双缓冲绘图的基本原理,但是在研究很久之后才大概知道具体的使用过程,本文将详细介绍本人在实际项目中使用双缓冲绘图的案例. 实现功能:主界面显示某张包含人脸的图片,通过dlib detec ...
- Selenium2怎么调用selenium1中方法
虽然selenium1.0已经成为过去时,现在都用selenium2.0,但是如果想要在代码中调用selenium1.0的api怎么办,看下面 WebDriver driver = new Chrom ...
- 移动web页面使用的字体的思考
前言 记得做PC端页面的时候,字体一般设置为微软雅黑,现在做起移动端页面来了,设计师们一般都还把字体设置为微软雅黑字体,但是做出来后,测试的时候发现页面中的字体不是微软雅黑,怎么办? 后来了解到的手机 ...
- POJ 1556 The Doors --几何,最短路
题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...
- HDU 2669 Romantic【扩展欧几里德】
裸的扩展欧几里德,求最小的X,X=((X0%b)+b)%b,每个X都对应一个Y,代入原式求解可得 #include<stdio.h> #include<string.h> ty ...
- flex4的s:states和mx:states的区别
http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf63611-7ffa.html#WS43468 ...
- linux启动jmeter,执行./jmeter.sh报错解决方法
1.l-bash: ./jmeter.sh: Permission denied解决办法:jmeter.sh的执行权限改改,是权限不够chmod 777 jmeter.sh 2.An error oc ...