Configuration

Common:

DbEntry mainly using App.config/Web.config to set the configure information of databases. First, it has a section configure to define the new section org.hanzify.llf.Settings:

<configSections>
  <section name="org.hanzify.llf.Settings"
    type="org.hanzify.llf.util.Setting.NameValueSectionHandler, org.hanzify.llf.util" />
</configSections>

It is fixed, so we don’t need to change it, just copy it to the App.config/Web.config.

And then, we need define the section org.hanzify.llf.Settings, it is same format as appSettings:

<org.hanzify.llf.Settings>
  <add key="DataBase" value="@Access : @~test.mdb" />
</org.hanzify.llf.Settings>

The above defined the default context to using Access and the short connection string is ~test.mdb.

The configure information also can store as a embedded resource xml in the assembly, it is the same format as it in App.config/Web.config, if the setting information is not find in App.config/Web.config, it will search for assemblies to get it, if all failed, it will raise an exception.

This is an embedded resource xml configuration example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <org.hanzify.llf.Settings>
    <add key="DataBase" value="@Access : @~test.mdb" />
  </org.hanzify.llf.Settings>
</configuration>

The embedded resource xml configuration could be used sometime we can not set the configuration in App.config/Web.config, for example, I used it in the unit test project, because it is a library and don’t know who will load and run it.

There are some not usually used configure items: SqlTimeOut, TimeConsumingSqlTimeOut, MaxRecords, Orm.UsingParamter.

The SqlTimeOut and TimeConsumingSqlTimeOut are set for normal SQL calling from ORM layer, the timeout of SELECT SQL will be set as TimeConsumingSqlTimeOut, and others will be set as SqlTimeOut. The default value of SqlTimeOut is 30. The default value of TimeConsumingSqlTimeOut is 60.

<org.hanzify.llf.Settings>
  <add key="TimeConsumingSqlTimeOut" value="3600" />
  <add key="DataBase" value="@Access : @~test.mdb" />
</org.hanzify.llf.Settings>

There is a hidden mode of setting to set the mapped table name as another name. If we add the setting item and the key is start with @, it will be a re-map setting. The value will be the final table name. Sometimes I used it to change the table name of the inner classes of DbEntry like EnumTable and LogItem.

<org.hanzify.llf.Settings>
  <add key="@EnumTable" value="MyEnums" />
</org.hanzify.llf.Settings>

If we using SQL directly, the timeout will be 30 and we can change it manually.

SqlStatement sql = new SqlStatement("select count(*) from [User]");
sql.SqlTimeOut = 180;
DbEntry.Context.ExecuteScalar(sql);

The MaxRecords is set for SELECT SQL the max records count we will load, if this value is 0, it is unlimited, the default value is 0. This setting item could be used in some special time like SQL QUERYER, the SQL is input by end user, and we don’t want the result will be unlimited to delay our database response performance. Otherwise, let it be 0.

The Orm.UsingParamter is set for how to give the information by clauses. By default, this value is true. It means it will use SQL Parameter to send the information to database. If the value is false, it will replace the SQL Statement parts as the information, but it is not fully test, and doesn’t support all types. I used this setting only if I want see the whole SQL and don’t mind the result. (The spelling of this word in the setting is wrong, by this version, let it be)

Working with databases:

The following is how to set database context in config file.

DataBase joins with two part, first is Dialect, last is ConnectionString, split with ":". If the first of char of Dialect is @, it's a short style, and means it is a inner Dialect, ConnectionString for Access, Excel, SQLite, if the first char is @, it's a short style, just put the file name after @.

If there is {BaseDirectory} or ~ in short style ConnectionString, it will replace to current directory of this application (or web), so please ensure there is no {BaseDirectory} or ~ in ConnectionString you don't want.

For SQLite, the short style set cache to 100K and do not use system Flush function. (The origin provider set cache to 2K and use system Flush function.)

The UnitTest using SQLite, to run it, you should install the SQLite provider first.

DbProviderFactory should set DbProviderFactory fullname, for @SmartDbFactory, it join with two part, last is Assembly fullname, SmartDbFactory will search driver automaticly, it could be use as suport the driver of ADO.Net 1.x , and, it also provide search Stored Procedure's paramters.

Assembly's fullname could be get by AssemblyNameGetter.exe in the samples.

The sample of config:

Access:
<org.hanzify.llf.Settings>
  <add key="DataBase" value="@Access : @~test.mdb" />
</org.hanzify.llf.Settings>

Excel:
<org.hanzify.llf.Settings>
  <add key="DataBase" value="@Excel : @~test.xls" />
</org.hanzify.llf.Settings>

Sql Server:
<org.hanzify.llf.Settings>
  <add key="DataBase"
    value="@SqlServer2005 : data source=wms;initial catalog=wms;user id=sa;password=1" />
</org.hanzify.llf.Settings>

MySql: http://dev.mysql.com/downloads/connector/net/5.1.html
<org.hanzify.llf.Settings>
  <add key="DataBase"
    value="@MySql : server=localhost;user id=root; password=1; database=wms; pooling=false" />
  <add key="DbProviderFactory"
    value="@SmartDbFactory : MySql.Data, Version=5.1.2.2, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</org.hanzify.llf.Settings>

SQLite: http://sourceforge.net/project/showfiles.php?group_id=132486
<org.hanzify.llf.Settings>
  <add key="DataBase" value="@SQLite : @~Test.db" />
  <add key="DbProviderFactory"
    value="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.36.1, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</org.hanzify.llf.Settings>

Auto create table:

Another setting AutoCreateTable is set to tell DbEntry to create table if it doesn’t exist. The default value is false.

If it is activated, when some code want access database by using object model, DbEntry will try to create the table first if it doesn't exist.

This feature only works when the table doesn't exist, it doesn't judge columns.

This feature only works for ORM functions, execute SQL directly doesn't raise it.

This feature will help us to implements application prototype faster, it should only used in development or test.

When the application deployed to the working environment, change the value as false or delete this line in config file.

Attention: MySql doesn't have Unicode type of string, so if you specify the string column as Unicode type in object model, the created column size maybe not what we thought.

Default context

DbEntry.Net allows user pre-setup some database connections and set one of them as the default context.

The following config file shows 3 phases of project, and the current selected is development:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="org.hanzify.llf.Settings"
      type="org.hanzify.llf.util.Setting.NameValueSectionHandler, org.hanzify.llf.util" />
  </configSections>

  <org.hanzify.llf.Settings>
    <add key="DefaultContext" value="development" />

    <add key="test.DataBase" value="@Access : @~WMStest.mdb" />
    <add key="development.DataBase" value="@Access : @~WMSdevelp.mdb" />
    <add key="SqlServer.DataBase" value="@SqlServer2005 : data source=wms;initial catalog=WMS;user id=sa;password=1" />
  </org.hanzify.llf.Settings>
</configuration>


The following config file stored some databases connections infomation in it, and the current context is Access:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="org.hanzify.llf.Settings"
      type="org.hanzify.llf.util.Setting.NameValueSectionHandler, org.hanzify.llf.util" />
  </configSections>

  <org.hanzify.llf.Settings>
    <add key="DefaultContext" value="Access" />

    <add key="Access.DataBase" value="@Access : @~test.mdb" />

    <add key="Excel.DataBase" value="@Excel : @~test.xls" />

    <add key="SqlServer.DataBase" value="@SqlServer2005 : data source=wms;initial catalog=WMS;user id=sa;password=1" />

    <add key="MySql.DataBase"
      value="@MySql : server=localhost;user id=root; password=123; database=mytest; pooling=false" />
    <add key="MySql.DbProviderFactory"
      value="@SmartDbFactory : MySql.Data, Version=5.1.2.2, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </org.hanzify.llf.Settings>
</configuration>

Last edited Jul 26, 2007 at 6:50 AM by lifeng, version 5

Comments

No comments yet.