C# 使用 DataAdapter 和 Dataset 获取 Select 的结果
C# get result of Select using DataAdapter and Dataset
我想执行一个 SELECT 查询,但它抛出异常:
不存在从 object 类型 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();
}
}
}
标题、类别为 varchar(),日期为 dateTime。
使用 select 的值而不是 select 本身:
sqlcom.Parameters.AddWithValue("@cat", sCategory.Value);
或者,使用您之前在代码中声明和初始化的变量:
sqlcom.Parameters.AddWithValue("@cat", cat);
我想执行一个 SELECT 查询,但它抛出异常:
不存在从 object 类型 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();
}
}
}
标题、类别为 varchar(),日期为 dateTime。
使用 select 的值而不是 select 本身:
sqlcom.Parameters.AddWithValue("@cat", sCategory.Value);
或者,使用您之前在代码中声明和初始化的变量:
sqlcom.Parameters.AddWithValue("@cat", cat);