【问题标题】:C# get result of Select using DataAdapter and DatasetC# 使用 DataAdapter 和 Dataset 获取 Select 的结果
【发布时间】:2016-11-11 14:53:57
【问题描述】:

我想执行一个 SELECT 查询,但是它抛出了一个异常:

不存在从对象类型 System.Web.UI.HtmlControls.HtmlSelect 到已知托管提供程序本机类型的映射。

这是我的代码:

        DateTime start = new DateTime(startY, startM, startD);
        //string s = start.ToString();
        DateTime end = new DateTime(endY, endM, endD);
        string title = title_key.Value;
        string cat = sCategory.Value;

        var sqlcon = new SqlConnection("Data Source=maryam-pc;Initial Catalog=news_portal1;Integrated Security=True;");
        sqlcon.Open();

        SqlCommand sqlcom = new SqlCommand();
        sqlcom.Connection = sqlcon;
       // SqlCommandBuilder sc = new SqlCommandBuilder();
        //sc.

       sqlcom.CommandText = "select title,text,category,datetime from news where  category= @cat and title = @ttl ";
       // sqlcom.CommandText = "select pk,title,text,category,datetime from news ";
        sqlcom.Parameters.AddWithValue("@ttl", title);
        sqlcom.Parameters.AddWithValue("@cat", sCategory);
        sqlcom.Parameters.AddWithValue("@start", start.ToString());
        sqlcom.Parameters.AddWithValue("@end", end.ToString());
        // sqlcom.Parameters.AddWithValue("@ttl", title);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = sqlcom;
        //GridView GridView1 = new GridView();
        da.Fill(ds);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count != 0)
            {
                System.Diagnostics.Debug.WriteLine("not empty");
                SearchResults.DataSource = ds;
                SearchResults.DataBind();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("empty");
                SearchResults.DataSource = null;
                SearchResults.DataBind();
            }
        }



    }

title、category 为 varchar(),日期为 dateTime。

【问题讨论】:

    标签: c# sql dataadapter


    【解决方案1】:

    使用选择的值而不是选择本身:

    sqlcom.Parameters.AddWithValue("@cat", sCategory.Value);
    

    或者,使用您之前在代码中声明和初始化的变量:

    sqlcom.Parameters.AddWithValue("@cat", cat);
    

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 2011-02-12
      • 2011-06-02
      • 2011-10-26
      • 2011-06-02
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多