2014-12-15 4 views
0

MySQL 데이터 테이블에서 데이터를 가져온 WPF 앱에서 DataGrid를 채우려고합니다. 나는 같은 구조의 5 개의 테이블을 가지고 있으며 각각에서 20 개의 라인을 가져와야합니다.데이터 세트에 열 추가 C#

아래 코드는 하나의 작은 점을 제외하고는 훌륭하게 수행됩니다. 저장소 이름 (첫 번째 쿼리에서 가져온 변수를 저장)이있는 추가 열이 각 해당 행에 필요합니다.

어디에서 시작 하시겠습니까?

ds.Tables[0].Columns.Add("ColumnName", Type.GetType("System.String")); 

:

void fillDataTable() 
{ 
    string store; 
    string connection = Properties.Settings.Default.sqlconn; 
    MySqlConnection conn = new MySqlConnection(connection); 
    conn.Open(); 
    string nameQuery = "SELECT `name` FROM `stores`"; 
    MySqlCommand selectStore = new MySqlCommand(nameQuery, conn); 
    MySqlDataReader readName = selectStore.ExecuteReader(); 
    DataSet ds = new DataSet(); 
    while (readName.Read()) 
    { 
     store = readName["name"].ToString(); 
     MySqlConnection conn1 = new MySqlConnection(connection); 
     conn1.Open(); 
     string dataQuery = "SELECT * FROM `"+ store + "` ORDER BY `id` DESC LIMIT 20"; 
     MySqlCommand selecetData = new MySqlCommand(dataQuery, conn1); 
     MySqlDataAdapter da = new MySqlDataAdapter(selecetData); 
     da.Fill(ds, "LoadDataBinding"); 
     conn1.Close(); 
    } 
    salesGrid.DataContext = ds; 
    conn.Close(); 
} 

답변

0

아니라, 당신은이 하나로 간단하게 코드에 의해 귀하의 DataTable에 열 임의의 수를 추가 할 수 있습니다 당신이 데이터 집합을 입력 한 후 열을 추가 할 필요가 .

2

당신은 (당신의 열을 채우기 위해 귀하의 데이터 테이블을 통해 피 반복)이 시도 할 수 있습니다 :

string dataQuery = "SELECT '"+ store + "' AS \"store\",* FROM '"+ store + "' ORDER BY 'id' DESC LIMIT 20"; 
관련 문제