MySQL truncate command with parameter not working - c#

Why do I get an exception when trying to truncate a MySQL table (using MySQL Connector/Net)? I am trying to give the table name with a parameter.
This is the code I'm executing:
var connectionString = "Server="+_server+";Uid="+_user+";Pwd="+_password+";Database="+_database+";";
try
{
using (var conn = new MySqlConnection(connectionString))
{
conn.Open();
const string sql = "TRUNCATE TABLE #tablename"; // also tried with TRUNCATE #tablename
var cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("#tablename", "test");
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (MySqlException ex)
{
Console.WriteLine(ex.ToString());
}
And this is the execption:
MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error
in your SQ L syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near ''test'' at line 1
When I try a select query, for example, then I don't have any problems. This runs fine and returns correct data:
conn.Open();
const string sql = "SELECT body FROM test WHERE id=#pid";
var cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("#pid", 1);
cmd.ExecuteScalar();
conn.Close();

Parameters are used for query values, not object names like tables.
So this will not work for sure.
You need to set the table name in the command string by using string concatenation. You can avoid sql injection attacks by manually checking for weird characters in the table name (spaces, dashes, semicolons, etc..)

I've been playing around with this for a while now, and i can't seem to get it to work either. I can't find any documentation online, so i'm starting to think you may not be able to truncate with a parameter like you've tried.
However, is there really a need to prevent SQL injection on this command? Does the user enter the name of the table they want to truncate, and if so, they're just going to truncate a table which...is essentially what the command does anyway?

Related

Syntax error trying to ALTER TABLE using variable

I am trying to add a section that can add extra columns to a table so I am trying a parametrised ALTER TABLE query. However when I run it I am getting an error :
SQL logic error near "#TableName":syntax error
Code:
public void addNewField(string tableName, string newFieldName, string dataType)
{
SQLiteConnection dbConnection = new SQLiteConnection(connectionStrings.getSQLiteDBConnectString());
string sql = #"ALTER TABLE #TableName ADD #NewFieldName #dataType";
dbConnection.Open();
SQLiteCommand cmd = new SQLiteCommand(sql, dbConnection);
cmd.Parameters.AddWithValue("#TableName", tableName);
cmd.Parameters.AddWithValue("#NewFieldName", newFieldName);
cmd.Parameters.AddWithValue("#dataType", dataType);
try
{
cmd.ExecuteNonQuery();
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
dbConnection.Close();
}
Why am I getting the error? I tried different field names and have run the SQL string (with # variables replaced) in a part of my application that allows to run free typed queries and the base query worked.
With SQLite, the only kind of parameterization possible is parameterized values in DQL/DML statements. It is not possible to provide any database object identifier names as query parameters to any kind of statements.
Parameterization is intended to separate user-supplied values from the SQL code. And the user should not be provided with ability to change the schema. Otherwise, RDBMS may not be a good fit for your application.

SqlException : Incorrect syntax near '1'

I am currently trying to implement SQL into a project with Unity3D. So far, I was able to do "normal" UPDATE, ADD, DELETE, DROP, ALTER, INSERT".
Trying to go a step further, I am trying to insert prepared statements, using this link as a guide
Here is my code :
SqlConnection sqlConnection = new SqlConnection(Connection.connectionString)
sqlConnection.Open();
SqlCommand cmd = new SqlCommand(null, sqlConnection);
cmd.CommandText = "INSERT INTO IngredientTypes (Name) VALUES (#name)";
SqlParameter nameParam = new SqlParameter("#name", SqlDbType.Text, 155);
nameParam.Value = Name;
cmd.Parameters.Add(nameParam);
cmd.Prepare();
cmd.ExecuteNonQuery();
My table looks like so :
CREATE TABLE IngredientTypes
(
IngredientTypeID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(155)
);
I get this error :
SQLException : Incorrect systax near '1'.
System.Data.SqlClient.SqlConnection.ErrorHandler (System.Object sender, Mono.Data.Tds. Protocol.TdsInternalErrorMessageEventArgs e)
Help please? Thank you in advance.. I can't find where I did wrong.
You can reduce that code quite a bit with no loss of function, and even some important improvements (for example, this will close the connection even if an exception is thrown):
using (var sqlConnection = new SqlConnection(Connection.connectionString))
using (var cmd = new SqlCommand("INSERT INTO IngredientTypes (Name) VALUES (#name)", sqlConnection))
{
cmd.Parameters.Add("#name", SqlDbType.VarChar, 155).Value = Name;
sqlConnection.Open();
cmd.ExecuteNonQuery();
}
I'm not sure what's causing that exception in your existing code, though, because 1 is not used anywhere in that query. I suspect the problem has something to do with SqlDbType.Text, since that is not the correct type to use with a VarChar column, but it seems just as likely there's code somewhere we haven't seen yet that's changing your SQL command text.
Definitely the Prepare() method in your link is not needed for Sql Server. It's inherited here from DbCommand, where it's included because it's an important part of the API for some other databases, but Sql Server has handled this automatically for more than 10 years now.
SqlDbType.Text Is not the same as varchar. I don’t believe Text types have a length you specify.
Could you try below? Using the "using" structure is safer for sql connections by the way, the connection automatically closes when your process is done.
using (SqlConnection sqlConnection = new SqlConnection(Connection.connectionString))
{
SqlCommand command = new SqlCommand("INSERT INTO IngredientTypes (Name) VALUES (#name)", connection);
command.Parameters.Add("#name", SqlDbType.Varchar, 155);
command.Parameters["#name"].Value = Name; //make sure Name is string.
try
{
sqlConnection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
I tried your code exactly as it is and found no issue. Though there are few compilation errors (missing ; in line 1 and Name variable should be coming as parameter) but I am sure you know that. If you have posted your table structure and code exactly the same as you have in your project, then there is no problem in this code.

Column name having # symbol

I am executing a dynamically generated MySQL query using C# code. An exception gets thrown:
CREATE TABLE dump ("#employee_OID" VARCHAR(50));
"{"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '\"#employee_OID\" VARCHAR(50))' at line 1"}"
I cannot skip the '#' symbol from the Column Name.
Manually, using MySQL Workbench, I can get it executed after executing the following query.
SET sql_mode='ANSI_QUOTES';
Following is the code I am able to produce:
MySqlConnection conn = null;
MySqlCommand cmd = null;
string mySQLConnectionString = ConfigurationManager.AppSettings["MySQLAutomationServerConnectionString"];
//Dynamically getting generated using some other code
string sqlQueryString = "CREATE TABLE dump ("#employee_OID" VARCHAR(50));";
try
{
conn = new MySqlConnection(mySQLConnectionString);
conn.Open();
cmd = new MySqlCommand(sqlQueryString, conn);
executionResultStatus = cmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
Console.WriteLine("MySQL Exception: " + ex.Message.ToString());
}
I have tried to execute the "SET sql_mode='ANSI_QUOTES';" query before this gets executed using the code right before executing this code, its not working. What shall I do?
Change your command to
string sqlQueryString = "CREATE TABLE dump (`#employee_OID` VARCHAR(50));";
Notice how there are two backticks before and after the problematic name (ALT+096)
This will allow your engine to ignore the character # used to identify parameters

Creating Registration form in c# with mysql

Hello so i m creating a registration form in C# with MySql so it connects to the database and everything but i get this error Napaka pri registraciji Unknown column " in 'field list' the translation of Napaka pri registraciji means Error at registering i just have it in my language. I get this error when i insert data in textboxes and press Register..
the code:
private void btn_Reg_Click(object sender, EventArgs e)
{
MySqlConnection dataConnection = new MySqlConnection();
dataConnection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
dataConnection.Open();
MySqlTransaction transakcija = dataConnection.BeginTransaction();
MySqlCommand dataCommand = new MySqlCommand();
dataCommand.Connection = dataConnection;
dataCommand.Transaction = transakcija;
try
{
dataCommand.CommandText = "Insert INTO lr.users (upIme,geslo) VALUES (`"+this.tB_upIme.Text+"`,`"+this.tB_geslo.Text+"`)";
dataCommand.CommandType = CommandType.Text;
dataCommand.ExecuteNonQuery();
transakcija.Commit();
MessageBox.Show("Registracija uspešna!");
}
catch (Exception eks)
{
transakcija.Rollback();
MessageBox.Show("Napaka pri registraciji\n" + eks.Message);
}
finally
{
dataCommand.Connection.Close();
}
}
There are two things I immediately see wrong here...
First, you're using back ticks to wrap your values. In MySQL Back ticks represent database objects, so the query is looking for objects named by those values instead of using the values themselves. So instead of this:
`"+this.tB_upIme.Text+"`
You'd want this:
'"+this.tB_upIme.Text+"'
Second, and vastly more importantly, your code is wide open to SQL injection attacks. You'll want to use query parameters, not direct string concatenation. While it may look like you're just putting values into the query string, you're actually taking user input and treating it as executable code in your query string, which means users can run any arbitrary code they want on your database.
First, add parameters to your query:
"Insert INTO lr.users (upIme,geslo) VALUES (#upIme, #geslo)"
(You'll notice this also makes the query a heck of a lot cleaner and easier to read.) Then add your parameters to the command:
dataCommand.Parameters.AddWithValue("#upIme", this.tB_upIme.Text);
dataCommand.Parameters.AddWithValue("#geslo", this.tB_geslo.Text);
Then when you execute that command it will treat the user-input values as values instead of as executable code.
Change to single quotes ' in the values.
dataCommand.CommandText =
"Insert INTO lr.users (upIme,geslo)
VALUES ('"+this.tB_upIme.Text+"','"+this.tB_geslo.Text+"');";

C# SQL statement - Syntax error in query. Incomplete query clause

Below is the code I have, I can't for the life of me work out what is wrong with the query.
I originally had the error "no value given for 1 or more parameters", which seems to have gone away (although again I don't even know why I was getting it).
The connection is opened prior to this code.
The parameter GVars.thisFY is a string = "FY13" - this table definitely exists.
The parameter GVars.currentDate is a DateTime = today.
Records definitely exist for this [Destination] and [Next Collection] range:
string sql;
OleDbDataAdapter adapter;
sql = "SELECT * FROM #CurFY WHERE [Destination] = #Destination AND [Next Collection] BETWEEN #NextCollectionA AND #NextCollectionB;";
// Create the command object
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
// Add values to the fields
cmd.Parameters.AddWithValue("#CurFY", GVars.thisFY);
cmd.Parameters.AddWithValue("#Destination", "Henwood");
cmd.Parameters.AddWithValue("#NextCollectionA", GVars.currentDate);
cmd.Parameters.AddWithValue("#NextCollectionB", GVars.currentDate.AddDays(1));
adapter = new OleDbDataAdapter(cmd.CommandText, conn);
try
{
adapter.Fill(ds);
GVars.bLblLastUpdate = DateTime.Now.ToString("HH:mm:ss");
}
catch (Exception ex)
{
}
EDIT:
I have changed the code to remove the table parameter as below, still getting the "no value given for 1 or more parameters" though which I can't pin down..
EDIT2: I removed the extra stuff so the post relates only to the original question, which has been answered. I will make a new question for my strange "no value given" error
You cannot parameterize queries with names of tables, views, or columns. Only data members can be parameterized.
You need to make your SQL dynamically, e.g. like this:
sql = string.Format(
"SELECT * FROM {0} WHERE [Destination] = #Destination AND [Next Collection] BETWEEN #NextCollectionA AND #NextCollectionB;"
, GVars.thisFY
);
This should be done only if GVars.thisFY is controlled by your code, e.g. comes from a pre-defined list or checked for absence of non-alphanumeric characters to avoid SQL injection attacks.
Try this one -
sql = Sring.Format(
"SELECT * FROM {0} WHERE [Destination] = #Destination AND [Next Collection] BETWEEN #NextCollectionA AND #NextCollectionB;",
GVars.thisFY
)
cmd.Parameters.AddWithValue("#Destination", "Henwood");
cmd.Parameters.AddWithValue("#NextCollectionA", GVars.currentDate);
cmd.Parameters.AddWithValue("#NextCollectionB", GVars.currentDate.AddDays(1));

Categories