首页 > 代码库 > C# DataTable 相关操作

C# DataTable 相关操作

判断DataTable中某列是否包含某值

/// <summary>
    /// 判断DataTable中是否包含某值
    /// </summary>
    /// <param name="dt">DataTable</param>
    /// <param name="columnName">列名</param>
    /// <param name="fieldData"></param>
    /// <returns></returns>
    public Boolean IsColumnIncludeData(DataTable dt, String columnName, string fieldData)
    {
        if (dt == null)
        {
            return false;
        }
        else
        {
            DataRow[] dataRows = dt.Select(columnName + "=‘" + fieldData + "");
            if (dataRows.Length.Equals(1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

 

向DataTable中添加数据

DataTable dt = null;

        dt = handle.ExecuteDataTable(sql, true);

        #region

        DataRow dr;

        for (int i = 0; i < code.Length; i++)
        {
            if (IsColumnIncludeData(dt, "SystemCode", code[i]) == false)
            {
                dr = dt.NewRow();
                dr[0] = name[i];
                dr[1] = code[i];
                dr[2] = 0;
                dt.Rows.Add(dr);
            }
        }
        
        #endregion