First I tested simple cases:
cmd.Parameters.Add(new OracleParameter(":paramCode", OracleDbType.NVarchar2)).Value = userCode;
cmd.CommandText = "SELECT * FROM VIEWUSERDATA WHERE Codigo = :paramCode";
cmd.CommandType = System.Data.CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//Works, retrieve data
}
Another:
cmd.Parameters.Add(new OracleParameter(":paramRole", OracleDbType.NVarchar2)).Value = userRole;
cmd.CommandText = "SELECT * FROM VIEWUSERDATA WHERE Role = :paramRole";
...
while (reader.Read())
{
//Also works
}
But when join, doesn't work.
cmd.Parameters.Add(new OracleParameter(":paramCode", OracleDbType.NVarchar2)).Value = userCode;
cmd.Parameters.Add(new OracleParameter(":paramRole", OracleDbType.NVarchar2)).Value = userRole;
cmd.CommandText = "SELECT * FROM VIEWUSERDATA WHERE Role = :paramRole AND Code = :paramCode";
...
while (reader.Read())//don't retrieve anything
{
}
Data exists, If I do the query in an external query editor(window) works fine.
Thanks.
By default, Oracle parameter binding is by position, not by name. Either swap the two cmd.Parameters.Add calls or set cmd.BindByName = true; before executing the query.
Related
I am using the following code that was provided in the npgsql docs. I can retrieve data when my query string is Select * from public.accounts but this function will not return data when conditions are in the where clause.
I have seen other answers on SO that say to pass the command parameters in the AddWithValue function, but this doesn't return anything for me.
The query Select * from public.accounts where ext_auth0_user_id = 'github|42357689' DOES return data when I run it directly in pgAdmin, so I am assuming I have some formatting wrong.
var userId = "github|42357689";
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand("Select * from public.accounts where ext_auth0_user_id = #userId", conn);
// Retrieve all rows
using (cmd)
{
cmd.Parameters.AddWithValue("#userId", userId);
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
dataTable.Load(reader);
}
}
}
}
NpgsqlParameter paramUserId = cmd.Parameters.Add("userId", NpgsqlTypes.NpgsqlDbType.Varchar, 20);
paramUserId.Direction = ParameterDirection.Input;
paramUserId.value = userId;
NpgsqlParameter paramAnotherId = cmd.Parameters.Add("anotherId", NpgsqlTypes.NpgsqlDbType.Integer);
paramAnotherId.Direction = ParameterDirection.Input;
paramAnotherId.value = anotherId;
Assign entered texbox value to Sql query |C#|Oracle
How do i assign the Textbox value to my SQL Query C#
In the Above Image i typed Tablename = PERSONDETAILS this entered value in textBox1.Text should be passed to my SQL Query as a Paramter.So Hard coding the table name in the SQl query will be reduced
Actual code where i want it to be Included
select dbms_metadata.get_ddl('TABLE',table_name,user) AS my_ddl from all_tables where owner ='HR' AND table_name ='PERSONDETAILS'"
code :
OracleConnection con = new OracleConnection();
con.ConnectionString = conString;
con.Open();
OracleCommand cmd = con.CreateCommand();
cmd.Parameters.Add("userid", OracleDbType.Varchar2, 20).Value = textBox1.Text.ToString();
cmd.Parameters.Add("tableName", OracleDbType.Varchar2, 20).Value = texttablename.Text.ToString();
//cmd.CommandText = "select * from all_all_tables WHERE table_name=:tableName";
cmd.CommandText ="select dbms_metadata.get_ddl('TABLE',table_name,user) AS my_ddl from all_tables where owner = :userid AND table_name = :tableName";
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
textBox6.Text = reader.GetString(0);
}
con.Close();
You need to pass value of a textbox as a parameter to your command through Parameters property of OracleCommand.
So all you need to include in your code is:
cmd.CommandText = "select * from all_all_tables WHERE table_name=:tableName";
cmd.Parameters.Add("tableName", OracleType.VarChar, 20).Value = textBox1.Text;
Update regarding comments:
cmd.CommandText ="select dbms_metadata.get_ddl('TABLE',table_name,user) AS my_ddl from all_tables where owner = :userid AND table_name = :tableName";
// here you might want to use appropiate datatype
cmd.Parameters.Add("userid", OracleDbType.Varchar2, 20).Value = textBox1.Text;
cmd.Parameters.Add("tableName", OracleDbType.Varchar2, 20).Value = texttablename.Text;
For the past few days I cannot pass any fixed parameters to my SQL query.
I try all possible tutorials to pass the parameter to a query, but nothing works.
However, I have seen by putting fixed parameters directly in the query (method 1) it worked perfectly.
I do not see that in method 2 prevents the functioning of my function.
When I say that it does not work is that in the first method my reader is filled while in the method 2 my reader is empty
method 1 : works (i don't need this kind of function)
public void VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select nom_projet from analyses where nom_projet='demonstration'";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
if (dr.Read())
Console.WriteLine("Data already exist");
else
Console.WriteLine("Data doesn't exist");
}
method 2 : doesn't works (I need this kind of function)
public void VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select nom_projet from analyses where nom_projet=:test";
cmd.Parameters.Add(new OracleParameter("test", "demonstration"));
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
if (dr.Read())
Console.WriteLine("Data already exist");
else
Console.WriteLine("Data doesn't exist");
}
String updateCmd;
SqlCommand myCommandUpd;
updateCmd = "UPDATE RHRMVacationRequest SET [EmplId] = #Emplid WHERE RecId = #RecId";
myCommandUpd = new SqlCommand(updateCmd, DataBase.GetConnetionToBase());
myCommandUpd.Parameters.Add(new SqlParameter("#Emplid", SqlDbType.VarChar, 10));
myCommandUpd.Parameters.Add(new SqlParameter("#RecId", SqlDbType.BigInt));
myCommandUpd.Parameters["#Emplid"].Value = emplIdUpd.Text.Trim();
myCommandUpd.Parameters["#RecId"].Value = Convert.ToInt64(RecIdUpd.Value.Trim());
myCommandUpd.Connection.Open();
myCommandUpd.ExecuteNonQuery();
may be i recall the wrong way round - maybe i tried :? and it did not work and used :test - but i think i got you issue, check the code below.
public void VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select nom_projet from analyses where nom_projet=:test";
cmd.Parameters.Add(new OracleParameter(":test", "demonstration"));
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
if (dr.Read())
Console.WriteLine("Data already exist");
else
Console.WriteLine("Data doesn't exist");
}
After many hours of research, i have I finally found the solution:
public Boolean VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
string str = "demonstration";
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.BindByName = true;
cmd.CommandText = "select * from analyses where nom_projet='"+str+"'";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Console.WriteLine("Data already exist");
return true;
}
Console.WriteLine("Data doesn't already exist");
return true;
}
I am trying to use MYSQL select query with c#.
Following query for searching "ID" is working fine:
conn = new MySqlConnection(cs);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from catalog_product_entity where entity_id = ?Id";
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
cmd.Parameters.Add("?Id", SqlDbType.Text).Value = ProductList[i].ProductId.ToString();
adp.Fill(MagentoProduct);
Now, I want to search exact string value in table. I am using following code and its giving empty result:
My Code:
conn = new MySqlConnection(cs);
conn.Open();
cmd = new MySqlCommand("select * from catalog_category_entity_varchar where value = #Value;", conn);
cmd.Parameters.AddWithValue("#Value", "Storybooks");
MySqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
log.WriteEntry(r.GetString("value"));
}
This is the problem:
where value = '?cname'
That's specifying ?cname as the literal value you're searching for - when you actually just want the parameter. Remove the quotes and it should be fine:
where value = ?cname
(You should use using statements for the connection and command, mind you...)
You could try SQL Reader
c = new MySqlCommand("select * from catalog_product_entity where column_nam = #Value;", conn);
c.Parameters.AddWithValue("#Value", your string);
MySqlDataReader r = c.ExecuteReader();
and then use Reader methods like reader.GetString("column_name"), ....
I'm having an issue with an update query in C#. It's odd to me that I'm having an issue with update, but select, delete, and insert work.
public void updateTeacherInfo(string SSN, string Classroom, string salary, string tenured, string phone)
{
OracleConnection conn = new OracleConnection("myconnectionstring;");
conn.Open();
OracleCommand cmd = new OracleCommand("Update Teachers Set classroom_number = :TRM, Salary = :TSALARY, Tenured = :TTENURE, Phone_numer = :TPHONE Where SSN = :TSSN", conn);
OracleCommand commit = new OracleCommand("COMMIT", conn);
cmd.Parameters.Add(new OracleParameter(":TSSN", SSN));
cmd.Parameters.Add(new OracleParameter(":TRM", Classroom));
cmd.Parameters.Add(new OracleParameter(":TSALARY", salary));
cmd.Parameters.Add(new OracleParameter(":TTENURE", tenured));
cmd.Parameters.Add(new OracleParameter(":TPHONE", phone));
int i = cmd.ExecuteNonQuery();
int k = commit.ExecuteNonQuery();
MessageBox.Show(i + " rows affected");
MessageBox.Show(k + " rows affected");
conn.Close();
}
Edit* the rest of the method to clear things up, and also, it is not throwing any errors, but does not update the database.
Put the Parameters.Add in proper sequence. In your update query
"Update Teachers Set classroom_number = :TRM, Salary = :TSALARY, Tenured = :TTENURE, Phone_numer = :TPHONE Where SSN = :TSSN"
:TRM is occuring first and likewise. So keep the Parameters.Add also in same sequence. It will work. The sequence will be:
cmd.Parameters.Add(new OracleParameter(":TRM", Classroom));
cmd.Parameters.Add(new OracleParameter(":TSALARY", salary));
cmd.Parameters.Add(new OracleParameter(":TTENURE", tenured));
cmd.Parameters.Add(new OracleParameter(":TPHONE", phone));
cmd.Parameters.Add(new OracleParameter(":TSSN", SSN));