您的位置:首页 > 数据库

使用参数化SQL语句进行模糊查找

2012-03-17 09:43 309 查看
今天想用参数化SQL语句进行模糊查找,一开始的使用方法不正确,摸索了好一会。

1、使用参数化SQL语句进行模糊查找的正确方法:

//定义sql语句

string sql = "SELECT StudentID,StudentNO,StudentName FROM Student WHERE StudentName like @StudentName";

//给参数赋值

command.Parameters.AddWithValue("@StudentName", txtStudentName.Text+"%");

2.错误做法1:

//定义sql语句

string sql = "SELECT StudentID,StudentNO,StudentName FROM Student WHERE StudentName like '@StudentName%'";

//给参数赋值

command.Parameters.AddWithValue("@StudentName", txtStudentName.Text);

3.错误做法2:

//定义sql语句

string sql = "SELECT StudentID,StudentNO,StudentName FROM Student WHERE StudentName like @StudentName%";

//给参数赋值

command.Parameters.AddWithValue("@StudentName", txtStudentName.Text);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: