I am trying to find a
way to populate my hash table variables from a database instead of hard coding
them like this (hshTable .Add("Author1", "Mahesh Chand");) id rather want to
get the data, or object values of the hashtable from a database and then
populate the hash htable maybe something like this Hashtable ht = new hashtable();
ht.ADD(“FirstName”,”Get data from database stored procedure”), I hope im making
sense. Thanks in adavnace.
Loading
IamStalkerPosted Nov 19, 2008, 11:08 AM
HashTable ht = new HashTable();
foreach(DataRow dr in ds.Tables[0].Rows)
{
ht.Add(dr["EmployeeID"].toString(),dr["EmployeeName"].ToString());
}
it's works for me very nicely so have fun!!!
Regards IamStalker...
Ryan AlfordPosted Nov 18, 2008, 10:56 AM
Satish KathiPosted Nov 17, 2008, 10:51 AM
//Here is sample code to read data from access table1 and fill hash Table
//make sure column value which is data source for Key value in hash table is unique
//(in this example values in Column1 must be unique)
//Connection string for Access
string ConnectionStringAccess =
string.Format("Data Source= {0}; Provider=Microsoft.Jet.OLEDB.4.0; Persist security Info = false", @"C:\KSK_Access.mdb");
//Connection object for Access
OleDbConnection connAccess = new OleDbConnection(ConnectionStringAccess);
//Command object for Access
OleDbCommand cmdAccess = connAccess.CreateCommand();
cmdAccess.CommandType = CommandType.Text;
cmdAccess.CommandText = "SELECT Column1, Column2 From Table1 ";
//Open connections
connAccess.Open();
//Read Excel
OleDbDataReader drAccess = cmdAccess.ExecuteReader();
Hashtable ht = new Hashtable();
while (drAccess.Read())
{
ht.Add(drAccess[0], drAccess[1]);
}
//close connections
connAccess.Close();
//hope this helps