2013-01-23 86 views
0

我通过C#应用程序从SQL Server数据库访问我的数据时遇到了一些问题。我有一个小项目,我不能进一步,因为我的数据加载英寸我试图加载它在DataGridView将数据从SQL Server加载到C#应用程序

下面是一些代码示例:

public List<Country> GetCountryList() 
{ 
     string query = "SELECT * FROM Orszag", error = string.Empty; 
     SqlDataReader rdr = ExecuteReader(query, ref error); 

     List<Country> countryList = new List<Country>(); 
     if (error == "OK") 
     { 
      while (rdr.Read()) 
      { 
       Country item = new Country(); 
       item.CountryId = Convert.ToInt32(rdr[0]); 
       item.CountryName = rdr[1].ToString(); 
       countryList.Add(item); 
      } 
     } 
     CloseDataReader(rdr); 

     return countryList; 
    } 

这就是我把我的数据在列表

private void FillDgvGames() 
{ 
     dgvGames.Rows.Clear(); 
     List<Country> CountryList = m_Country.GetCountryList(); 

     foreach (Country item in CountryList) 
     { 
      dgvGames.Rows.Add(item.CountryId,item.CountryName); 
     } 
} 

而这正是我找回它......我必须做出同样的有8个桌子的东西,但这是最简单的,我认为这样更容易....如果有人可以帮助我,我会很感激它...

PS:

这是在执行读写

protected SqlDataReader ExecuteReader(string query, ref string errorMessage) 
{ 
     try 
     { 
      OpenConnection(); 
      SqlCommand cmd = new SqlCommand(query, m_Connection); 
      SqlDataReader rdr = cmd.ExecuteReader(); 
      errorMessage = "OK"; 
      return rdr; 
     } 
     catch (SqlException e) 
     { 
      errorMessage = e.Message; 
      CloseConnection(); 
      return null; 
     } 
} 

而且这是在连接字符串

protected string m_ConnectionString = "Data Source=SpD-PC;Initial Catalog=master;Integrated Security=SSPI"; 
+2

我不明白实际的问题。 _“我不能进一步因为我的数据正在加载”_这是什么意思? –

+0

你不会说这是什么异常或其他意外行为。 – Matt

+0

问题是,我的数据网格视图是空白.....我的数据不会出现.... – spd92

回答

0

你要为你的DataGridView的数据源?

dgvGames.DataSource = CountryList; 
相关问题