how to pass parameter to oracle function within c# environment [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I search on the net to execute oracle stored function and get the value of it
and I found something similar to this but i don't really understand it so i am not able to find out what's the error with it... please if someone can explan
whats happening after opening the connection with the database ?
public void Get_Office_Desc()
{
string oradb = "Data Source=mysource;User Id=emp;Password=00;";
var v_Office_code = Current_Office_code.Text;
string CommandStr = "F_Get_Office_Desc(:pOfficeCode)";
using (OracleConnection conn = new OracleConnection(oradb))
using (OracleCommand cmd = new OracleCommand(CommandStr, conn))
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("pOfficeCode", v_Office_code));
cmd.Parameters.Add("pOfficeDesc", OracleType.Char, 128);
cmd.Parameters["pOfficeDesc"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
var pOfficeDesc = Convert.ToString(cmd.Parameters["pOfficeDesc"].Value);
messagebox.show(pOfficeDesc);
}
}

You need to set CommandType to StoredProcedure - like that:
cmd.CommandType = CommandType.StoredProcedure;

Related

parameterised query to prevent SQL injection [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
can someone please help me re-write this line of code to parameterise the variable CMA_AAP_ID before it's passed to the GetList method?
public virtual IList<KGV_CMS_PHYSICAL_MAPPINGS> GetAssociatedPhysicalMappings()
{
return CMS_MAPPINGS.GetList(string.Format("from CMS_MAPPINGS as MAPPINGS where MAPPINGS.MAP_ID in ( select MAP_ID from FIELD_MAP_APP as FieldsAppls where FMA_APP_ID = {0} )", CMA_APP_ID));
}
You can try the following:
string sqlQuery = "from CMS_MAPPINGS as MAPPINGS where MAPPINGS.MAP_ID in ( select MAP_ID from FIELD_MAP_APP as FieldsAppls where FMA_APP_ID = #id";
then try the following:
using (var connection = new SqlConnection(/* some connection info */))
using (var command = new SqlCommand(sql, connection))
{
var idParameter = new SqlParameter("id", SqlDbType.int); // change here
idParameter.Value = 10;
command.Parameters.Add(idParameter);
var results = command.ExecuteReader();
}

ExecuteReader() Object cant be converted [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have set up a mySql database on a server, and now i want to reach it in order for me to make a webservice. Firstly i just want to test if i can grab an entity from my query in my method (OneEntity), and put it into my list.
public IEnumerable<Person> Get()
{
return new List<Person> {
new Person{ ID = 0, First = OneEntity(), Last ="Example"}
};
}
public string OneEntity()
{
MySql.Data.MySqlClient.MySqlConnection mySqlConnection;
MySql.Data.MySqlClient.MySqlCommand cmd;
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["MySql"].ToString();
mySqlConnection = new MySql.Data.MySqlClient.MySqlConnection(connString);
cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.CommandText = "SELECT 'name' FROM 'CustomerDb' WHERE 'id' = 0";
cmd.CommandType = CommandType.Text;
cmd.Connection = mySqlConnection;
mySqlConnection.Open();
SqlDataReader reader = cmd.ExecuteReader();
mySqlConnection.Close();
return reader;
}
I am not very experienced in c# and are therefore not sure if im doing it correct. However in my cmd.ExecuteReader() (Object i guess it is?!??!) i get that it
cannot implicitly convert type 'MySql.Data.MySqlDataReader' to
'System.Data.SqlClient.SqlDataReader'
What am i doing wrong here?? obviously my return is not correct either, as i specified my method to be 'string'.. but even though i type in a string, the error doesn't dissapear?
you shoud use MySqlDataReader not SqlDataReader
MySqlDataReader Reader = cmd.ExecuteReader();
code should return string not the reader in your case.
To return the first item use this return reader.GetString(0);

Search database for value and get info c# Forms [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a database called "users" where user information is saved. When you login with your name and pass, your name is send to the homeform, where i want to get the "to-do list" that's saved in the database with your info. Here's my code so far:
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT * FROM `user` WHERE `username`=#username AND `todo`=#todo";
So basically i need to get the todo of the user (which is sent to this form as _name) lets say admin, and display it in tbTodo.
if it is in the same form you can try using
select `user`.todo from `user` where `username` = #username
considering username is unique
If the main problem is parameters - u can use property Parameters:
cmd.Parameters.AddWithValue("#username", user);
There is an example in msdn:
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("#ID", SqlDbType.Int);
command.Parameters["#ID"].Value = customerID;
// Use AddWithValue to assign Demographics.
// SQL Server will implicitly convert strings into XML.
command.Parameters.AddWithValue("#demographics", demoXml);
try
{
connection.Open();
Int32 rowsAffected = command.ExecuteNonQuery();
Console.WriteLine("RowsAffected: {0}", rowsAffected);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

C# How to get DataTable value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I still new to C# and just have 3 months plus of learning process. I would like to seeking advice on how to extract data value from my DataTable I have create for validation checking.
private void checker
{
string sqlSelect2 = "SELECT a.AccNo, a.CompanyName , a.CreditLimit, FROM Debtor a JOIN";
}
which i have named TableCehecker, i do not need to put it to gridview, just for checking purposes.
In the private void process how can I extract dataTable TableCehecker and the value?
Thank you,
Brian
You need to follow the Docs from MSDN:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
DataTable dt = new DataTable();
dt.Load(reader);
sqlConnection1.Close();
You can then check whatever you want of data inside the DataTable either by querying or by looping through the rows and checking the column values.
Based on your comments, to extract values from single row:
DataRow drow = dt.Rows[0];
string value = drow.Field<string>("CompanyName");

Oracle: Bulk update of records from c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a requirement where I have to perform bulk update of records in Oracle using c#. I'm trying to achieve this by passing an array of record ids that have to be updated back to oracle,but the code is not working.
The latest code that I have used is-
List<Int64> listTransId = new List<Int64>();
while (reader.Read())
{
listTransId.Add(Convert.ToInt64(reader["TOLL_TX_SEQ_NUM"]));
}
Int64[] arrTOLL_TX_SEQ_NUM = new Int64[listTransId.Count];
arrTOLL_TX_SEQ_NUM = listTransId.ToArray();
OracleTransaction txn = connection.BeginTransaction(IsolationLevel.ReadCommitted);
OracleCommand updateCmd = new OracleCommand(#" BEGIN UPDATE TOLL_TRANSACTION SET CCH_EXPORT_DATETIME = SYSDATE WHERE TOLL_TX_SEQ_NUM = :TOLL_TX_SEQ_NUM; END;");
updateCmd.CommandType = CommandType.Text;
updateCmd.Connection = connection;
//update Cmd.BindByName = true;
update Cmd.ArrayBindCount = arrTOLL_TX_SEQ_NUM.Length;
OracleParameter TOLL_TX_SEQ_NUM = new OracleParameter("TOLL_TX_SEQ_NUM", OracleDbType.Int64);
TOLL_TX_SEQ_NUM.Direction = ParameterDirection.Input;
TOLL_TX_SEQ_NUM.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
TOLL_TX_SEQ_NUM.Value = arrTOLL_TX_SEQ_NUM;
TOLL_TX_SEQ_NUM.Size = arrTOLL_TX_SEQ_NUM.Length;
updateCmd.Parameters.Add(TOLL_TX_SEQ_NUM);
Console.WriteLine("Connection state - " + connection.State);
updateCmd.ExecuteNonQuery();
txn.Commit();
connection.Close();
use "array binding" with a simply update statement.
http://www.oracle.com/technetwork/issue-archive/2009/09-sep/o59odpnet-085168.html

Categories