我正在使用 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());
}
}