/// <summary> /// Return True if can init db connection /// </summary> /// <returns></returns> bool Init_DbConnection() { var cs = ConfigurationManager.AppSettings.Get("ConnectionString"); // fix the bug Dialect Postgresql provider wrong datetime SqlServerOrmLiteDialectProvider dialect = SqlServerOrmLiteDialectProvider.Instance; dialect.UseUnicode = true; dialect.UseDatetime2(true); dialect.StringColumnDefinition = "nvarchar(MAX)"; dialect.StringLengthColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.StringLengthNonUnicodeColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.StringLengthUnicodeColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.NamingStrategy = new ABNamingStrategy(); dbFactory = new OrmLiteConnectionFactory(cs, dialect); dbFactory.AutoDisposeConnection = true; // now we try to open a connection to check try { PATH_DECRYPTION_INPUT = Db_GetSetting(Enum_Settings_Key.WEBSITE_DGL_AUTODECRYPT_INPUT, System.IO.Path.GetTempPath(), Enum_Settings_DataType.String).ToString(); PATH_DECRYPTION_OUTPUT = Db_GetSetting(Enum_Settings_Key.WEBSITE_DGL_AUTODECRYPT_OUTPUT, System.IO.Path.GetTempPath(), Enum_Settings_DataType.String).ToString(); log.Log(string.Format("Auto Decryption: Input folder = {0}, Output = {1}", PATH_DECRYPTION_INPUT, PATH_DECRYPTION_OUTPUT)); } catch (Exception ex) { log.Log("Auto Decryption"); log.Log(ex); return(false); } return(true); }
private static OrmLiteConnectionFactory GetDbConnectionFromConfig() { var cs = ConfigurationManager.AppSettings.Get("ConnectionString"); var db_type = ConfigurationManager.AppSettings.Get("DatabaseType").ToString().ToString().ToLower(); // for sqlite, need to check the app_data folder cs = cs.Replace("~/", "~/".MapHostAbsolutePath()); if (!System.IO.Directory.Exists("~/App_Data".MapHostAbsolutePath())) { System.IO.Directory.CreateDirectory("~/App_Data".MapHostAbsolutePath()); } // fix the bug Dialect Postgresql provider wrong datetime if (db_type == "sqlserver") { ModelBase.DatabaseType = PhotoBookmart.DataLayer.DatabaseTypeEnum.SQLServer; SqlServerOrmLiteDialectProvider dialect = SqlServerOrmLiteDialectProvider.Instance; dialect.UseUnicode = true; dialect.UseDatetime2(true); dialect.StringColumnDefinition = "nvarchar(MAX)"; dialect.StringLengthColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.StringLengthNonUnicodeColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.StringLengthUnicodeColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.NamingStrategy = new ABNamingStrategy(); var ret = new OrmLiteConnectionFactory(cs, dialect); ret.AutoDisposeConnection = true; return(ret); } //else if (db_type == "sqlite") //{ // ModelBase.DatabaseType = PhotoBookmart.DataLayer.DatabaseTypeEnum.Sqlite; // SqliteOrmLiteDialectProvider dialect = SqliteOrmLiteDialectProvider.Instance; // dialect.UseUnicode = true; // dialect.NamingStrategy = new ABNamingStrategy(); // var ret = new OrmLiteConnectionFactory(cs, dialect); // ret.AutoDisposeConnection = true; // return ret; //} //if (db_type == "postgresql") //{ // ModelBase.DatabaseType = PhotoBookmart.DataLayer.DatabaseTypeEnum.PostgreSQL; // PostgreSQLDialectProvider dialect = PostgreSQLDialectProvider.Instance; // dialect.UseUnicode = true; // dialect.NamingStrategy = new ABNamingStrategy(); // var ret = new OrmLiteConnectionFactory(cs, dialect); // ret.AutoDisposeConnection = true; // return ret; //} else { return(null); } }
bool ConnectDB() { SqlServerOrmLiteDialectProvider dialect = SqlServerOrmLiteDialectProvider.Instance; dialect.UseUnicode = true; dialect.UseDatetime2(true); dialect.StringColumnDefinition = "nvarchar(MAX)"; dialect.StringLengthColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.StringLengthNonUnicodeColumnDefinitionFormat = dialect.StringColumnDefinition; dialect.StringLengthUnicodeColumnDefinitionFormat = dialect.StringColumnDefinition; var ret = new OrmLiteConnectionFactory(this.tConnection.Text, dialect); ret.AutoDisposeConnection = true; try { this.Db = ret.OpenDbConnection(); return(true); } catch { return(false); } }