private bool CreateSdfFile(string filepath) { bool fileCreated = false; PFDatabase db = null; StringBuilder connectionString = new StringBuilder(); try { db = new PFDatabase(DatabasePlatform.SQLServerCE40); //"data source='C:\Testfiles\Randomizer\TestTables.sdf';" connectionString.Length = 0; connectionString.Append("data source='"); connectionString.Append(filepath); connectionString.Append("';"); fileCreated = db.CreateDatabase(connectionString.ToString()); } catch (System.Exception ex) { _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); AppMessages.DisplayErrorMessage(_msg.ToString()); } finally { ; } return(fileCreated); }
/// <summary> /// Writes data contained in ADO.NET DataTable object to path stored in OutputFileName property. /// </summary> /// <param name="dt">DataTable object containing data to be output.</param> /// <returns>True if output operation is successful. False if write fails.</returns> public bool WriteDataToOutput(DataTable dt) { bool success = true; IDesktopDatabaseProvider db = null; string dbPlatformDesc = DatabasePlatform.Unknown.ToString(); string connStr = string.Empty; string nmSpace = string.Empty; string clsName = string.Empty; string dllPath = string.Empty; string templateFile = string.Empty; try { if (File.Exists(_outputFileName)) { if (_replaceExistingFile) { try { File.SetAttributes(_outputFileName, FileAttributes.Normal); File.Delete(_outputFileName); } catch (System.Exception ex) { _msg.Length = 0; _msg.Append("Unable to delete old file. It may be locked by SQLAnywhere local server. Exit this application and try again. This should remove the lock."); _msg.Append(Environment.NewLine); _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } } else { _msg.Length = 0; _msg.Append("File exists and ReplaceExistingFile set to False. Write to Output has failed."); throw new System.Exception(_msg.ToString()); } } string oldLogFile = Path.Combine(Path.GetDirectoryName(_outputFileName), Path.GetFileNameWithoutExtension(_outputFileName) + ".Log"); if (File.Exists(oldLogFile)) { File.SetAttributes(oldLogFile, FileAttributes.Normal); File.Delete(oldLogFile); } if (this._desktopDbVersion == enDesktopDbVersion.SQLCE_Version40) { templateFile = Path.Combine(_defaultDbTemplatesFolder, _defaultSQLCE40TemplateFile); dbPlatformDesc = DatabasePlatform.SQLServerCE40.ToString(); } else if (this._desktopDbVersion == enDesktopDbVersion.SQLAnywhere) { templateFile = Path.Combine(_defaultDbTemplatesFolder, _defaultSQLAnywhereTemplateFile); dbPlatformDesc = DatabasePlatform.SQLAnywhere.ToString(); } else if (this._desktopDbVersion == enDesktopDbVersion.SQLAnywhere_UltraLite) { templateFile = Path.Combine(_defaultDbTemplatesFolder, _defaultSQLAnywhereUltraLiteTemplateFile); dbPlatformDesc = DatabasePlatform.SQLAnywhereUltraLite.ToString(); } else { templateFile = Path.Combine(_defaultDbTemplatesFolder, _defaultSQLCE35TemplateFile); dbPlatformDesc = DatabasePlatform.SQLServerCE35.ToString(); } string configValue = AppConfig.GetStringValueFromConfigFile(dbPlatformDesc, string.Empty); string[] parsedConfig = configValue.Split('|'); nmSpace = parsedConfig[0]; clsName = parsedConfig[1]; dllPath = parsedConfig[2]; db = new PFDatabase(dbPlatformDesc, dllPath, nmSpace + "." + clsName); if (_desktopDbVersion == enDesktopDbVersion.SQLCE_Version35 || _desktopDbVersion == enDesktopDbVersion.SQLCE_Version40) { db.DatabasePath = _outputFileName; connStr = db.ConnectionString; db.CreateDatabase(connStr); } else if (_desktopDbVersion == enDesktopDbVersion.SQLAnywhere_UltraLite) { connStr = OutputFileName; db.CreateDatabase(connStr); connStr = _defaultSQLAnywhereUltraLiteConnectionString.Replace("<filename>", OutputFileName); db.ConnectionString = connStr; } else if (_desktopDbVersion == enDesktopDbVersion.SQLAnywhere) { if (File.Exists(templateFile)) { connStr = _defaultSQLAnywhereConnectionString.Replace("<filename>", templateFile); db.ConnectionString = connStr; db.OpenConnection(); db.CreateDatabase(OutputFileName); db.CloseConnection(); //db.CreateDatabase(OutputFileName, templateFile); //file copy create not working properly: transaction log lock outs. connStr = _defaultSQLAnywhereConnectionString.Replace("<filename>", OutputFileName); db.ConnectionString = connStr; } else { _msg.Length = 0; _msg.Append("Unable to find template file for SQLAnywhere databases: "); _msg.Append(templateFile); _msg.Append("."); throw new System.Exception(_msg.ToString()); } } else { _msg.Length = 0; _msg.Append("Invalid or unexpected desktop database platform: "); _msg.Append(_desktopDbVersion); _msg.Append("."); throw new System.Exception(_msg.ToString()); } db.OpenConnection(); db.CreateTable(dt); db.CloseConnection(); db.OpenConnection(); db.ImportDataFromDataTable(dt); //this is very slow for SQLAnywhere UltraLite db.CloseConnection(); } catch (System.Exception ex) { success = false; _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } finally { if (db != null) { if (db.IsConnected) { db.CloseConnection(); } db = null; } } return(success); }