我在使用contains(transact-SQL)返回记录时遇到了麻烦。
此查询在SQL服务器查询窗口中使用静态值
的结果select * from archive_master where docgroupID=46 and( CONTAINS((docno,docname,accountno,accountName,tags,docyear,docmonth),'"1"')
OR CONTAINS((docno,docname,accountno,accountName,tags,docyear,docmonth),'"*1"')
OR CONTAINS((docno,docname,accountno,accountName,tags,docyear,docmonth),'"1*"')
OR CONTAINS((docno,docname,accountno,accountName,tags,docyear,docmonth),'"*1*"')
)
但是如果我把查询放到sqlcommand并用参数替换静态值,它就不会返回记录
继承我的代码:
string oconn = ConfigurationManager.ConnectionStrings["ARCHDB"].ConnectionString;
using (SqlConnection empCon = new SqlConnection(oconn))
{
if (txtsearch.Text == "")
{
query = "select * from archive_master where docgroupID=@docgroupID";
}
else
{
query = @"select * from archive_master where docgroupID=@docgroupID and (CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), '""@qry""') OR CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), ""*@qry"") OR CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), ""@qry*"") OR CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), ""*@qry*""))";
}
using (SqlCommand emp_Cmd = new SqlCommand(query, empCon))
{
if (txtsearch.Text == "")
{
SqlParameter pdocgroupID = emp_Cmd.Parameters.Add("@docgroupID", SqlDbType.BigInt);
pdocgroupID.Value = Request.QueryString["docgroupID"];
}
else
{
SqlParameter pdocgroupID = emp_Cmd.Parameters.Add("@docgroupID",SqlDbType.BigInt);
SqlParameter pqry = emp_Cmd.Parameters.Add("@qry", SqlDbType.NVarChar);
pdocgroupID.Value = Request.QueryString["docgroupID"];
pqry.Value = txtsearch.Text;
}
emp_Cmd.CommandType = CommandType.Text;
empCon.Open();
SqlDataAdapter adpData = new SqlDataAdapter(emp_Cmd);
DataTable dt = new DataTable();
adpData.Fill(dt);
GridDocgroup.DataSource = dt;
GridDocgroup.DataBind();
empCon.Close();
}
}
是我的查询字符串正确的方法吗?其中@qry和@docgroupID是参数
query = @"select * from archive_master where docgroupID=@docgroupID and (CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), '""@qry""') OR CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), ""*@qry"") OR CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), ""@qry*"") OR CONTAINS((docno, docname, accountno, accountName, tags, docyear, docmonth), ""*@qry*""))";
答案 0 :(得分:0)
作为一种解决方法,我修改了查询并在参数值
上传递了通配符action