实例化Database对象 |
Top Previous Next |
要使用NBearLite进行查询,首先,要实例化Database对象。
实例化Database对象主要有两种常用方法:
1、指定Web.config或App.config中的connectionStrings配置节中的connectionString的name。
例如:
对于下面的
<connectionStrings> <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" /> <add name="Postgres" connectionString="User ID=postgres;Password=sasa;Host=localhost;Port=5432;Database=postgres;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;" providerName="postgresql" /> </connectionStrings>
connectionStrings配置,我们可以用下面的语法实例化Database对象:
Database northwindDB = new Database("Northwind"); Database postgresDB = new Database("Postgres");
2、指定数据库类型和具体的ConnectionString。
例如:
Database northwindDB = new Database(DatabaseType.SqlServer, "Server=(local);Database=Northwind;Uid=sa;Pwd=sa"); Database postgresDB = new Database(DatabaseType.PostgreSql, "User ID=postgres;Password=sasa;Host=localhost;Port=5432;Database=postgres;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;"); |