Crew .NET |
|
Werk met een Dictionary!
public Dictionary<int, String> GetProductMatenByID(int iProductID)
{
Dictionary<int, String> temp = new Dictionary<int, String>();
SqlConnection conn = new SqlConnection(@"Connectiestring hier");
SqlCommand cmd = new SqlCommand("SELECT productCode, maat FROM productCategory WHERE productID='" + iProductID + "'", conn);
SqlDataReader rdr;
try
{
conn.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
temp.Add(rdr.GetInt32(0), rdr.GetString(1));
}
catch (Exception ex)
{
}
finally
{
rdr.Close();
conn.Close();
}
return temp;
}
public Dictionary<int, String> GetProductMatenByID(int iProductID) { Dictionary <int, String> temp = new Dictionary <int, String>(); SqlConnection conn = new SqlConnection (@"Connectiestring hier"); SqlCommand cmd = new SqlCommand ("SELECT productCode, maat FROM productCategory WHERE productID='" + iProductID + "'", conn ); SqlDataReader rdr; try { conn.Open(); rdr = cmd.ExecuteReader(); while (rdr.Read()) temp.Add(rdr.GetInt32(0), rdr.GetString(1)); } catch (Exception ex) { } finally { rdr.Close(); conn.Close(); } return temp; }
En om dan uit te lezen doe je:
foreach(KeyValuePair<int, String> kvp in GetProductMatenByID(2))
//bijvoorbeeld:
Console.WriteLine(kvp.Key + ": " + kvp.Value + Environment.NewLine);
foreach(KeyValuePair<int, String> kvp in GetProductMatenByID(2)) //bijvoorbeeld: Console.WriteLine(kvp.Key + ": " + kvp.Value + Environment.NewLine);
|