处理oralce,sqlserver,access及其他常用数据库,下载和学习地址
1、web.config配置数据库连接字符串,以及数据库类型:
<connectionStrings>
<!--MySql-->
<add name="DosConn" providerName="Dos.ORM.MySql" connectionString="Data Source=127.0.0.1;Database=DBName;User Id=root;Password=root;" /> //未检验
<!--SqlServer-->
<add name="DosConn" providerName="Dos.ORM.SqlServer9" connectionString="server=127.0.0.1;database=DBName;uid=root;pwd=root;" /> //检验可以
<!--SqlServer-->
<add name="DosOracle" providerName="Dos.ORM.Oracle" connectionString="User ID=AQPU;Password=AQPU;Data Source=orcl;Max Pool Size=512"/> //经检验可以
</connectionStrings>
组件读取connectionStrings节点的配置的时候会根据providerName来判断是哪种数据库。
- Sql server 2000:Dos.ORM.SqlServer
- Sql server 2005/2008及以上:Dos.ORM.SqlServer9
- MS Access:Dos.ORM.MsAccess
- Sqlite:Dos.ORM.Sqlite
- Oracle:Dos.ORM.Oracle
- MySql:Dos.ORM.MySql
2、全局声明
public class DB {
public static readonly DbSession Context = new DbSession("DosConn");
}
3、开始使用Dos.ORM
var list = DB.Context.From<Dos.Model.TableName>().ToList();
4、使用示例
全局配置: public class DB { public static readonly DbSession Context = new DbSession("DosConn"); } 查询示例(Lambda表达式写法): DB.Context.From<Dos.Model.TableName>() .Select(d => new { d.ID, d.Price }) //select ID,Price from TableName .Where(d => (d.ID == 2 && d.Name != "itdos" && d.Name.In("com","net","cn") && ) || d.Sex != null) // where (id=2 and Name<>'itdos' and Name in('com','net','cn')) or Sex is not null .GroupBy(d => new { d.Name, d.Sex }) //group by Name,Sex .OrderBy(d => new { d.CreateTime, d.Name }) //order by CreateTime,Name .Having(d => d.Name != '') //having Name<>'' .Top(5) .Page(10, 2) //分页返回结果 每页10条返回第2页数据 .ToList(); //返回实体列表 //.ToFirst(); //返回第一个实体 //.ToFirstDefault(); //返回第一个实体,如果为null,则默认实例化一个 //.ToDataSet(); //返回DataSet //.ToDataReader(); //返回IDataReader //.ToDataTable(); //返回DataTable //.ToScalar(); //返回单个值 插入示例: //单个实体插入 var newModel = new Dos.Model.TableName(){ ID = Guid.NewGuid(), Name = "ITdos.com" }; var count1 = DB.Context.Insert<Dos.Model.TableName>(newModel);
//批量插入 var newList = new List<Dos.Model.TableName>(); newList.Add(new Dos.Model.TableName(){ ID = Guid.NewGuid(), Name = "ITdos.com" }); var count2 = DB.Context.Insert<Dos.Model.TableName>(newList);
删除示例: var count1 = DB.Context.Delete<Dos.Model.TableName>(d => d.ID == 1);
修改示例: var uptModel = DB.Context.From<Dos.Model.TableName>().Where(d => d.ID == 1).ToFirst(); uptModel.Attach(); uptModel.Name = "ITdos.net"; var count1 = DB.Context.Update<Dos.Model.TableName>(uptModel);