0

I am executing Pl/Sql block using OLEDB in C#. The code is:

        string a1;
        a1 = discom1.Text.ToString();

        my_querry10 = "BEGIN"+
                      "SELECT * from MasterCompliant WHERE Discom ='" + a1 + "';"+
                      "" + var1 + " = SQL%ROWCOUNT;"+
                      "END;";
        OleDbCommand cmd12 = new OleDbCommand(my_querry10, conn);
        conn.Open();

        cmd12.ExecuteNonQuery();

The exception is coming to statement:

"cmd12.ExecuteNonQuery" as "Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'."

MasterCompliant is table name; Discom is column name and var1 is integer.

3
  • Well your string starts with BEGINSTART - not a great starting point. You're also putting your parameters directly into your SQL string, which is a really bad idea. Use parameterized SQL instead. Do you really need to execute a block instead of just one command? It's not clear what you're trying to achieve here. Commented Aug 16, 2014 at 8:10
  • My task is: i am searching the string 'a1' from Discom column name and trying to retrieve its count in integer 'var1' using SQL&ROWCOUNT.. Commented Aug 16, 2014 at 8:16
  • This block will not work as intended even if you correct the syntax errors. You're executing a SELECT statement where you should open a cursor. Furthermore, you're attempting to assign SQL%ROWCOUNT to C# variable var1 from inside a PL/SQL block - this won't work as PL/SQL has no knowledge of your C# (or VB or whatever) variables. (And - use the assignment operator :=). Please - have a look at this question which shows HOW to get values back from an anonymous block. Best of luck. Commented Aug 16, 2014 at 20:01

2 Answers 2

1

It seems to me that you don't need a separate block for this at all - you can just use SELECT COUNT(1) and ExecuteScalar:

string sql = "SELECT COUNT(1) FROM MasterCompliant WHERE Discom = ?";
int count;

// Open and close a connection each time you need one - let the connection pool
// handle making that efficient.
using (var connection = new OleDbConnection(...))
{
    connection.Open();
    using (var command = new OleDbCommand(sql, conn))
    {
        command.Parameters.Add("@v", OleDbType.VarChar).Value = discom1.Text;
        count = (int) command.ExecuteScalar();
    }
}

It's possible that you need to cast to long instead of int - you should basically try it and see.

Note how using a parameterized query makes the SQL simpler to read and prevents SQL Injection attacks.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove semicolon near a1

Discom ='" + a1 + "';"+ to Discom ='" + a1 + "'"+

"BEGIN"+
"SELECT * from MasterCompliant WHERE Discom ='" + a1 + "'"+
"" + var1 + " = SQL%ROWCOUNT;"+
"END;"

1 Comment

Its not working.. similar exception even after removing the semicolon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.