Calling an Oracle Stored Procedure in C# - c#

I am trying to call an Oracle stored procedure from a C# program. I am using a SYS_REFCURSOR an the output of the stored procedure. I am getting invalid SQL error when I reach the line
OracleDataReader reader = cmd.ExecuteReader()
in my C# program. I can't figure out why I am getting this invalid SQL error.
Here is the C# code:
private void button1_Click(object sender, EventArgs e)
{
string custname;
int custnbr;
List<Customer> customers = new List<Customer>();
string oradb = "User Id=XXXXX;Password=XXXXX;Data Source=IP:PORT/xxxx;Pooling=false;";
OracleConnection conn = new OracleConnection(oradb);
try
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "PROCEDURE_TEST";
OracleParameter oraP = new OracleParameter();
oraP.ParameterName = "R_RECORDSET";
oraP.OracleDbType = OracleDbType.RefCursor;
oraP.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(oraP);
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
custnbr = reader.GetInt32(0);
custname = reader.GetString(1);
Customer custTemp = new Customer(custnbr, custname);
customers.Add(custTemp);
}
foreach (var cust in customers)
{
textBox1.AppendText("Customer Number: " + cust.custnbr + "\t");
textBox1.AppendText("Customer Name: " + cust.custname + "\r\n");
}
}
catch(Exception ex)
{
textBox1.AppendText(ex.ToString());
conn.Close();
}
}
Here is the Oracle stored procedure:
create or replace PROCEDURE PROCEDURE_TEST
( R_RECORDSET OUT SYS_REFCURSOR) AS
BEGIN
OPEN R_RECORDSET FOR
SELECT POTCHARGECATEGORY, POTCHARGECODE, POTCHARGEDESCRIPTION,
POTCHARGEBASEAMT, SUM(POTCHARGEQTY), SUM(POTCHARGEAMOUNT)
FROM riowner.ccum_customer customer
WHERE ic.collection_Datetime =
TO_DATE('30-SEP-2015 23:59:59','DD-MON-YYYY HH24:MI:SS')
GROUP BY POTCHARGECATEGORY, POTCHARGECODE, POTCHARGEDESCRIPTION,
POTCHARGEBASEAMT;
END PROCEDURE_TEST;

cmd.CommandType = CommandType.Text;
should be
cmd.CommandType = CommandType.StoredProcedure;

As an alternative to MethodMan's answer, you should be able to keep the command type as Text, but change your SQL command to this:
cmd.CommandText = "BEGIN PROCEDURE_TEST END;";
MethodMan's method is better if you just need to call one procedure, but the way I did it above would allow you to do more procedures, so it's something to be aware of in the future.

Related

Trying to add a C# variable into a row in an existing table using windows form [duplicate]

I'm able to delete, insert and update in my program and I try to do an insert by calling a created stored procedure from my database.
This button insert I made works well.
private void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(dc.Con);
SqlCommand cmd = new SqlCommand("Command String", con);
da.InsertCommand = new SqlCommand("INSERT INTO tblContacts VALUES (#FirstName, #LastName)", con);
da.InsertCommand.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add("#LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}
This is the start of the button that calls the procedure named sp_Add_contact to add a contact. The two parameters for sp_Add_contact(#FirstName,#LastName). I searched on google for some good examples but found nothing interesting.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(dc.Con);
SqlCommand cmd = new SqlCommand("Command String", con);
cmd.CommandType = CommandType.StoredProcedure;
???
con.Open();
da. ???.ExecuteNonQuery();
con.Close();
dt.Clear();
da.Fill(dt);
}
It's pretty much the same as running a query. In your original code you are creating a command object, putting it in the cmd variable, and never use it. Here, however, you will use that instead of da.InsertCommand.
Also, use a using for all disposable objects, so that you are sure that they are disposed properly:
private void button1_Click(object sender, EventArgs e) {
using (SqlConnection con = new SqlConnection(dc.Con)) {
using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
cmd.Parameters.Add("#LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
You have to add parameters since it is needed for the SP to execute
using (SqlConnection con = new SqlConnection(dc.Con))
{
using (SqlCommand cmd = new SqlCommand("SP_ADD", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#FirstName", txtfirstname.Text);
cmd.Parameters.AddWithValue("#LastName", txtlastname.Text);
con.Open();
cmd.ExecuteNonQuery();
}
}
cmd.Parameters.Add(String parameterName, Object value) is deprecated now. Instead use cmd.Parameters.AddWithValue(String parameterName, Object value)
Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value)
There is no difference in terms of functionality. The reason they
deprecated the cmd.Parameters.Add(String parameterName, Object value) in favor of AddWithValue(String parameterName, Object value) is to give more
clarity. Here is the MSDN reference for the same
private void button1_Click(object sender, EventArgs e) {
using (SqlConnection con = new SqlConnection(dc.Con)) {
using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
cmd.Parameters.AddWithValue("#LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
As an alternative, I have a library that makes it easy to work with procs: https://www.nuget.org/packages/SprocMapper/
SqlServerAccess sqlAccess = new SqlServerAccess("your connection string");
sqlAccess.Procedure()
.AddSqlParameter("#FirstName", SqlDbType.VarChar, txtFirstName.Text)
.AddSqlParameter("#FirstName", SqlDbType.VarChar, txtLastName.Text)
.ExecuteNonQuery("StoredProcedureName");
public void myfunction(){
try
{
sqlcon.Open();
SqlCommand cmd = new SqlCommand("sp_laba", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcon.Close();
}
}
The .NET Data Providers consist of a number of classes used to connect to a data source, execute commands, and return recordsets. The Command Object in ADO.NET provides a number of Execute methods that can be used to perform the SQL queries in a variety of fashions.
A stored procedure is a pre-compiled executable object that contains one or more SQL statements. In many cases stored procedures accept input parameters and return multiple values . Parameter values can be supplied if a stored procedure is written to accept them. A sample stored procedure with accepting input parameter is given below :
CREATE PROCEDURE SPCOUNTRY
#COUNTRY VARCHAR(20)
AS
SELECT PUB_NAME FROM publishers WHERE COUNTRY = #COUNTRY
GO
The above stored procedure is accepting a country name (#COUNTRY VARCHAR(20)) as parameter and return all the publishers from the input country. Once the CommandType is set to StoredProcedure, you can use the Parameters collection to define parameters.
command.CommandType = CommandType.StoredProcedure;
param = new SqlParameter("#COUNTRY", "Germany");
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
command.Parameters.Add(param);
The above code passing country parameter to the stored procedure from C# application.
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection connection ;
SqlDataAdapter adapter ;
SqlCommand command = new SqlCommand();
SqlParameter param ;
DataSet ds = new DataSet();
int i = 0;
connetionString = "Data Source=servername;Initial Catalog=PUBS;User ID=sa;Password=yourpassword";
connection = new SqlConnection(connetionString);
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "SPCOUNTRY";
param = new SqlParameter("#COUNTRY", "Germany");
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
command.Parameters.Add(param);
adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
MessageBox.Show (ds.Tables[0].Rows[i][0].ToString ());
}
connection.Close();
}
}
}
Here is my technique I'd like to share. Works well so long as your clr property types are sql equivalent types eg. bool -> bit, long -> bigint, string -> nchar/char/varchar/nvarchar, decimal -> money
public void SaveTransaction(Transaction transaction)
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
using (var cmd = new SqlCommand("spAddTransaction", con))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (var prop in transaction.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
cmd.Parameters.AddWithValue("#" + prop.Name, prop.GetValue(transaction, null));
con.Open();
cmd.ExecuteNonQuery();
}
}
}

OleDb Update database.mdb

When i use the CustomButton for to save the "Full_Name" in the Database [Rooms] => Person then there is just nothing happen. Also if i use the try & catch function, there will be no Exception.
The field in the Database stays Empty.
When i show the required variable in the MessageBox (idPlus2, Full_Name) then it throws me back the right informations.
So i think the problem must be in the UPDATE Sql string but i don't know whats wrong.
private string connstr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\....mdb";
//Path anonymous
string Full_Name;
[Obsolete]
private void customButton1_Click(object sender, EventArgs e)
{
conn = new OleDbConnection(connstr);
conn.Open();
strSQL = "SELECT * FROM [Guests] WHERE ID = ?";
cmd = new OleDbCommand(strSQL, conn);
da = new OleDbDataAdapter(cmd);
int id = CustomComboBox1.SelectedIndex;
int idPlus = id + 1;
cmd.Parameters.Add("?", idPlus);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Full_Name = reader["Vorname"].ToString() + ' ' + reader["Nachname"].ToString();
}
reader.Close();
string insertQuery = #"UPDATE [Rooms] SET Person = #Full_Name WHERE ID = ?";
cmd = new OleDbCommand(insertQuery, conn);
int id2 = customComboBox2.SelectedIndex;
int idPlus2 = id2 + 2;
cmd.Parameters.Add("?", idPlus2);
cmd.Parameters.Add(new OleDbParameter("#Full_Name", Full_Name));
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
LoadTheme();
}
I have the answer
cmd.Parameters.Add("?", OleDbType.VarChar, 255).Value = CustomComboBox1.Texts;
cmd.Parameters.Add("?", idPlus2);
With OleDb you have to use ? for each variable or object which should be added to the database. That means that you can't specify the variable by name in the SQL string. You have to use the same order as the SQL string in C # code to insert the parameters.

Sequential stored procedure with User Input

I have a web app that takes user input and generates an image from user input. Those images should be generated in a sequence and will need to reset everyday.
I'm trying to at least store the sequence values, the sequence and the date into a database, but it will not update.
So far, my database will not update, my code works, but is not performing correctly. I'm not sure where my problem lies. I found similar help online, but they did not seem to work.
Stored procedure:
ALTER PROCEDURE barcode_insert(
#Seq_Num int,
#date datetime,
#ImageName varchar
)
AS
BEGIN
SET NOCOUNT ON
UPDATE ImageInfoTable
SET imagedate = #date,ImageNum = #Seq_Num
WHERE image_name = #ImageName
END
RETURN #Seq_Num
C# Code
protected void gen_barcode(object sender, EventArgs e)
{
int n;
int i = Int32.Parse(amount.Text);
string date_picker = datepicker.Text;
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "barcode_insert"
cmd.Parameters.AddWithValue("#Seq_Num", amount.Text);
cmd.Parameters.AddWithValue("#date", date_picker);
cmd.Parameters.AddWithValue("#ImageName", CheckBox.Checked);
if (CheckBox_Code.Checked)
{
//generate image code
}
cmd.Connection.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
Try setting the types of your parameters explicitly.
protected void gen_barcode(object sender, EventArgs e)
{
DateTime date_picker = datepicker.Value;
int intAmount; // get the int value for the amount here...
String imgName; // get the image name here...
SqlConnection conn = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "barcode_insert"
cmd.Parameters.Add("#Seq_Num", SqlDbType.Int);
cmd.Parameters.Add("#date", SqlDbType.DateTime);
cmd.Parameters.Add("#ImageName" , SqlDbType.NVarChar);
cmd.Parameters["#Seq_Num"].Value = intAmount;
cmd.Parameters["#date"].Value = date_picker;
cmd.Parameters["#ImageName"].Value = imgName;
if (CheckBox_Code.Checked)
{
//put this somewhere else since it isn't related in function
}
cmd.Connection.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

Unable to execute MySql stored procedure from c#

In MySql:
DELIMITER //
DROP PROCEDURE IF EXISTS `testdb`.`Check_UserId_Sproc` //
CREATE PROCEDURE `testdb`.`Check_UserId_Sproc` (IN User_Id NVARCHAR(100))
BEGIN
select count(*) from demo_user where userid = User_Id;
END //
DELIMITER ;
In C#:
public DataTable ExecuteParameterizedSelectCommand(string CommandName, CommandType cmdType,MySqlParameter[] param)
{
DataTable table = new DataTable();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using(MySqlConnection con = new MySqlConnection(CS))
{
using (MySqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = CommandName;
cmd.Parameters.AddRange(param);
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
da.Fill(table);
}
}
catch
{
throw;
}
return table;
}
}
}
public DataTable checkExistingUserId()
{
MySqlDBHelper oHelper = new MySqlDBHelper();
MySqlParameter[] parameters = new MySqlParameter[]
{
new MySqlParameter("User_Id", 'DemoId')
};
return oHelper.ExecuteParameterizedSelectCommand("Check_UserId_Sproc", CommandType.StoredProcedure, parameters);
}
When I try to execute the checkExistingUserId(), I get following exception:
Incorrect number of arguments for PROCEDURE testdb.Check_UserId_Sproc; expected 1, got 0
May be I am doing a silly mistake but I am not able to figure it out. I am new to mysql and trying to work around it.
When I debug the array contains the parameter as seen in below image, but it is not collected by the SP.
Thanks in advance
In your code:
cmd.CommandType = CommandType.Text;
should be
cmd.CommandType = cmdType;

Getting error while supplying parameters to a stored procedure

I am getting the following error while supplying parameters to a stored procedure:
Procedure or function 'ismovieexists' expects parameter '#movie_name', which was not supplied
and the same error message for the procedure insert_values_in_movie_master..
public int add_movie(mymovie objmymovie)
{
SqlConnection cn = new SqlConnection(_connectionstring);
cn.Open();
//SqlDataReader dr;
SqlCommand cmd = new SqlCommand("ismovieexists", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#movie_name", objmymovie.MOVIE_NAME);
SqlParameter d = new SqlParameter("#d", SqlDbType.Int);
d.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(d);
cmd.ExecuteReader();
int i = (int)cmd.Parameters["#d"].Value;
if (i == 0)
{
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = cn;
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.CommandText = "insert_values_in_movie_master";
cmd1.Parameters.AddWithValue("#movie_name", objmymovie.MOVIE_NAME);
cmd1.Parameters.AddWithValue("#rating", objmymovie.RATING);
cmd1.Parameters.AddWithValue("#realease_year", objmymovie.REALEASE_YEAR);
cmd1.Parameters.AddWithValue("#starcast", objmymovie.STARCAST);
cmd1.Parameters.AddWithValue("#language", objmymovie.LANGUAGE);
cmd1.Parameters.AddWithValue("#display_home", objmymovie.DISPLAY_HOME);
cmd1.Parameters.AddWithValue("#block_status", objmymovie.BLOCK_STATUS);
cmd1.Parameters.AddWithValue("#no_of_copies", objmymovie.no_of_copies);
cmd1.Parameters.AddWithValue("#MOVIE_category", objmymovie.MOVIE_category);
cmd1.Parameters.AddWithValue("#MOVIE_flag", objmymovie.MOVIE_FLAG);
cmd1.ExecuteNonQuery();
return i;
}
else
return 1;
}
Does the parameter #Movie_Name exist in your Stored Procedures? If the parameter does exist it's likely that you are not passing a value to objmymovie.MOVIE_NAME

Categories

Resources