프로그램 자료/ASP.NET

DataTable의 Row가 하나일 때 각각의 ColumnName과 Value를 Dictionary로 반환하기

motolies 2015. 4. 23. 13:06

출처 : 본인



게시판에서 게시글 View 페이지 같은 곳에서 쓰면 편할 꺼 같아서.....



/// <summary>

/// 쿼리 반환값이 로우 1개일 칼럼명을 Key Value 가져오는 함수

/// </summary>

/// <param name="dt">DataTable : Rows.Count 1 가능</param>

/// <returns></returns>

public Dictionary<string, string> GetRowsValueToDictionary(DataTable dt)

{

    Dictionary<string, string> dic = new Dictionary<string, string>();

 

    for (int i = 0; i < dt.Columns.Count; i++)

    {

        string colName = dt.Columns[i].ColumnName;

        string ColValue = string.Empty;

        if (dt.Rows.Count > 0)

            ColValue = dt.Rows[0][i].ToString();

 

        dic.Add(colName, ColValue);

    }

 

    return dic;

}