My code is as below:
if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN ('" + Mpdue.TheObjectPropertyNameNs + "'); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["dOCode"].ToString() + "<br />");
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
Output:
D410
D420
D430
D440
Now I tried use command.Parameters.AddWithValue
in MySql
query.
I have been trying and cannot find error, the output when use command.Parameters.AddWithValue
is empty, why ?
if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN (?); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
command.Parameters.AddWithValue("param1", Mpdue.TheObjectPropertyNameNs);
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["dOCode"].ToString() + "<br />");
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
Edit #2
var parameters = new string[Mpdue.TheObjectPropertyNameNs.Length];
var paramValue = string.Join(", ", parameters);
string sql = string.Format("SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters.Select(x => "?")));
int index = 0;
foreach (var param in parameters)
{
command.Parameters.AddWithValue("param{index}", param);
index++;
}
Edit #1
My new code is as below:
if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
var parameters = new string[Mpdue.TheObjectPropertyNameNs.Length];
string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters));
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
command.Connection.Open();
for (int i = 0; i < Mpdue.TheObjectPropertyNameNs.TrimStart('\'').TrimEnd('\'').Length; i++)
{
parameters[i] = string.Format("param1{0}", i);
command.Parameters.AddWithValue(parameters[i], Mpdue.TheObjectPropertyNameNs.TrimStart('\'').TrimEnd('\'')[i]);
}
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Response.Write(reader["dOCode"].ToString() + "<br />");
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
Error :
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.5.24-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , , , , , , , , , , , , , , , , , , , , , , , , , ) at line 1