首页 > 解决方案 > 列 'country_id' 不属于 table 。- 错误类型:System.Data.DataColumn GetDataColumn(System.String)

问题描述

仅在服务器错误日志上随机获取此错误,实际上 GetAllCountries 程序总是返回所有国家,没有 country_id 就没有结果的机会

我将响应数据表记录到一个文件中,有趣的是,记录的响应是由另一个从其他函数调用的存储过程返回的。我认为 DBHelper 类共享两个响应或 sql 执行错误的过程。

public List<Country> GetAllCountry()
    {
        List<Country> countries = new List<Country>();

        try
        {
            SqlCommand cmd = new SqlCommand();
            DataTable dt = new DataTable();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetAllCountries";
            dt = DBHelper.Read(cmd);

            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Country country = new Country();
                    country.CountryId = Convert.ToInt32(dr["country_id"]);
                    country.CountryCode =Convert.ToString(dr["country_code"]);
                    country.CountryName =Convert.ToString(dr["country_name"]);

                    countries.Add(country);
                }
            }
        }
        catch(Exception ex)
        {
            SysError.LogError(ex, "", "country", "GetAllCountry");
        }

        return countries;
    }

Read Function

public static class DBHelper
{

     private static string ConnStr;
                private static string Connection= "SettingsDB";
         public static DataTable Read(SqlCommand cmd)
                {

                    ConnStr = 
          ConfigurationManager.ConnectionStrings[Connection].ConnectionString;
                    DataTable dt = new DataTable();
                    using (SqlConnection con = new SqlConnection(ConnStr))
                        try
                        {

                            using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                            {
                                cmd.Connection = con;
                                con.Open();
                                dt.Locale = 
            System.Globalization.CultureInfo.InvariantCulture;
                                adp.Fill(dt);
                            }
                        }
                        catch (Exception ex)
                        {
                             SysError.LogError(ex, "Read", "", "");
                        }
                        finally
                        {
                            con.Close();
                        }

                    return dt;
                }
}

有问题的存储过程:

select country_id ,
country_code ,
country_name +' '+'('+country_code+')' as country_name
from tbl_country 
order by country_name asc

标签: c#sqlsql-serverc#-4.0

解决方案


你的问题可能在这里吗?

                    Country country = new Country();
                    Country.CountryId = Convert.ToInt32(dr["country_id"]);
                    Country.CountryCode =Convert.ToString(dr["country_code"]);
                    Country.CountryName =Convert.ToString(dr["country_name"]);

                    countries.Add(country);

您使用名称 country(小写)实例化 Country 类的新实例;但是,分配(正确的案例)都是针对类名点(国家)而不是实例化的名称。然后将实例化的类添加到列表中。我也同意@mortb 关于(dr["country_name"]).


推荐阅读