简体   繁体   English

ASP.net C#简单查询参数问题

[英]ASP.net c# simple query paramter question

        // Add into DB
        using (tblArtworkTemplatesTableAdapter tblAdapter = new tblArtworkTemplatesTableAdapter())
        {
            tblAdapter.Insert(DateTime.Now, "@specID");
            "@specID" = int.Parse(lstChooseSpec.SelectedValue)
        }

I know the code is wrong, just for illustration of my objective, how do I paramatise the input? 我知道代码是错误的,仅出于说明目的的考虑,如何对输入进行参数化?

Generally it depends. 通常,这取决于。 If You are using any kind of ORM like LINQ to SQL or NHibernate, it will do it for You no questions asked. 如果您正在使用诸如LINQ to SQL或NHibernate之类的ORM,它将毫无问题地为您完成。 If YOu are doing it using Plain ADO objects (which I suppose is the case) then You will have to comeup with the Command (or SQLCommand or any other ICommand implementation) object and use SQLParameter class (or other parameter classes). 如果您使用的是普通ADO对象(我想是这种情况),那么您将不得不提出Command(或SQLCommand或任何其他ICommand实现)对象,并使用SQLParameter类(或其他参数类)。

ICommand has the collection of parameters that You can arbitralily edit. ICommand具有您可以任意编辑的参数集合。

    SqlCommand cmd = new SqlCommand(
            "select * from STH where column = @SpecID", conn);

    //it might be useful to specify a type as well
    SqlParameter param  = new SqlParameter();
    param.ParameterName = "@SpecID";
    //I woudl use the TryParse method though
    param.Value         = int.Parse(lstChooseSpec.SelectedValue);

    cmd.Parameters.Add(param);

This line 这条线

"@specID" = int.Parse(lstChooseSpec.SelectedValue)

Is incorrect. 是不正确的。 You can't assign a value to a constant. 您不能为常数分配值。 You might mean something like 您可能会说类似

specId = int.Parse(lstChooseSpec.SelectedValue);

The rest of the code is confusing. 其余代码令人困惑。 Why are you parsing lstChooseSpec.SelectedValue to an integer, then trying to add it to the adapter as a DateTime? 为什么将lstChooseSpec.SelectedValue解析为整数,然后尝试将其作为DateTime添加到适配器? C# is strongly-typed: something is either an int or a DateTime , but cannot be both. C#是强类型的:东西可以是intDateTime ,但不能两者都为。

It might help if you could post the rest of the method. 如果可以发布其余方法,则可能会有所帮助。

Also, have a look at this overview on MSDN. 另外,请查看有关MSDN的概述。

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

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