/// <summary> /// /// </summary> /// <param name="dbFilePath"></param> public DataBaseDriver(string dbFilePath) { _dbFilePath = dbFilePath; try { // Check if the database file is exists if (!File.Exists(_dbFilePath)) throw new FileNotFoundException("Файл базы данных не найден.", _dbFilePath); // Create and setup connection _sqlFactory = (SQLiteFactory)DbProviderFactories.GetFactory("System.Data.SQLite"); _sqlConnection = (SQLiteConnection)_sqlFactory.CreateConnection(); _sqlConnection.ConnectionString = "Data Source = " + _dbFilePath; // Open connection _sqlConnection.Open(); // Assign instance for a singleton _instance = this; } catch (Exception e) { MessageBox.Show(e.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines) { var connectionString = ConfigurationManager.ConnectionStrings["data"]; var providerFactory = new SQLiteFactory(); var repository = new Repository(providerFactory, connectionString.ConnectionString); container.Register(typeof(IRepository), repository); base.ApplicationStartup(container, pipelines); }
internal static void ExecuteNonQuery(string command, DbConnection conn, SQLiteFactory factory, DbTransaction trans) { using (DbDataAdapter adpt = factory.CreateDataAdapter()) { using (DbCommand cmd = conn.CreateCommand()) { cmd.Transaction = trans; cmd.CommandText = command; cmd.ExecuteNonQuery(); } } }
/// <summary> /// c-tor /// </summary> /// <param name="settings">Settings.</param> public SqlGuestbookApplication(IApplicationSettings settings) : base(settings) { _factory = new SQLiteFactory(); if (!File.Exists(DB_FILENAME)) { InitDatabase (); } DbManager.AddDataProvider("sqlite", typeof(SQLiteDataProvider)); DbManager.AddConnectionString("sqlite", "Data Source = " + DB_FILENAME); DbManager.DefaultConfiguration = "sqlite"; }
private static PetaPoco.Database GetDatabase() { // A sqlite database is just a file. String fileName = "badges.db"; if (!File.Exists(fileName)) { SeedDatabase(fileName); } String connectionString = String.Format("Data Source={0}", fileName); DbProviderFactory sqlFactory = new System.Data.SQLite.SQLiteFactory(); PetaPoco.Database db = new PetaPoco.Database(connectionString, sqlFactory); return(db); }
private static void QueryDatabaseOrm(string fileName) { // create a database "context" object String connectionString = String.Format("Data Source={0}", fileName); DbProviderFactory sqlFactory = new System.Data.SQLite.SQLiteFactory(); PetaPoco.Database db = new PetaPoco.Database(connectionString, sqlFactory); // load an array of POCO for Badges String sql = "select * from Badges"; foreach (Badge rec in db.Query <Badge>(sql)) { Console.WriteLine("{0} {1} {2}", rec.Id, rec.Title, rec.Description); } }
private void Connect(Action<DbConnection> action) { using (var factory = new SQLiteFactory()) { using (DbConnection connection = factory.CreateConnection()) { if (connection == null) { throw new InvalidOperationException("connection should not be null"); } connection.ConnectionString = _connectionString; connection.Open(); action(connection); } } }
internal static void ExecuteNonQuery(string command, DbConnection conn, SQLiteFactory factory) { DbTransaction trans = conn.BeginTransaction(System.Data.IsolationLevel.Serializable); try { conn.Open(); ExecuteNonQuery(command, conn, factory, trans); trans.Commit(); } catch (Exception) { trans.Rollback(); } finally { conn.Close(); } }
/// <summary> /// Ensures the database integrity. /// </summary> /// <param name="systemId">The system id.</param> /// <param name="descriptor">The descriptor.</param> /// <param name="mode">The mode.</param> /// <exception cref="UnexpectedNHibernateConfigurationException">Unexpected dialect. /// or /// Unexpected connection driver.</exception> /// <exception cref="InvalidConfigurationException"> /// Unable to connection information in NHibernate Descriptor settings. /// or /// Unable to find data source in connection string. /// </exception> /// <exception cref="Forge.Configuration.Shared.InvalidConfigurationException">Unable to connection information in NHibernate Descriptor settings. /// or /// Unable to find data source in connection string.</exception> public virtual void EnsureDatabaseIntegrity(long systemId, Dictionary<string, string> descriptor, SchemaFactoryModeEnum mode) { if (descriptor == null) { ThrowHelper.ThrowArgumentNullException("descriptor"); } if (descriptor.ContainsKey(DIALECT)) { if (!DIALECT_EXPECTED_VALUE.Equals(descriptor[DIALECT])) { throw new UnexpectedNHibernateConfigurationException("Unexpected dialect."); } } if (descriptor.ContainsKey(CONNECTION_DRIVER)) { if (!CONNECTION_DRIVER_EXPECTED_VALUE.Equals(descriptor[CONNECTION_DRIVER])) { throw new UnexpectedNHibernateConfigurationException("Unexpected connection driver."); } } string databaseFile = string.Empty; string connectionString = string.Empty; if (descriptor.ContainsKey(CONNECTION_STRING)) { connectionString = descriptor[CONNECTION_STRING]; databaseFile = GetDatabaseFile(connectionString); } else if (descriptor.ContainsKey(CONNECTION_STRING_NAME)) { connectionString = ConfigurationManager.ConnectionStrings[descriptor[CONNECTION_STRING_NAME]].ConnectionString; databaseFile = GetDatabaseFile(connectionString); } else { throw new InvalidConfigurationException("Unable to connection information in NHibernate Descriptor settings."); } if (string.IsNullOrEmpty(databaseFile)) { throw new InvalidConfigurationException("Unable to find data source in connection string."); } using (SQLiteFactory factory = new SQLiteFactory()) { FileInfo dbFileInfo = new FileInfo(databaseFile); if (!dbFileInfo.Exists) { using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; connection.Open(); } } else { if (mode == SchemaFactoryModeEnum.Create || mode == SchemaFactoryModeEnum.Create_And_Drop) { dbFileInfo.Delete(); using (DbConnection connection = factory.CreateConnection()) { connection.ConnectionString = connectionString; connection.Open(); } } } } }
public void SetSession(SqlProviders pType, string sHost, string sPort = "", string sUser = "", string sPass = "", string sData = "") { switch (pType) { case SqlProviders.SqlServer: dbFactory = SqlClientFactory.Instance; sConnection = "Server=" + sHost + (sPort.Length > 0 ? "," + sPort : ""); if (sUser != "") { sConnection += ";User Id=" + sUser; if (sPass != "") { sConnection += ";Password="******";Integrated Security=True;Trusted_Connection=True"; } sConnection += ";Initial Catalog=" + sData + ";"; dbConnection = dbFactory.CreateConnection(); dbCommand = dbFactory.CreateCommand(); dbConnection.ConnectionString = sConnection; dbCommand.Connection = dbConnection; break; case SqlProviders.SQLite: sqlFactory = SQLiteFactory.Instance; sDbPathSQLite = sHost + @"\" + sDbFileSQLite; if (!Directory.Exists(sHost)) { Directory.CreateDirectory(sHost); } SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder(); sb.DefaultTimeout = 5000; sb.SyncMode = SynchronizationModes.Off; sb.JournalMode = SQLiteJournalModeEnum.Memory; sb.PageSize = 65536; sb.CacheSize = 16777216; sb.FailIfMissing = false; sb.ReadOnly = false; sConnection = "data source=" + @sDbPathSQLite + ";" + sb.ConnectionString; sqlConnection = (SQLiteConnection)sqlFactory.CreateConnection(); sqlCommand = (SQLiteCommand)sqlFactory.CreateCommand(); sqlConnection.ConnectionString = sConnection; sqlCommand.Connection = sqlConnection; break; } }
public static void CreateConstant( PSCmdletBase cmdlet, //int testBucketId, ITestBucket bucket, string[] constantNames, object[] constantValues, System.Type[] constantTypes) { try { checkConnection(TestData.CurrentStructureDB.Connection); DbProviderFactory factory = new SQLiteFactory(); cmdlet.WriteVerbose(cmdlet, "begin transaction"); using (DbTransaction dbTrans = TestData.CurrentStructureDB.Connection.BeginTransaction()) { cmdlet.WriteVerbose(cmdlet, "creating a data adapter"); using (DbDataAdapter adpConstant = factory.CreateDataAdapter()) { cmdlet.WriteVerbose(cmdlet, "creating a command"); using (DbCommand cmd1 = TestData.CurrentStructureDB.Connection.CreateCommand()) { cmd1.Transaction = dbTrans; cmd1.CommandText = "SELECT * FROM TestConstants WHERE 1 = 2"; adpConstant.SelectCommand = cmd1; using (DbCommandBuilder bldConstant = factory.CreateCommandBuilder()) { bldConstant.DataAdapter = adpConstant; using (DataTable tblConstant = new DataTable()) { adpConstant.Fill(tblConstant); for (int i = 0; i < constantNames.Length; i++) { DataRow rowConstant = tblConstant.NewRow(); rowConstant["ConstantName"] = constantNames[i]; //rowConstant["ConstantTag"] = constantTags[i]; //rowConstant["Description"] = constantDescriptions[i]; rowConstant["ConstantValue"] = constantValues[i]; rowConstant["ConstantType"] = constantTypes[i]; rowConstant["BucketId"] = bucket.BucketId; tblConstant.Rows.Add(rowConstant); ITestConstant constant = new TestConstant( constantNames[i], constantValues[i], constantTypes[i]); constant.BucketId = bucket.BucketId; } cmdlet.WriteVerbose(cmdlet, "12"); cmdlet.WriteVerbose(cmdlet, tblConstant.Rows.Count.ToString() + " rows"); adpConstant.Update(tblConstant); cmdlet.WriteVerbose(cmdlet, tblConstant.Rows.Count.ToString() + " rows"); cmdlet.WriteVerbose(cmdlet, "14"); } } } } dbTrans.Commit(); } } catch (Exception eCreateConstant) { cmdlet.WriteError(cmdlet, "Failed to create test constant(s). " + eCreateConstant.Message, "CreateConstantFailed", ErrorCategory.InvalidOperation, true); } }
//public static void BackupTestResults(PSCmdletBase cmdlet, SQLiteConnection connection) public static void BackupTestResults(PSCmdletBase cmdlet, string databaseName) { // 20120920 SQLiteConnection connection = ConnectionMakeAlive(cmdlet, databaseName); DbProviderFactory factory = new SQLiteFactory(); cmdlet.WriteVerbose(cmdlet, "begin transaction"); using (DbTransaction dbTrans = connection.BeginTransaction()) { cmdlet.WriteVerbose(cmdlet, "creating a data adapter"); using (DbDataAdapter adp1 = factory.CreateDataAdapter()) { cmdlet.WriteVerbose(cmdlet, "creating a command"); using (DbCommand cmd1 = connection.CreateCommand()) { cmdlet.WriteVerbose(cmdlet, "1"); cmd1.Transaction = dbTrans; cmdlet.WriteVerbose(cmdlet, "2"); cmd1.CommandText = "SELECT * FROM TestSuites WHERE 1 = 2"; cmdlet.WriteVerbose(cmdlet, "3"); adp1.SelectCommand = cmd1; cmdlet.WriteVerbose(cmdlet, "4"); using (DbCommandBuilder bld1 = factory.CreateCommandBuilder()) { cmdlet.WriteVerbose(cmdlet, "5"); bld1.DataAdapter = adp1; cmdlet.WriteVerbose(cmdlet, "6"); using (DataTable tbl1 = new DataTable()) { cmdlet.WriteVerbose(cmdlet, "7"); adp1.Fill(tbl1); //for (int n = 0; n < 10000; n++) { cmdlet.WriteVerbose(cmdlet, "8"); for (int suiteCounter = 0; suiteCounter < TestData.TestSuites.Count; suiteCounter++) { cmdlet.WriteVerbose(cmdlet, "9"); DataRow row1 = tbl1.NewRow(); //row[1] = n; //(Id INTEGER PRIMARY KEY, SuiteId TEXT, SuiteName TEXT, StatusId NUMERIC, Description TEXT);"); row1["SuiteId"] = SQLiteHelper.PrepareEscapedString(TestData.TestSuites[suiteCounter].Id); row1["SuiteName"] = SQLiteHelper.PrepareEscapedString(TestData.TestSuites[suiteCounter].Name); row1["StatusId"] = (int)TestData.TestSuites[suiteCounter].enStatus; row1["Description"] = SQLiteHelper.PrepareEscapedString(TestData.TestSuites[suiteCounter].Description); tbl1.Rows.Add(row1); } cmdlet.WriteVerbose(cmdlet, "12"); cmdlet.WriteVerbose(cmdlet, tbl1.Rows.Count.ToString() + " rows"); adp1.Update(tbl1); cmdlet.WriteVerbose(cmdlet, tbl1.Rows.Count.ToString() + " rows"); cmdlet.WriteVerbose(cmdlet, "14"); //dbTrans.Commit(); } } } } cmdlet.WriteVerbose(cmdlet, "15"); using (DbDataAdapter adp2 = factory.CreateDataAdapter()) { cmdlet.WriteVerbose(cmdlet, "16"); using (DbCommand cmd2 = connection.CreateCommand()) { cmdlet.WriteVerbose(cmdlet, "17"); cmd2.Transaction = dbTrans; cmd2.CommandText = "SELECT * FROM TestScenarios WHERE 1 = 2"; cmdlet.WriteVerbose(cmdlet, "19"); adp2.SelectCommand = cmd2; cmdlet.WriteVerbose(cmdlet, "20"); using (DbCommandBuilder bld2 = factory.CreateCommandBuilder()) { cmdlet.WriteVerbose(cmdlet, "21"); bld2.DataAdapter = adp2; cmdlet.WriteVerbose(cmdlet, "22"); using (DataTable tbl2 = new DataTable()) { cmdlet.WriteVerbose(cmdlet, "23"); adp2.Fill(tbl2); //for (int n = 0; n < 10000; n++) { foreach (TestSuite testSuite in TestData.TestSuites) { for (int scenarioCounter = 0; scenarioCounter < testSuite.TestScenarios.Count; scenarioCounter++) { DataRow row2 = tbl2.NewRow(); //row[1] = n; //Id INTEGER PRIMARY KEY, SuiteId TEXT, ScenarioId TEXT, ScenarioName TEXT, StatusId NUMERIC, Description TEXT);"); row2["SuiteId"] = SQLiteHelper.PrepareEscapedString(testSuite.TestScenarios[scenarioCounter].SuiteId); row2["ScenarioId"] = SQLiteHelper.PrepareEscapedString(testSuite.TestScenarios[scenarioCounter].Id); row2["ScenarioName"] = SQLiteHelper.PrepareEscapedString(testSuite.TestScenarios[scenarioCounter].Name); //row["StatusId"] = (int)testSuite.TestScenarios[scenarioCounter]. row2["Description"] = SQLiteHelper.PrepareEscapedString(testSuite.TestScenarios[scenarioCounter].Description); tbl2.Rows.Add(row2); } } cmdlet.WriteVerbose(cmdlet, "27"); adp2.Update(tbl2); cmdlet.WriteVerbose(cmdlet, "28"); //dbTrans.Commit(); } } } } cmdlet.WriteVerbose(cmdlet, "29"); using (DbDataAdapter adp3 = factory.CreateDataAdapter()) { cmdlet.WriteVerbose(cmdlet, "30"); using (DbCommand cmd3 = connection.CreateCommand()) { cmdlet.WriteVerbose(cmdlet, "31"); cmd3.Transaction = dbTrans; cmd3.CommandText = "SELECT * FROM TestResults WHERE 1 = 2"; adp3.SelectCommand = cmd3; cmdlet.WriteVerbose(cmdlet, "34"); using (DbCommandBuilder bld3 = factory.CreateCommandBuilder()) { cmdlet.WriteVerbose(cmdlet, "35"); bld3.DataAdapter = adp3; cmdlet.WriteVerbose(cmdlet, "36"); using (DataTable tbl3 = new DataTable()) { cmdlet.WriteVerbose(cmdlet, "37"); adp3.Fill(tbl3); //for (int n = 0; n < 10000; n++) { foreach (TestSuite testSuite in TestData.TestSuites) { foreach (TestScenario testScenario in testSuite.TestScenarios) { for (int trCounter = 0; trCounter < testScenario.TestResults.Count; trCounter++) { DataRow row3= tbl3.NewRow(); //row[1] = n; //Id INTEGER PRIMARY KEY, TestResultId TEXT, TestResultName TEXT, " + //"StatusId NUMERIC, Description TEXT, ScriptName TEXT, LineNumber NUMERIC, " + //"Position NUMERIC, Error BLOB, Code TEXT, Description TEXT, Parameters BLOB, " + //"SuiteId TEXT, ScenarioId TEXT, Timestamp TEXT, TimeSpent NUMERIC, Generated NUMERIC, Screenshot TEXT);"); row3["SuiteId"] = SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].SuiteId); row3["ScenarioId"] = SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].ScenarioId); row3["TestResultId"] = SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].Id); row3["TestResultName"] = SQLiteHelper.PrepareEscapedString(SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].Name)); row3["ScriptName"] = SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].ScriptName); row3["LineNumber"] = testScenario.TestResults[trCounter].LineNumber.ToString(); row3["Position"] = testScenario.TestResults[trCounter].Position.ToString(); if (null != testScenario.TestResults[trCounter].Error) { row3["Error"] = testScenario.TestResults[trCounter].Error; } row3["Code"] = SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].Code); row3["Parameters"] = testScenario.TestResults[trCounter].Parameters; row3["Timestamp"] = testScenario.TestResults[trCounter].Timestamp; row3["TimeSpent"] = testScenario.TestResults[trCounter].TimeSpent; row3["Generated"] = Convert.ToInt32(testScenario.TestResults[trCounter].Generated); //row3["Screenshot"] = testScenario.TestResults[trCounter].Screenshot; if (null != testScenario.TestResults[trCounter].Screenshot) { byte[] screenshot = GetScreenshotFromFileSystem( testScenario.TestResults[trCounter].Screenshot); row3["Screenshot"] = screenshot; } row3["StatusId"] = (int)testScenario.TestResults[trCounter].enStatus; row3["Description"] = SQLiteHelper.PrepareEscapedString(testScenario.TestResults[trCounter].Description); tbl3.Rows.Add(row3); } } } cmdlet.WriteVerbose(cmdlet, "38"); adp3.Update(tbl3); cmdlet.WriteVerbose(cmdlet, "39"); //dbTrans.Commit(); cmdlet.WriteVerbose(cmdlet, "40"); } } } } cmdlet.WriteVerbose(cmdlet, "41"); using (DbDataAdapter adp4 = factory.CreateDataAdapter()) { cmdlet.WriteVerbose(cmdlet, "42"); using (DbCommand cmd4 = connection.CreateCommand()) { cmdlet.WriteVerbose(cmdlet, "43"); cmd4.Transaction = dbTrans; cmd4.CommandText = "SELECT * FROM TestResultDetails WHERE 1 = 2"; cmdlet.WriteVerbose(cmdlet, "45"); adp4.SelectCommand = cmd4; cmdlet.WriteVerbose(cmdlet, "46"); using (DbCommandBuilder bld4 = factory.CreateCommandBuilder()) { bld4.DataAdapter = adp4; cmdlet.WriteVerbose(cmdlet, "48"); using (DataTable tbl4 = new DataTable()) { cmdlet.WriteVerbose(cmdlet, "49"); adp4.Fill(tbl4); //for (int n = 0; n < 10000; n++) { foreach (TestSuite testSuite in TestData.TestSuites) { foreach (TestScenario testScenario in testSuite.TestScenarios) { foreach (TestResult testResult in testScenario.TestResults) { for (int trdCounter = 0; trdCounter < testResult.Details.Count; trdCounter++) { DataRow row4 = tbl4.NewRow(); //Id INTEGER PRIMARY KEY, TestResultId TEXT, TestResultDetailName TEXT, " + //"TestResultDetail BLOB);" row4["TestResultId"] = SQLiteHelper.PrepareEscapedString(testResult.Id); row4["TestResultDetailName"] = SQLiteHelper.PrepareEscapedString(testResult.Details[trdCounter].Name); row4["Timestamp"] = testResult.Details[trdCounter].Timestamp; row4["TestResultDetail"] = SQLiteHelper.PrepareEscapedString(testResult.Details[trdCounter].GetDetail().ToString()); tbl4.Rows.Add(row4); } } } } adp4.Update(tbl4); dbTrans.Commit(); } } } } } }
internal static void Initialize(HttpServerUtility server) { _factory = new SQLiteFactory(); var builder = (SQLiteConnectionStringBuilder)_factory.CreateConnectionStringBuilder(); builder.DataSource = server.MapPath(DatabasePath); builder.FailIfMissing = false; _connectionString = builder.ConnectionString; _connection = (SQLiteConnection)_factory.CreateConnection(); _connection.ConnectionString = _connectionString; _connection.Open(); using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "CREATE TABLE IF NOT EXISTS report (id INTEGER PRIMARY KEY AUTOINCREMENT, guid TEXT NOT NULL, name TEXT NOT NULL, created_at INTEGER NOT NULL, created_by TEXT NOT NULL, contents BLOB NOT NULL)"; cmd.ExecuteNonQuery(); } }