简体   繁体   English

如何将参数传递给WHERE子句?

[英]How could I pass a parameter to a WHERE clause?

The title of the question might be misleading. 问题的标题可能会引起误解。 I need to fetch a attribute from XML file. 我需要从XML文件中获取属性。 And then, search the Database with the attribute. 然后,使用属性搜索数据库。 Since the value of attribute is dynamic, I have to try send it as parameter to WHERE Clause of SQL scripts. 由于attribute的值是动态的,因此我必须尝试将其作为参数发送到SQL脚本的WHERE子句。 However, it always return invalid column error. 但是,它总是返回无效的列错误。

Here is a part of the codes: 这是部分代码:

string umail = "";
XDocument loaded = XDocument.Load(@"C:\1.xml");

var q = from c in loaded.Descendants("AdminUserDB.dbo.U_User")
        select (string)c.Element("URI");

foreach (string em in q)
    umail = em;

SqlConnection cn = new SqlConnection("server=(local);database=AdminUserDB;Persist Security Info=True; uid=sa;pwd=pwd");
cn.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM dbo.U_User WHERE URI=umail", cn);

..... .....

Is there any other methods for this operation? 此操作还有其他方法吗?

Thanks 谢谢

SuT SuT

Simply use parameters: 只需使用参数:

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM dbo.U_User WHERE URI=@umail", cn);
da.SelectCommand.Parameters.AddWithValue("@umail", umail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM