SQL Server????????????????μ????
???????????? ???????[ 2013/2/18 10:15:14 ] ????????
??????????СС??????????????????в????????????where in??like???????????????????????????????????μ??????????????????????????SQL????в??????????????????????????SQL?????????????????where in ?????????????????????????????????????????????е?????????????where in??like????????????????ò?????????????
????where in ?????????????
???????????????????????????SQL????????????????????????
string userIds = "1??2??3??4";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = string.Format("select * from Users(nolock) where UserID in({0})"?? userIds);
comm.ExecuteNonQuery();
}
????????????????????е?????????????????????SQL???????
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select * from Users(nolock) where UserID in(@UserID)";
comm.Parameters.Add(new SqlParameter("@UserID"?? SqlDbType.VarChar?? -1) { Value = "1??2??3??4" });
comm.ExecuteNonQuery();
}
?????????????????????? varchar ? '1??2??3??4' ????????????? int ????????????????????????where in????@UserID??????????????????????????????????????
select * from Users(nolock) where UserID in('1??2??3??4')
????????е????????????????SQL??в???????????????????κν????
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select * from Users(nolock) where UserName in(@UserName)";
comm.Parameters.Add(new SqlParameter("@UserName"?? SqlDbType.VarChar?? -1) { Value = "'john'??'dudu'??'rabbit'" });
comm.ExecuteNonQuery();
}
?????????????κδ?????鯔?????????????????@UserName??????????????????????????????????????
select * from Users(nolock) where UserName in('''john''??''dudu''??''rabbit''')
????????????????????μ???where in ?????????????????????????????????????????????????????????????where in?????????????????where in ???Σ???????????????????????
????????1?????CHARINDEX??like ??????????????????????????????????????????????????????????????????????ò???????Ч????????????????????????????????裬???????????????????????????????????????д?????????????????????????????С???????????????????SQL?????????д??????????????????????????????
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//???CHARINDEX?????????????????????ò?????????????????Ч
comm.CommandText = "select * from Users(nolock) where CHARINDEX('??'+ltrim(str(UserID))+'??'??'??'+@UserID+'??')>0";
comm.Parameters.Add(new SqlParameter("@UserID"?? SqlDbType.VarChar?? -1) { Value = "1??2??3??4" });
comm.ExecuteNonQuery();
}
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
//???like?????????????????????ò?????????????????Ч
comm.CommandText = "select * from Users(nolock) where '??'+@UserID+'??' like '%??'+ltrim(str(UserID))+'??%' ";
comm.Parameters.Add(new SqlParameter("@UserID"?? SqlDbType.VarChar?? -1) { Value = "1??2??3??4" });
comm.ExecuteNonQuery();
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11