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 临时变量 ...
随机推荐
- [转]WCDMA系统结构及关键技术
本文转自:http://blog.csdn.net/lele52141/article/details/8498951 WCDMA系统结构: CN指核心网,UTRAN接入网,UE用户设备. UTRAN ...
- jmap,jhat分析内存
分析JAVA Application的内存使用时,jmap是一个很实用的轻量级工具.使用jmap可以查看heap空间的概要情况,粗略的掌握heap的使用情况.也可以生成heapdump文件,再使用jh ...
- java lambda小纪
一个通俗的说法是 :C#委托和Java中实现了 函数式编程的方法,它是函数式编程中的概念,为了更快的处理集合,在Java,c#等静态类型语言中想要引用一个函数的一种方式,(实现了通过封装匿名方法来达到 ...
- 怎样用ZBrush中的Curves和Insert笔刷创建四肢
之前的ZBrush教程给大家介绍了人体结构比例和肌肉走向,同时使用ZBrush®软件中的CuverTube笔刷为模型添加了颈部和手臂.使用InsertSphere笔刷添加腰部,本讲将继续使用Curv ...
- ZOJ 3967 Colorful Rainbows --栈的应用
题意:给出n条y=ai*x+bi的直线.对于这些直线,如果存在x使得该直线y大于其他任意一直线,那么这条直线可以被看见,问有多少条直线可以被看见. 做法什么的不讲了,参见:http://blog.cs ...
- Unity使用 UnityVS+VS2013 调试脚本
好消息:UnityVS免费啦 好消息:微软收购了UnityVS公司,UnityVS免费啦 下载地址:https://visualstudiogallery.msdn.microsoft.com/sit ...
- iOS打包导出时出现Missing iOS Distribution signing
iOS打包导出时出现Missing iOS Distribution signing 上传APP就出现Missing iOS Distribution signing indetity for 打包i ...
- iOS宏定义
1.__OBJC__宏定义作用 在.pch 文件中一般都会自动加上这句宏定义,表示宏内引用的文件确保只被使用Objective-C语言的文件所引用,保证引用关系的清晰.因为在一个OC工程中,可能包含. ...
- Gradle的HelloWorld
Gradle的脚本名为 build.gradle task hello{ doLast{ println("Hello World") } } 运行:gradle -q hell ...
- C语言 简单的队列(数组队列)
//简单的队列 #include<stdio.h> #include<stdlib.h> #define datatype int #define N 10 //定义队列结构体 ...