Sequential OleDbCommand named parameters, do you understand?

Source: Internet
Author: User
Tags assert

Access to an old project, which uses a lot of OleDbConnection for database operations, and in the execution of SQL block statements, it is not aware of its sequential parameters, named parameters. It is said that you cannot use named parameters, but I have tried them here, as if they were possible, but the order of the parameters is still required. See if you can figure out the output below?

Test environment: oledbconnection+oracle10g

using System;


using System.Data;


using System.Data.OleDb;


using System.Data.OracleClient;


using System.Text.RegularExpressions;


using System.Text;


using System.Collections;





using System.Diagnostics;


namespace ConsoleApplication1


{


///<summary>

Summary description of the
///Class1.


///</summary>


Class Program


    {








private void Test1 ()


        {


using (OleDbConnection conn = new OleDbConnection ("Provider=oraoledb.oracle; User Id=oauser;data source=oa163; password=1234; Persist security info=true; ")


            {


String sql = "begin delete from B; Insert into B (A,B) VALUES (: A,:B);        end; "; It's a normal
.




OleDbCommand cmd = new OleDbCommand (sql,conn);





cmd. Parameters.Add ("A", oledbtype.varchar,100);


cmd. Parameters["a"]. Value = "a";





cmd. Parameters.Add ("B", oledbtype.varchar,100);


cmd. Parameters["B"]. Value = "B";





cmd.commandtype = CommandType.Text;


Conn. Open ();


cmd. ExecuteNonQuery ();





cmd.commandtext = "Select a,b from B";


using (OleDbDataReader dr = cmd. ExecuteReader (Commandbehavior.singlerow))


                {


Debug.Assert (Dr. Read ());





Debug.Assert (Dr. GetString (Dr.        GetOrdinal ("a")) = = "a"); Normal result


Debug.Assert (Dr. GetString (Dr. GetOrdinal ("b")) = = "B");


                }


               





            }


        }





private void Test2 ()


        {


using (OleDbConnection conn = new OleDbConnection ("Provider=oraoledb.oracle; User Id=oauser;data source=oa163; password=1234; Persist security info=true; ")


            {


String sql = "begin delete from B; Insert into B (b,a) VALUES (: B,:A);  end; "; Here, change the order,
.




OleDbCommand cmd = new OleDbCommand (sql,conn);





cmd. Parameters.Add ("A", oledbtype.varchar,100);


cmd. Parameters["a"]. Value = "a";





cmd. Parameters.Add ("B", oledbtype.varchar,100);


cmd. Parameters["B"]. Value = "B";





cmd.commandtype = CommandType.Text;


Conn. Open ();


cmd. ExecuteNonQuery ();





cmd.commandtext = "Select a,b from B";


using (OleDbDataReader dr = cmd. ExecuteReader (Commandbehavior.singlerow))


                {


Debug.Assert (Dr. Read ());





Debug.Assert (Dr. GetString (Dr.                 GetOrdinal ("a")) = = "B"); It turns out to be different,
.

Debug.Assert (Dr. GetString (Dr. GetOrdinal ("b")) = = "a");


                }


               





            }


        }





private void Test3 ()


        {


using (OleDbConnection conn = new OleDbConnection ("Provider=oraoledb.oracle; User Id=oauser;data source=oa163; password=1234; Persist security info=true; ")


            {


String sql = "declare v_exists int: = 1;" +


"Begin" +


"Delete from B;" +


"SELECT COUNT (*) into v_exists from B where A=:a and B=:b and RowNum < 2;" +//Very normal


"if (v_exists = 0) then" +


"INSERT into B (A,B) VALUES (: A,:B);" +


"End If;" +


"END;";





OleDbCommand cmd = new OleDbCommand (sql,conn);





cmd. Parameters.Add ("A", oledbtype.varchar,100);


cmd. Parameters["a"]. Value = "a";





cmd. Parameters.Add ("B", oledbtype.varchar,100);


cmd. Parameters["B"]. Value = "B";





cmd.commandtype = CommandType.Text;


Conn. Open ();


cmd. ExecuteNonQuery ();


               


cmd.commandtext = "Select a,b from B";


using (OleDbDataReader dr = cmd. ExecuteReader (Commandbehavior.singlerow))


                {


Debug.Assert (Dr. Read ());





Debug.Assert (Dr. GetString (Dr.                 GetOrdinal ("a")) = = "a"); Normal result


Debug.Assert (Dr. GetString (Dr. GetOrdinal ("b")) = = "B");


                }





            }





        }


private void Test4 ()


        {


using (OleDbConnection conn = new OleDbConnection ("Provider=oraoledb.oracle; User Id=oauser;data source=oa163; password=1234; Persist security info=true; ")


            {


String sql = "declare v_exists int: = 1;" +


"Begin" +


"Delete from B;" +


"SELECT COUNT (*) into v_exists from B where B=:b and A=:a and RowNum < 2;" +//B=:b A nd a=:a Change the order


"if (v_exists = 0) then" +


"INSERT into B (A,B) VALUES (: A,:B);" +


"End If;" +


"END;";





OleDbCommand cmd = new OleDbCommand (sql,conn);





cmd. Parameters.Add ("A", oledbtype.varchar,100);


cmd. Parameters["a"]. Value = "a";





cmd. Parameters.Add ("B", oledbtype.varchar,100);


cmd. Parameters["B"]. Value = "B";





cmd.commandtype = CommandType.Text;


Conn. Open ();


cmd. ExecuteNonQuery ();


               


cmd.commandtext = "Select a,b from B";


using (OleDbDataReader dr = cmd. ExecuteReader (Commandbehavior.singlerow))


                {


Debug.Assert (Dr. Read ());





Debug.Assert (Dr. GetString (Dr.                 GetOrdinal ("a")) = = "B"); It turns out to be different,
.

Debug.Assert (Dr. GetString (Dr. GetOrdinal ("b")) = = "a");


                }





            }





        }





///<summary>


///The main entry point of the application.


///</summary>


[STAThread]


static void Main (string[] args)


        {


            //


//TODO: Add code here to start the application


            //





Try


            {


Program prog = new program ();





Prog. Test1 ();


Prog. Test2 ();


Prog. Test3 ();


Prog. Test4 ();





            }


catch (Exception exp)


            {


Console.WriteLine (exp. ToString ());


            }


finally


            {


Console.ReadLine ();


           


            }


        }


    }


}

It appears that it is possible to use block statements in Oleclient. I hope there won't be any problems in 9G.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.