【问题标题】:remove duplicate column name before add it on DataTable在将其添加到 DataTable 之前删除重复的列名
【发布时间】:2019-12-09 15:17:56
【问题描述】:

这是我的代码:

private void executerRequete(string requete, string description, string niveauAlerte)
{
    RequetesSQLResult res = new RequetesSQLResult();
    res.Description = description;

    string StSQL = requete;
    res.Results = new DataTable();
    try
    {
        using (SqlConnection _oConnection = new SqlConnection(_dataService.ParamGlobaux.ConnectionString))
        {
            SqlCommand command = new SqlCommand(StSQL, _oConnection);
            command.Parameters.AddWithValue("@mat", _dataService.ParamGlobaux.StMatricule);
            command.Parameters.AddWithValue("@dd", dateDebut);
            command.Parameters.AddWithValue("@df", datefin);


            _oConnection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var row = res.Results.NewRow();
                    object[] values = new object[reader.FieldCount];
                    int fieldCount = reader.GetValues(values);

                    if (res.Results.Columns.Count == 0)
                    {
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            res.Results.Columns.Add(new DataColumn(reader.GetName(i)));
                        }
                    }

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        row[i] = values[i];
                    }

                    res.Results.Rows.Add(row);
                }
            }
        }
    }
    catch (Exception x)
    {
        Debug.WriteLine(x.Message + ":" + x.StackTrace);
    }

    if (res.Results.Rows.Count > 0)
    {
        Results.Add(res);
    }
}

我的问题是当循环到达这里时:

res.Results.Columns.Add(new DataColumn(reader.GetName(i)));

只要我只使用一张桌子,一切都很好。当我使用两个或更多表时,我收到一条错误消息“列 [i] 已存在”。

示例:

SELECT * FROM Contrats X, Contrats Y
WHERE x.Avenant = 'CONTRAT' AND y.Avenant = 'CONTRAT' AND X.Matricule = Y.Matricule AND x.[Num Contrat] != y.[Num Contrat] AND
((x.[Date début] < y.[Date Fin] AND x.[Date Fin] > Y.[Date début]) OR
(x.[Date début] > y.[Date début] AND y.[Date Fin] IS NULL) OR
(x.[Date début] < y.[Date début] AND y.[Date Fin] IS NULL))
AND x.Matricule = @mat

=> 错误将是:“列'名称'已经存在”,因为它被读取了两次(每个表一个)。

如果它们具有相同的名称,我如何只保留一列?

【问题讨论】:

    标签: c# sql wpf


    【解决方案1】:

    在这种情况下,您不应使用SELECT *,而必须使用SELECT Column1, Column2,.. Column n

    使用JOIN 链接两个表而不是comma 也是一个好习惯

    SELECT Column1, Column2,.. Column n
    FROM Contrats X, Contrats Y
    WHERE x.Avenant = 'CONTRAT' AND y.Avenant = 'CONTRAT' AND X.Matricule = Y.Matricule AND x.[Num Contrat] != y.[Num Contrat] AND
          ((x.[Date début] < y.[Date Fin] AND x.[Date Fin] > Y.[Date début]) OR
          (x.[Date début] > y.[Date début] AND y.[Date Fin] IS NULL) OR
          (x.[Date début] < y.[Date début] AND y.[Date Fin] IS NULL))
          AND x.Matricule = @mat
    

    【讨论】:

      【解决方案2】:

      你需要两个表数据吗?

      如果不是 - 只返回第一个表数据

      select X.* from ...
      

      如果是 - 列出 select 中的所有必填字段,并为其指定唯一名称:

      select X.Name as X_Name, Y.Name as Y_Name from ...
      

      【讨论】:

        【解决方案3】:

        您可以在添加之前检查是否已经存在同名的列:

        string name = reader.GetName(i);
        if (!res.Results.Columns.Contains(name))
            res.Results.Columns.Add(new DataColumn(name));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-08
          • 2021-09-12
          • 1970-01-01
          相关资源
          最近更新 更多