Exemplo n.º 1
0
        /// <summary>
        /// Execute update table to data base by command with args.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="command"></param>
        /// <param name="args"></param>
        /// <returns>Number of rows affected.</returns>
        public virtual int ExecuteNonQuery(DataTable table, string command, params SqliteParameter[] args)
        {
            try
            {
                using (var conn = new SqliteConnection(connectionString))
                {
                    conn.Open();
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = command;
                        cmd.Parameters.AddRange(args);

                        using (var adapter = new SqliteDataAdapter(cmd))
                        {
                            using (var builder = new SqliteCommandBuilder(adapter))
                            {
                                adapter.InsertCommand = builder.GetInsertCommand();
                                adapter.UpdateCommand = builder.GetUpdateCommand();
                                adapter.DeleteCommand = builder.GetDeleteCommand();

                                return(adapter.Update(table));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogException(ex);
                return(0);
            }
        }
Exemplo n.º 2
0
        [Category("NotWorking")]          // Requires newer sqlite than is on wrench
        public void XimarinBugzillaBug853Test()
        {
            const string     connectionString = "URI = file:./SqliteTest.db; Version = 3";        //will be in System.Data directory
            SqliteConnection dbConnection     = new SqliteConnection(connectionString);

            dbConnection.Open();
            SqliteCommand ClearTableEntry = new SqliteCommand("DELETE FROM Primus;", dbConnection);

            ClearTableEntry.ExecuteNonQuery();

            SqliteDataAdapter    sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
            SqliteCommandBuilder builder           = new SqliteCommandBuilder(sqliteDataAdapter);

            sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
            sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();

            DataSet dataSet = new DataSet();

            sqliteDataAdapter.Fill(dataSet, "Primus");            //reset

            DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();

            rowToBeAdded["id"]    = 123;
            rowToBeAdded["name"]  = "Name";           //not null primary key
            rowToBeAdded["value"] = 777;

            dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
            sqliteDataAdapter.Update(dataSet, "Primus");

            //This would fail with NULL constraint violation in bug
            //report.  Because before the patch, it would create
            //a new record with all fields being null-- if the
            //exception rises, test fails
            sqliteDataAdapter.Update(dataSet, "Primus");

            dbConnection.Close();
            dbConnection = null;
        }