Ejemplo n.º 1
0
        private void GvProdList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colBtnEdit.Index)
            {
                DataGridViewRow row        = this.gvProdList.Rows[e.RowIndex];
                FrmProduct      frmProduct = new FrmProduct();
                frmProduct.isNew = false;
                frmProduct.dsCategoriesCurrencies = (DataSetProducts)this.dsCategoriesCurrencies.Copy();
                frmProduct.ProductCode            = row.Cells[colProductCode.Index].Value.ToString();
                this.Hide();
                frmProduct.ShowDialog();
                if (frmProduct.dataSaved)
                {
                    GetProductList();
                }
                frmProduct.Dispose();
                this.Show();
            }
            else if (e.ColumnIndex == colBtnDelete.Index)
            {
                DialogResult result = MessageBox.Show("Are you sure to delete?",
                                                      "Confirm",
                                                      MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Question,
                                                      MessageBoxDefaultButton.Button2);

                if (result != DialogResult.Yes)
                {
                    return;
                }

                DataGridViewRow row         = this.gvProdList.Rows[e.RowIndex];
                String          productcode = row.Cells[colProductCode.Index].Value.ToString();

                String cmdQuery = @"DELETE FROM PRODUCTS WHERE PRODUCT_CODE=:PRODUCT_CODE";

                CdbcConnection con = UtilCommon.getConnection();
                CdbcCommand    cmd = new CdbcCommand(cmdQuery, con);

                CdbcParameter para = new CdbcParameter(":PRODUCT_CODE", CdbcOracleDbType.Varchar2);
                para.Value = productcode;
                cmd.Parameters.Add(para);
                try
                {
                    cmd.ExecuteNonQuery();
                    GetProductList();
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message,
                                    "Error",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error)
                    ;
                }
            }
        }
        public static void CommandCollection_ExecuteMultiQuery_Sample()
        {
            Console.WriteLine(String.Format("{0} CommandCollection_ExecuteMultiQuery_Sample", databaseProductType.ToString()));
            Console.WriteLine("Start");

            Console.WriteLine("--------------------------");
            Console.WriteLine("CommandCollection.ExecuteMultiQuery is original Method of CdbcCommandCollection.");
            Console.WriteLine("It can execute multi command in same transaction.");
            Console.WriteLine("It is designed to improve performance and Data Concurrency and Consistency");
            Console.WriteLine("--------------------------");
            Console.WriteLine("This sample will insert 3 records and than update one of the record.");
            Console.WriteLine("--------------------------");

            CdbcConnection con       = UtilCommon.getConnection(databaseProductType);
            string         queryStr1 = @"INSERT INTO CODE_SAMPLE(VAR1 , DATE1 ) VALUES (:VAR1, SYSDATE)";
            CdbcCommand    cmd1      = new CdbcCommand(queryStr1, con);

            string[] arrParas = new string[3];

            for (int i = 0; i < arrParas.Length; i++)
            {
                arrParas[i] = Guid.NewGuid().ToString();
            }

            cmd1.ArrayBindCount = 3;
            CdbcParameter prm =
                new CdbcParameter("VAR1", CdbcOracleDbType.Varchar2);

            prm.Value = arrParas;
            cmd1.Parameters.Add(prm);

            string      queryStr2 = @"UPDATE CODE_SAMPLE SET DATE1 = DATE1+1 WHERE VAR1=:VAR1";
            CdbcCommand cmd2      = new CdbcCommand(queryStr2, con);

            prm       = new CdbcParameter("VAR1", CdbcOracleDbType.Varchar2);
            prm.Value = arrParas[0];
            cmd2.Parameters.Add(prm);


            Console.WriteLine("--------------------------");
            Console.WriteLine("SQL 1 :" + queryStr1);
            Console.WriteLine("Para 1 :");

            for (int i = 0; i < arrParas.Length; i++)
            {
                Console.WriteLine(arrParas[i]);
            }
            Console.WriteLine("");
            Console.WriteLine("SQL 2 :" + queryStr2);

            Console.WriteLine("--------------------------");
            try
            {
                CdbcCommandCollection cmdCollection = new CdbcCommandCollection();
                cmdCollection.Add(cmd1);
                cmdCollection.Add(cmd2);
                cmdCollection.ExecuteMultiQuery();

                for (int iCmd = 0; iCmd < cmdCollection.Count; iCmd++)
                {
                    Console.WriteLine(String.Format("Command {0} Result : {1} ", iCmd, cmdCollection.ExecuteNonQueryResult[iCmd].ToString()));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine(ex.Message);
            }

            //Confirm Data
            Console.WriteLine("---Confirm Data-----------------------");
            string      queryStrSelect = @"SELECT VAR1, DATE1 FROM CODE_SAMPLE WHERE VAR1 IN (:VAR1_0 ,:VAR1_1,:VAR1_2) ";
            CdbcCommand cmdSelect      = new CdbcCommand(queryStrSelect, con);

            for (int i = 0; i < arrParas.Length; i++)
            {
                prm       = new CdbcParameter("VAR1_" + i.ToString(), CdbcOracleDbType.Varchar2);
                prm.Value = arrParas[i];
                cmdSelect.Parameters.Add(prm);
            }

            try
            {
                DbDataReader reader = cmdSelect.ExecuteReader();
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        Console.WriteLine(String.Format("{0} = {1}", reader.GetName(i), reader.GetValue(i).ToString()));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine(ex.Message);
            }

            //Clear Data
            Console.WriteLine("---Clear Data-----------------------");

            string      queryStrDelete = @"DELETE FROM CODE_SAMPLE WHERE VAR1=:VAR1";
            CdbcCommand cmdDelete      = new CdbcCommand(queryStrDelete, con);

            cmdDelete.ArrayBindCount = 3;
            prm =
                new CdbcParameter("VAR1", CdbcOracleDbType.Varchar2);
            prm.Value = arrParas;
            cmdDelete.Parameters.Add(prm);
            try
            {
                int i = cmdDelete.ExecuteNonQuery();

                Console.WriteLine(String.Format("{0} Line(s) Deleted.", i));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("--------------------------");
            Console.WriteLine("End");
        }
Ejemplo n.º 3
0
        public static void Command_ExecuteNonQuery_Sample()
        {
            Console.WriteLine(String.Format("{0} Command_ExecuteNonQuery_Sample", databaseProductType.ToString()));

            Console.WriteLine("Start");

            CdbcConnection con      = UtilCommon.getConnection(databaseProductType);
            string         queryStr = @"INSERT INTO CODE_SAMPLE(VAR1, NUM1, DATE1, CLOB1, BLOB1) VALUES (:VAR1, :NUM1, :DATE1, :CLOB1, :BLOB1)";
            CdbcCommand    cmd      = new CdbcCommand(queryStr, con);

            string guid = Guid.NewGuid().ToString();

            CdbcParameter para = new CdbcParameter(":VAR1", CdbcOracleDbType.Varchar2);

            para.Value = guid;
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":NUM1", CdbcOracleDbType.Decimal);
            para.Value = DateTime.Now.Second;
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":DATE1", CdbcOracleDbType.Date);
            para.Value = DateTime.Now;
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":CLOB1", CdbcOracleDbType.Clob);
            para.Value = DateTime.Now.Second;
            cmd.Parameters.Add(para);

            para = new CdbcParameter(":BLOB1", CdbcOracleDbType.Blob);
            //para.Value = System.IO.File.ReadAllBytes(@"your file path");
            cmd.Parameters.Add(para);


            Console.WriteLine("--------------------------");
            Console.WriteLine("Sample 1 : Insert a record into table CODE_SAMPLE");
            Console.WriteLine("Sql : " + queryStr);

            try
            {
                int i = cmd.ExecuteNonQuery();

                Console.WriteLine(String.Format("{0} Line(s) inserted.", i));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine(ex.Message);
            }


            string      queryStrDelete = @"DELETE FROM CODE_SAMPLE WHERE VAR1=:VAR1";
            CdbcCommand cmdDelete      = new CdbcCommand(queryStrDelete, con);

            para       = new CdbcParameter(":VAR1", CdbcOracleDbType.Varchar2);
            para.Value = guid;
            cmdDelete.Parameters.Add(para);


            Console.WriteLine("--------------------------");
            Console.WriteLine("Sample 2 : Delete the record we inserted before.");
            Console.WriteLine("Sql : " + queryStrDelete);

            try
            {
                int i = cmdDelete.ExecuteNonQuery();

                Console.WriteLine(String.Format("{0} Line(s) deleted.", i));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:");
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("--------------------------");
            Console.WriteLine("End");
        }
Ejemplo n.º 4
0
        private bool SaveData()
        {
            CdbcConnection con = UtilCommon.getConnection();
            string         cmdQuery;

            if (isNew)
            {
                cmdQuery = @"INSERT INTO PRODUCTS("
                           + "    PRODUCT_NAME"
                           + "  , PRODUCT_PRICE"
                           + "  , CURRENCY_CODE"
                           + "  , PRODUCT_SUMMARY"
                           + "  , CATEGORY_CODE"
                           + "  , PRODUCT_IMAGE"
                           + "  , RELEASE_DATE"
                           + "  , PRODUCT_CODE"
                           + " ) "
                           + "VALUES ("
                           + "    :PRODUCT_NAME"
                           + "  , :PRODUCT_PRICE"
                           + "  , :CURRENCY_CODE"
                           + "  , :PRODUCT_SUMMARY"
                           + "  , :CATEGORY_CODE"
                           + "  , :PRODUCT_IMAGE"
                           + "  , :RELEASE_DATE"
                           + "  , :PRODUCT_CODE"
                           + ")";
            }
            else
            {
                cmdQuery = @"UPDATE PRODUCTS "
                           + " SET PRODUCT_NAME    = :PRODUCT_NAME"
                           + "  ,  PRODUCT_PRICE   = :PRODUCT_PRICE"
                           + "  ,  CURRENCY_CODE   = :CURRENCY_CODE"
                           + "  ,  PRODUCT_SUMMARY = :PRODUCT_SUMMARY"
                           + "  ,  CATEGORY_CODE   = :CATEGORY_CODE"
                           + "  ,  PRODUCT_IMAGE   = :PRODUCT_IMAGE"
                           + "  ,  RELEASE_DATE    = :RELEASE_DATE"
                           + " WHERE PRODUCT_CODE  = :PRODUCT_CODE";
            }

            // Create the OracleCommand
            CdbcCommand cmd = new CdbcCommand(cmdQuery, con);

            CdbcParameter para = new CdbcParameter(":PRODUCT_NAME", Cdbc.Common.Data.Type.CdbcOracleDbType.Varchar2);

            para.Value = txtProductName.Text;
            cmd.Parameters.Add(para);

            para = new CdbcParameter(":PRODUCT_PRICE", Cdbc.Common.Data.Type.CdbcOracleDbType.Decimal);
            int iPrice;

            if (Int32.TryParse(txtPrice.Text, out iPrice))
            {
                para.Value = iPrice;
            }
            else
            {
                MessageBox.Show("Invalid price.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error)
                ;
                return(false);
            }
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":CURRENCY_CODE", Cdbc.Common.Data.Type.CdbcOracleDbType.Varchar2);
            para.Value = cbCurrency.SelectedValue;
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":PRODUCT_SUMMARY", Cdbc.Common.Data.Type.CdbcOracleDbType.Varchar2);
            para.Value = txtSummary.Text;
            cmd.Parameters.Add(para);


            para       = new CdbcParameter(":CATEGORY_CODE", Cdbc.Common.Data.Type.CdbcOracleDbType.Varchar2);
            para.Value = cbCategory.SelectedValue;
            cmd.Parameters.Add(para);


            para       = new CdbcParameter(":PRODUCT_IMAGE", Cdbc.Common.Data.Type.CdbcOracleDbType.Blob);
            para.Value = UtilCommon.ImageToByteArray(pictureBox1.Image);
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":RELEASE_DATE", Cdbc.Common.Data.Type.CdbcOracleDbType.Date);
            para.Value = new DateTime(dtReleaseDate.Value.Year, dtReleaseDate.Value.Month, dtReleaseDate.Value.Day);
            cmd.Parameters.Add(para);

            para       = new CdbcParameter(":PRODUCT_CODE", Cdbc.Common.Data.Type.CdbcOracleDbType.Varchar2);
            para.Value = ProductCode;
            cmd.Parameters.Add(para);

            //if (!isNew)
            //{
            //    para = new CdbcParameter(":ISBN2", Cdbc.Common.Data.Type.CdbcOracleDbType.Varchar2);
            //    para.Value = txtISBN.Text;
            //    cmd.Parameters.Add(para);
            //}


            try
            {
                cmd.ExecuteNonQuery();
                return(true);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error)
                ;
                return(false);
            }
        }