C# 在dropdownlist中显示数据库中的项目
我正在使用ado.net显示sql数据库中的日期。以下是代码:C# 在dropdownlist中显示数据库中的项目,c#,asp.net,ado.net,C#,Asp.net,Ado.net,我正在使用ado.net显示sql数据库中的日期。以下是代码: protected void Page_Load(object sender, EventArgs e) { string cs = ConfigurationManager.ConnectionStrings["YoutubeConnectionString1"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) {
protected void Page_Load(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["YoutubeConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select CityId, CityName, Country from Youtube", con);
con.Open();
drpCountries.DataSource = cmd.ExecuteReader();
drpCountries.DataTextField = "CityName";
drpCountries.DataValueField = "CityId";
drpCountries.DataBind();
}
}
}
每次运行此命令时,都会出现一个错误,显示“无效的对象名Youtube”
谢谢
Dan从Youtube中选择CityId、CityName、Country。Youtube是您的表名吗?这意味着您在数据库中没有通过连接字符串访问的表
Youtube
。Youtube是“Youtube”YouTubeConnectionString指定的数据库中的表?这意味着在数据库中找不到Youtube
表或视图。检查数据库中是否存在该数据库,以及连接字符串是否指向正确的数据库。yea youtube是数据库的名称。这是我的连接字符串连接字符串>好的,很好。你应该发布你的答案,让其他人看到。