csharpnewbie
csharpnewbie

Reputation: 829

Escape forward slash in MySQL in C#

I need to connect to MYSQL from C#(ASP.NET Web API) and run an SQL. The dynamic input param for the SQL contains forward slash. The query returns 0 records, even though data is present for the input. Tried replacing the slash with double slash as well and that did not work either. (empName.Replace("/","//"))

string query = "select * from employee where empName = @empName";
string empName = "abc/abc";
using (DbCommand cmd = db.GetSqlStringCommand(query))
{
    db.AddInParameter(cmd, "empName", DbType.String, empName);
    using (IDataReader reader = db.ExecuteReader(cmd))
    {
    }
}

Can you pls suggest me on how to fix this problem? Thanks in advance!!

Upvotes: 0

Views: 1018

Answers (1)

mjb
mjb

Reputation: 7969

Change this

db.AddInParameter(cmd, "empName", DbType.String, empName);

to this:

db.AddInParameter(cmd, "@empName", DbType.String, empName);

Upvotes: 1

Related Questions