我想使用类似查询的字符串值。我写的是这样的,'_'用于接受字符串中的字符。但它不起作用。查询字符串是否正确?请帮忙。
userName = nvc["user_name"];
string userName_Remove_First = userName.Substring(1); //Removes the first character of userName
string Last = userName_Remove_First.Remove(userName_Remove_First.Length - 1); //Removes the last character of userName_Remove_First
SqlCommand cmd = new SqlCommand("select student_id,student_name from student_details where student_id LIKE ('"+last+"_') ", con);
SqlDataReader dr = cmd.ExecuteReader();
答案 0 :(得分:1)
您可以使用 Like Query
以下SQL语句选择名称以字母“s”开头的所有Employee:
SELECT * FROM Employee
WHERE EmployeeName LIKE 's%';
关于上面的例子我稍微修改了你的查询
string cmd = select student_id,student_name from student_details where student_id
LIKE ('"+Last+"%') and len(student_id) = "+Last.Length+1;
因此,如果您的表格中包含HALF,HALFER,HAF,HAG
个记录且您的用户名为AHAR
,那么您的OP将为HAF,HAG
答案 1 :(得分:0)
试试这个
SqlCommand cmd = new SqlCommand("select student_id,student_name from student_details where student_id LIKE "+last+"_", con);
答案 2 :(得分:0)
假设您要查找student_id
以Last
的内容开头的学生,您可以执行此查询:
var cmd = new SqlCommand("select student_id,student_name from student_details where student_id LIKE @term", con);
cmd.Parameters.AddWithValue("@term",Last + "%");
SqlDataReader dr = cmd.ExecuteReader();
如果您只需要ID为Last
且另外1个字符的学生,则可以使用
cmd.Parameters.AddWithValue("@term",Last + "_");
答案 3 :(得分:0)
尝试这样的事情:
string last= "A";
Select * from Students Where StudentID LIKE '"%+last+"'
OR 从学生ID LIKE'“+ last +%”'
中选择*