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"), ....
Related
here's my code
int indextest = 0;
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "Select backup,Client,No_Projet,Description from historique Where id= #dataID and index= #indexID";
cmd.Parameters.AddWithValue("#dataID", soumissionId.Text);
cmd.Parameters.AddWithValue("#indexID", indextest);
cmd.Connection = connection;
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
Cant figure how format my string form having the and working.
I'm always getting error near index= 0
Can anyone help me please
My SQL uses ` characters to enclose names, you can use them in your query:
cmd.CommandText = "Select `backup`,`Client`,`No_Projet`,`Description` from `historique` Where `id` = #dataID and `index` = #indexID";
Simple Question:
My code looks like this:
var con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.20.10.8)(PORT=1521))(CONNECT_DATA=(SID=orcl12c)));");
con.Open();
var adp = new OracleDataAdapter("select * from adr;select * from person;", con);
var ds = new DataSet();
adp.Fill(ds);
Now I would expect to get two tables in the DataSet, but I rather get an exception telling me that the SQL Syntax is not correct... It seems the ; is not recognized that way..? Any Ideas?
Edit #1: Also Adding BEGIN+END; does not work (multiple variations)
Edit #2: Wrapping the selects with execute immediate will run, but won't return a result set.
Solution: Combine the provided answer with Using Dapper with Oracle stored procedures which return cursors and enjoy.
You should write an anonymous pl/sql block that returns ref cursors.
Try this in ADO.NET:
oraConnection = new OracleConnection();
da = new OracleDataAdapter();
ds = new DataSet();
oraConnection.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.20.10.8)(PORT=1521))(CONNECT_DATA=(SID=orcl12c)));";
cmdText = "begin open :1 for select * from adr; open :2 for select * from person; end;";
cmd = new OracleCommand();
cmd.CommandText = cmdText;
cmd.Connection = oraConnection;
cmd.CommandType = CommandType.Text;
OracleParameter refcur1 = cmd.Parameters.Add("Refcur", OracleDbType.RefCursor);
refcur1.Direction = ParameterDirection.Output;
OracleParameter refcur2 = cmd.Parameters.Add("Refcur", OracleDbType.RefCursor);
refcur2.Direction = ParameterDirection.Output;
da.SelectCommand = cmd;
da.Fill(ds);
I have a big problem with save variable from select in mysql.
I wrote the following code:
string connectionstring = #"****;userid=*****;
password=***;database=***";
cnn = new MySqlConnection(connectionstring);
cnn.Open();
MySqlDataReader reader = null;
string query_date = "SELECT computer_name from wp_users where user_login = #login";
MySqlCommand command2 = new MySqlCommand(query_date, cnn);
command2.Parameters.AddWithValue("#login", metroTextBox.Text);
reader = command2.ExecuteReader();
while (reader.Read())
{
string ColumnName = (string)reader["computer_name"];
}
cnn.Close();
I tried a lot of commands like ExecuteReader , ExecuteNonQuery , ExecuteScalar. but none of them worked and I am getting the same error:
Really don't know what is wrong here I searched a lot and didn't find a solution of any form. Please help.
EDIT 1
I just did how you wrote and i did this :
string connectionstring = #"****;userid=*****;
password=***;database=***";
cnn = new MySqlConnection(connectionstring);
cnn.Open();
MySqlDataReader reader = null;
string query_date = "SELECT computer_name from wp_users where user_login = #login";
MySqlCommand command2 = new MySqlCommand(query_date, cnn);
command2.Parameters.AddWithValue("#login", metroTextBox.Text);
DataTable table = new DataTable("ResultTable");
MySqlDataAdapter adapter = new MySqlDataAdapter(command2);
adapter.Fill(table);
// This is the important line
string result = table.Rows[0].ToString();
cnn.Close();
Its the same error as earlier but another place. What's going on here... just don't know.
Additional information mean in english : The key is not present in the dictionary
EDIT 2
The funniest is when i just try to update with code :
string connectionstring = #"****;userid=*****;
password=***;database=***";
cnn = new MySqlConnection(connectionstring);
cnn.Open();
MySqlDataReader reader = null;
string upd = "UPDATE w_users Set computer_name = CURRENT_DATE where user_login = #login";
MySqlCommand command2 = new MySqlCommand(upd, cnn);
command2.Parameters.AddWithValue("#login", metroTextBox.Text);
DataTable table = new DataTable("ResultTable");
SqlDataAdapter adapter = new MySqlDataAdapter(command2);
adapter.Fill(table);
cnn.Close();
And this works fine without any errors just update my table... What's the point of it
EDIT 3
I jutr try used to ExecuteScalar() and still i have the same error :
The solution is simple:
Updating mysql.data.dll to the newest version fixed it (https://dev.mysql.com/downloads/connector/net/6.9.html).
This exception wants to tell you, that there is no matching key in your resultset. Keys are case sensitive. Have you tried to spell your column name with all uppercase letters: (string)reader["COMPUTER_NAME"]?
Databases tend to return the column names in uppercase, even though you selected the column name in lower case.
If you have the MySqlDataAdapter, try if the following code works for you:
string connectionstring = #"****;userid=*****;
password=***;database=***";
cnn = new MySqlConnection(connectionstring);
cnn.Open();
string query_date = "SELECT computer_name AS `computer_name` FROM wp_users WHERE user_login = #login";
MySqlCommand command2 = new MySqlCommand(query_date, cnn);
command2.Parameters.AddWithValue("#login", metroTextBox.Text);
DataTable table = new DataTable("ResultTable");
MySqlDataAdapter adapter = new MySqlDataAdapter(command2);
adapter.Fill(table);
// This is the important line
string result = table["computer_name"];
cnn.Close();
I'm trying to get values out of my database into my Listbox, I currently send all my results into a new object called Results
I want my listbox to show something like this:
Title(1)(enter)
Url(1)(enter)
Title(2)(enter)
Url(2)(enter)
and so on
It currently still gives an error at OleDbDataReader reader = command.ExecuteReader(); but I have no idea why.
This is the exact code
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\martijn\Dropbox\Proftaak Periode 2 Identity\Database11.accdb;
Persist Security Info=False;";
connection.Open();
OleDbCommand cmd1 = new OleDbCommand();
cmd1.Connection = connection;
cmd1.CommandText = "SELECT ZoekcriteriaID from Zoekcriteria WHERE ZoekCriteria = '" + Convert.ToString(lbzoektermen.SelectedItem) + "';";
OleDbDataReader reader1 = cmd1.ExecuteReader();
if(reader1.Read())
{
resultaatid = Convert.ToInt32(reader1["ZoekcriteriaID"]);
}
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT Titel, Webadress from Resultaat WHERE ZoekriteriaID = '"+ resultaatid +"';";
OleDbDataReader reader = command.ExecuteReader();
lbresultaten.Items.Clear();
List<Results> resultaten = new List<Results>();
while(reader.Read())
{
Results result = new Results();
result.url = Convert.ToString(reader["Webadress"]);
result.titel = Convert.ToString(reader["Webadress"]);
resultaten.Add(result);
}
foreach(Results result in resultaten )
{
lbresultaten.Items.Add(result.titel);
lbresultaten.Items.Add(result.url);
}
I hope someone could help me,
Kind Regards,
Martijn
Your problem probably lays in your where clause:
SELECT ZoekcriteriaID from Zoekcriteria WHERE **ZoekCriteria**
It should be a column name, not a table name.
I'm trying to prevent SQL injections. Am I doing this right? (I'm using MS Access.) Should I still use sqlparameter?
OleDbParameter[] myparm = new OleDbParameter[2];
myparm[0] = new OleDbParameter("#UserID", UserName.Text);
myparm[1] = new OleDbParameter("#Password", encode);
string queryStr = "SELECT * FROM TMUser WHERE UserID=#UserID AND Password=#Password";
OleDbConnection conn = new OleDbConnection(_connStr);
OleDbCommand cmd = new OleDbCommand(queryStr, conn);
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
Close!
string queryStr = "SELECT * FROM TMUser WHERE UserID=#UserID AND Password=#Password";
OleDbConnection conn = new OleDbConnection(_connStr);
OleDbCommand cmd = new OleDbCommand(queryStr, conn);
cmd.Parameters.AddWithValue("#UserID", UserName.Text);
cmd.Parameters.AddWithValue("#Password", encode);
The parameters are part of the command object and you use the Parameters.AddWithValue method to set the parameter values to what you have defined in the query string.
By the way, you should be using using statements to encapsulate some of your objects, here is what I typically do:
using (OleDbConnection conn = new OleDbConnection(_connStr))
using (OleDbCommand = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT ...";
cmd.Parameters.AddWithValue(...);
cmd.ExecuteReader();
//...
}
That way you don't have to worry about cleaning up resources if something goes wrong inside or closing the connection when you are done.