配置文件说明

web.config/app.config
你可以访问 http://lailexin.cnblogs.com 获得更好的格式支持

Section配置

<configSections>
	<sectionGroup name="dbproviders">
	<section name="mysql5" type="SimpleUnitity.Database.Config.DbProviderSection, SimpleUnitity"/>
	<section name="mysql" type="SimpleUnitity.Database.Config.DbProviderSection, SimpleUnitity"/>
	<section name="sqlserver" type="SimpleUnitity.Database.Config.DbProviderSection, SimpleUnitity"/>
	<section name="sqlite" type="SimpleUnitity.Database.Config.DbProviderSection, SimpleUnitity"/>
	<section name="oledb" type="SimpleUnitity.Database.Config.DbProviderSection, SimpleUnitity"/>
	</sectionGroup>
</configSections>
<dbproviders>
	<mysql Name="MySQL" ConnectionClass="MySql.Data.MySqlClient.MySqlConnection, mysql.data" DataAdapterClass="MySql.Data.MySqlClient.MySqlDataAdapter, mysql.data" IdentitySQL="SELECT LAST_INSERT_ID()" StatementSymbolAlias="$" ParameterPrefix="@" StatementPrefix="`" StatementSuffix="`"/>
	<mysql5 Name="MySQL5" ConnectionClass="MySql.Data.MySqlClient.MySqlConnection, mysql.data" DataAdapterClass="MySql.Data.MySqlClient.MySqlDataAdapter, mysql.data" IdentitySQL="SELECT LAST_INSERT_ID()" StatementSymbolAlias="$" ParameterPrefix="?" StatementPrefix="`" StatementSuffix="`"/>
	<sqlserver Name="MS SQL Server" ConnectionClass="System.Data.SqlClient.SqlConnection" DataAdapterClass="System.Data.SqlClient.SqlDataAdapter" StatementSymbolAlias="$" IdentitySQL="SELECT @@IDENTITY"/>
	<sqlite Name="Sqlite" ConnectionClass="System.Data.SQLite.SQLiteConnection, System.Data.SQLite" DataAdapterClass="System.Data.SQLite.SQLiteDataAdapter, System.Data.SQLite" IdentitySQL="SELECT LAST_INSERT_ID()" StatementSymbolAlias="$" ParameterPrefix="@" StatementPrefix="[" StatementSuffix="]"/>
	<oledb Name="Oledb" ConnectionClass="System.Data.OleDb.OleDbConnection" DataAdapterClass="System.Data.OleDb.OleDbDataAdapter" StatementSymbolAlias="$"/>
</dbproviders>


说明 如果是非引入程序集需要添加dll的名称, 上面的已经支持了SqlServer, MySql, Sqlite Oledb的连接
如果你不需要使用到DataTable可以尝试改造一下代码就可以免除DataAdapterClass的配置了

appSettings, connectionStrings配置
 <appSettings>
    <add key="DatabaseConfig" value="ProviderSection=dbproviders/mysql;ConnectionStringName=testdb;LogPath=d:/log/testdb;TimeMode=Hour;synclog=true"/>
  </appSettings>
  <connectionStrings>
    <add name="testdb" connectionString="server=127.0.0.1;database=test;user=xxx;password=xxx;port=3306;charset=utf8"/>
  </connectionStrings>


ProviderSection: 调用上面配置的section
ConnectionStringName: 连接字符串 (PS: 暂不支持连接字符串加密)
LogPath: SQL执行日志目录
TimeMode: 文件分割形式 Hour: 每小时生成一个(支持的值有: Month, Week, Hour, Minute)默认Hour
LogFile: 单一文件的日志
synclog=true: 使用异步方式写日志(默认false)
说明: 如果有LogPath优先于LogFile, 如果设置了LogPath将忽略LogFile
属性名不区分大小写, 但是值有区分(ProviderSection, ConnectionStringName, TimeMode)


demodb表
CREATE TABLE `mytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `order` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ;


添加数据
            int num = new Random().Next();

            var db = Helper.ParseDataHelper(ConfigurationManager.AppSettings["DatabaseConfig"]);
            // 插入一个数据并获取ID
            db.Open(); // 手动开启连接将不会自动关闭连接, 由于需要获取插入id因此需要手动开启连接
            DbData insertData = new DbData();
            insertData["code"] = "P001_" + num;
            insertData["name"] = "测试产品" + num;
            insertData["order"] = 1;    //注意 order是关键字, SimpleUnitity已经支持处理关键字
            db.Insert("mytable", insertData);
            int id = db.GetIdentity();
            Console.WriteLine("插入ID为:" + id);
            db.Close();

            Console.ReadLine();


Last edited Oct 17, 2013 at 10:29 AM by lailexin, version 8