UPDATE query doesn't implicate database - sql

I have a problem with a UPDATE query.
The UPDATE doesn't update the database at all...
It feels like the command.ExecuteNonQuery() doesn't happen.
Here is the code:
-- The Connection was opened before --
System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand();
command.Connection = connection;
command.CommandText = "UPDATE users SET user_name=#userName, pass_word=#pass, Uname=#Uname WHERE id=#id";
command.Parameters.AddWithValue("#id", theId);
command.Parameters.AddWithValue("#userName", userName);
command.Parameters.AddWithValue("#pass", pass);
command.Parameters.AddWithValue("#Uname", Uname);
command.ExecuteNonQuery();
Response.Redirect("~/profile.cshtml");
Thanks for any help
Etay

Related

Incorrect syntax near "SESSION"

I am having my SetCollation method written in C# and there's a sql string
Code snippet
private void SetCollation(DbConnection connection, DbTransaction? transaction)
{
const string sql1 = "ALTER SESSION SET NLS_SORT = BINARY_AI";
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = sql1;
cmd.CommandType = CommandType.Text;
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
}
const string sql2 = "ALTER SESSION SET NLS_COMP = LINGUISTIC";
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = sql2;
cmd.CommandType = CommandType.Text;
cmd.Transaction = transaction;
cmd.ExecuteNonQuery();
}
}
But unfortunately, when compiling that I get exception in cmd.ExecuteNonQuery() (before using directive) :
„Incorrect syntax near 'SESSION'.”
I am trying to switch from Oracle to SQL Server
How do I fix that?

"Incorrect syntax near ')'."

I have a program that is to take a couple inputs and do stuff to them then store them in a local database (using SQL Server).
I have the following code for the connection:
SqlConnection con = new SqlConnection();
con.ConnectionString = ("Data Source=DESKTOP-PGHMM6M;Initial Catalog=LocalUsers;Integrated Security=True");
con.Open();
String st = "INSERT INTO data(Username,Password, Hash, EncryptedPassword)";
SqlCommand cmd = new SqlCommand(st, con);
cmd.Parameters.AddWithValue("#Username", Username);
cmd.Parameters.AddWithValue("#Password", textBox2.Text);
cmd.Parameters.AddWithValue("#Hash", savedPasswordHash);
cmd.Parameters.AddWithValue("#EncryptedPassword", FinalEncryptedPass);
cmd.ExecuteNonQuery();
con.Close();
It fails at the cmd.ExecuteNonQuery(); line, and throws this exception :
Incorrect syntax near ')'
I'm not even really sure where to start, as I haven't done any of this since college (107 years ago). Can anybody help me get started? I've been scouring but nothing seems to be working.
Maybe your insert should look like this:
INSERT INTO data (Username, Password, Hash, EncryptedPassword)
VALUES (#Username, #Password, #Hash, #EncryptedPassword)
The VALUES clause will need to be within the insert statement string. I would also recommend a using block instead of directly opening the SqlConnection, as will automatically close the connection upon exit whether by completion or error. While technically there isn't a difference between using String and string as in your command text, String would most often be used to reference a class while string is typically used in object references such as the case here.
string connectionString = #"YourConnectionString";
string st = "INSERT INTO data(Username,Password, Hash, EncryptedPassword) VALUES (#Username, #Password, #Hash, #EncryptedPassword)";
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(st1, con);
cmd.Parameters.AddWithValue("#Username", Username);
cmd.Parameters.AddWithValue("#Password", textBox2.Text);
cmd.Parameters.AddWithValue("#Hash", savedPasswordHash);
cmd.Parameters.AddWithValue("#EncryptedPassword", FinalEncryptedPass);
con.Open();
cmd.ExecuteNonQuery();
}

Oracle parameterized update query c#

It should work but it doesn't.
I have referred others but couldn't find the reason.
OracleCommand cmd = con.CreateCommand();
var query = $#"UPDATE Customer SET ContactName = :ct WHERE CustomerID = :id";
cmd.CommandText = query;
cmd.Parameters.Clear();
cmd.Parameters.Add(new OracleParameter(":id", OracleDbType.Varchar2, "bbb1", System.Data.ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter(":ct", OracleDbType.Varchar2, "Joon", System.Data.ParameterDirection.Input));
var rst = cmd.ExecuteNonQuery();
Thanks in advance.
Joon
I found why it didn't update table.
To make it work I added parameters in the order of the query parameter and found it works. But I still do not understand why the order of adding parameters is so important to make it work.But the thing clear is that it is working when I make it like this:
OracleCommand cmd = con.CreateCommand();
var query = $#"UPDATE Customer SET ContactName = :ct WHERE CustomerID = :id";
cmd.CommandText = query;
cmd.Parameters.Clear();
cmd.Parameters.Add(new OracleParameter(":ct", OracleDbType.Varchar2, "Joon", System.Data.ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter(":id", OracleDbType.Varchar2, "bbb1", System.Data.ParameterDirection.Input));
var rst = cmd.ExecuteNonQuery();
Thanks everybody who paid attention on it.
Joon
In order to avoid the order declaration, you can use BindByName:
OracleCommand cmd = con.CreateCommand();
cmd.BindByName = true; // Just add this
...

How efficient is it to reuse the command and connection object in ADO.net?

There is a specific requirement where in I am required to connect to two different Oracle databases one after the other. Does it make sense to create new connection and command objects or should I reuse them like below? (Not sure if this would even work-just some pseudo code here)
OracleConnection conn = new OracleConnection(ConnectionString1);
OracleCommand cmd = new OracleCommand("StoredProcedure1 , conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.parameters.add("param1", OracleDbType.Varchar2 , 20 , ParameterDirection.Input);
conn.open();
cmd.ExecuteNonQuery();
conn.close();
// Second DB hit
conn.ConnectionString = ConnectionString2;
cmd.CommandText = "StoredProcedure1";
if (cmd.Parameters.Count > 0) cmd.Parameters.Clear();
cmd.parameters.add("param2", OracleDbType.Varchar2 , 30 , ParameterDirection.Input);
conn.open();
cmd.ExecuteNonQuery();
conn.close();
All criticism welcome.

Stored procedure in loop

for (int i = 0; i < purchaseListView.Items.Count; i++)
Connection con = new Connection();
SqlCommand cmd = new SqlCommand();
SqlCommand cmdFifo = new SqlCommand();
con.OpenConnection();
cmd.Connection = con.DataBaseConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertDetail";
cmdFifo.Connection = con.DataBaseConnection;
cmdFifo.CommandType = CommandType.StoredProcedure;
cmdFifo.CommandText = "insertInToMain";
This is my code and I want to know if the loop affects performance of my software and if this is the right way to call stored procedure in loop.
I have stored the procedure in a class and I want to call it from a form when the save button is clicked and insert 10 items in the database via same stored procedure.
Well, you are opening 10 connections, and it seems that you are not closing them, so you may run out of connections, but im guessing that's not the whole code, could you post the entire for ?
I would suggest you to create a table and insert the whole data into it by iterating through all the inputs ie., try to create a single stored procedure.
Running a for loop multpile times is inefficient and also the database will generate the result set number of times which will affect your performance as well due to network overhead.
You are creating a new connection object for every item which is highly inefficient.
Create one connection object and execute the stored procedure n times with the product details. Alternatively create a stored procedure to accept 10 items and insert the data at that level.
Move this outside the loop (You can access con and cmd inside the loop without creating a new instance:
Connection con = new Connection();
SqlCommand cmd = new SqlCommand();
con.OpenConnection();
cmd.Connection = con.DataBaseConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertDetail";
Inside your loop, you can add all the parameters to the cmd object.
cmd.Parameters.Add("#Item", SqlDbType.Int);
cmd.Parameters["#Item"].Value = purchaseListView.Items[i];
cmd.ExecuteNonQuery();
You should set up your connection and stored procedure before the loop and only update command parameter values and exec within the loop. Like this:
Connection con = new Connection();
SqlCommand cmd = new SqlCommand();
SqlCommand cmdFifo = new SqlCommand();
con.OpenConnection();
cmd.Connection = con.DataBaseConnection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertDetail";
cmd.Parameters.Add("#Item", SqlDbType.Int);
cmdFifo.Connection = con.DataBaseConnection;
cmdFifo.CommandType = CommandType.StoredProcedure;
cmdFifo.CommandText = "insertInToMain";
//now that your connections & commands are set up, you can reuse them within the loop
for (int i = 0; i < purchaseListView.Items.Count; i++)
{
//ToDo:assign any SP parameter values
cmd.Parameters["#Item"].Value = purchaseListView.Items[i];
// ...
//then exec within the loop
cmd.ExecuteNonQuery();
cmdFifo.ExecuteNonQuery();
}