Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列添加到数据集C#_C#_Mysql_Wpf_Datagrid - Fatal编程技术网

将列添加到数据集C#

将列添加到数据集C#,c#,mysql,wpf,datagrid,C#,Mysql,Wpf,Datagrid,我试图用从mysql表中获取的数据填充WPF应用程序中的datagrid。我有5个相同结构的表,我需要得到每个表的最后20行 下面的代码做得很好,除了一件小事——我需要在每个对应的行中添加一个带有存储名称的列(第一次查询获取的存储变量) 请想一想从哪里开始 void fillDataTable() { string store; string connection = Properties.Settings.Default.sqlconn; MySqlConnection

我试图用从mysql表中获取的数据填充WPF应用程序中的datagrid。我有5个相同结构的表,我需要得到每个表的最后20行

下面的代码做得很好,除了一件小事——我需要在每个对应的行中添加一个带有存储名称的列(第一次查询获取的存储变量)

请想一想从哪里开始

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();
}

好的,您可以通过如下代码向DataTable添加任意数量的列:

ds.Tables[0].Columns.Add("ColumnName", Type.GetType("System.String"));
注意:填充数据集后需要添加列。

您可以尝试此操作(避免通过数据表循环填充列):

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