1
//cmd.CommandText = @"select MaKH from KHACHHANG where HoTenKH='a'";
cmd.CommandText = @"select MaKH from KHACHHANG where HoTenKH=$tenKH";
cmd.Parameters.Add(new SQLiteParameter("$tenKH", tenKH));

The comment line works, cmd is a DbCommand, I've tried

cmd.CommandText = @"select MaKH from KHACHHANG where HoTenKH='$tenKH'";

But it doesn't work neither.

1
  • Are you getting an error message, or are you getting zero results? Commented Jun 21, 2010 at 3:03

3 Answers 3

2

This seems silly, but are you making sure to do this:

string tenKH = "a";

before doing this:

cmd.CommandText = @"select MaKH from KHACHHANG where HoTenKH = @tenKH";
cmd.Parameters.Add(new SQLiteParameter("@tenKH", tenKH));
Sign up to request clarification or add additional context in comments.

3 Comments

Why the downvote? This is a perfectly reasonable question. And he still hasn't told use what "doesn't work".
This really makes sense. I pass tenKH = textBox.ToString(); , I change to tenKH = textBox.Text; < Pure string, and it works. Thanks
@Sorry for wrong vote, you get my best vote, with respect :) I'm a young developer.
1

SQLite uses a colon to designate a named parameter, not a dollar sign. Try

cmd.CommandText = @"select MaKH from KHACHHANG where HoTenKH=:tenKH";
cmd.Parameters.Add(new SQLiteParameter("tenKH", tenKH));

You could also use the @ symbol if you want the code to look more like SQL Server code:

cmd.CommandText = @"select MaKH from KHACHHANG where HoTenKH=@tenKH";
cmd.Parameters.Add(new SQLiteParameter("tenKH", tenKH));

4 Comments

@nXqd: Did you try a colon instead? See my first example.
I've tried both ways, HoTenKH in MaKH Table is char(30) , tenKH is a string. Does it cause problem ?
I don't know SQLite, but with other databases, the prefix character for the parameter name needs to be used in both the SQL statement and the parameter definition, e.g., new SQLiteParameter(":tenKH", tenKH); // note added colon, or @ sign, whichever you use.
@Cylon: It's worth a try. I'm not a SQLite developer, but the examples I saw online omitted the prefix. Note that SqlParameters do not require the prefix.
1

Use an @ sign instead of $ to prefix your parameter name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.