加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何从datetime列中选择不同的年份并将结果添加到C#中的comboBox

发布时间:2020-12-15 04:27:15 所属栏目:百科 来源:网络整理
导读:我正在使用visual studio 2010和SQL Management Studio R2 虽然sql查询在sql management studio中工作正常.它在视觉工作室中引发了一个例外.超出索引例外,当我编辑进行任何其他调整时,它会抛出格式异常.请帮我.代码如下: string sql = "SELECT DISTINCT Yea
我正在使用visual studio 2010和SQL Management Studio R2
虽然sql查询在sql management studio中工作正常.它在视觉工作室中引发了一个例外.超出索引例外,当我编辑进行任何其他调整时,它会抛出格式异常.请帮我.代码如下:
string sql = "SELECT DISTINCT Year(tdate) FROM saletransaction ORDER BY Year(tdate) DESC";
 cmd = new SqlCommand(sql,con);                
 dr = cmd.ExecuteReader();
 DateTime dt;
 while (dr.Read())
 {
     if (dr.HasRows == true)
     {
         dt = Convert.ToDateTime(dr["tdate"].ToString()); //tdate is the name of the column (getting an error at this line. )
         comboBox1.Items.Add(dt.Year.ToString());
     }
 }

解决方法

你没有选择tdate但是你选择了Year(tdate)

我会将查询修改为:

string sql = "SELECT DISTINCT Year(tdate) AS tdate_year FROM saletransaction ORDER BY Year(tdate) DESC";

并使用dr [“tdate_year”]访问它

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读