Getting column info from db with C#
Posted
by lYriCAlsSH
on Stack Overflow
See other posts from Stack Overflow
or by lYriCAlsSH
Published on 2010-03-23T06:46:39Z
Indexed on
2010/03/23
6:53 UTC
Read the original article
Hit count: 189
In PHP I am able to retrieve information from a db like so:
<?php
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
I am trying to acomplish the same thing in C#:
OdbcCommand cmd = new OdbcCommand("SELECT * FROM users WHERE id = @id");
cmd.Parameters.Add("@id", id);
OdbcDataReader reader = cmd.ExecuteReader();
Dictionary<string, string> data = new Dictionary<string, string>();
while (reader.Read())
{
data.Add("id", reader.GetString(0));
data.Add("username", reader.GetString(1));
data.Add("firstName", reader.GetString(2));
}
return data;
Is it possible to reference the column in the table by name without going through all this trouble?
© Stack Overflow or respective owner