It can probably be simpler but here is what I and my colleagues did on
a small school project.
The odbc connection connected to a PostgreSql database.
Add the following "using" statement to the top of the cs page:
using System.Data.Odbc;
Then, here is a routine that was used to select data from a table and
bind it to a drop down box on the form.
private void InitWardDropDown()
{
OdbcConnection cn;
string sConnString
=@"Protocol=6.4;LFConversion=1;Ksqo=1;UnknownSizes=0;Debug=0;UseServerSidePrepare=0;Parse=0;SERVER=server8.cs.uofs.edu;ByteaAsLongVarBinary=0;TextAsLongVarchar=1;DRIVER=PostgreSQL;ReadOnly=0;FakeOidIndex=0;ConnSettings=;MaxLongVarcharSize=8190;MaxVarcharSize=254;Socket=4096;TrueIsMinus1=0;DisallowPremature=0;DATABASE=se521grp3;Optimizer=1;UID=se521grp3;ShowOidColumn=0;UseDeclareFetch=0;CancelAsFreeStmt=0;BI=0;CommLog=0;Fetch=100;UpdatableCursors=0;ExtraSysTablePrefixes=dd_;UnknownsAsLongVarchar=0;RowVersioning=0;PORT=5432;BoolsAsChar=1;ShowSystemTables=0";
cn = new OdbcConnection(sConnString);
cn.Open();
// The following select union puts an empty record at the
beginning of the
//return data. This makes the first item inthe drop down list
appear empty.
string sSql = "select '' as name from ward UNION select name
from ward order by name";
//Set up the data adapter
OdbcDataAdapter da = new OdbcDataAdapter();
da.TableMappings.Add("Table", "ward");
OdbcCommand cm = new OdbcCommand(sSql,cn);
cm.CommandType = CommandType.Text;
da.SelectCommand = cm;
da.SelectCommand.CommandText = sSql;
da.SelectCommand.Connection = cn;
//I think Dataset name here has to match DA mapping above.
DataSet ds = new DataSet("ward");
//tell the data adapter to fill the dataset
da.Fill(ds);
//The following 4 lines bind the dataset to the drop down
//list, giving it its values.
//ddlWard is the drop down list on the html form
ddlWard.DataSource=ds;
ddlWard.DataMember = ds.Tables["ward"].ToString();
ddlWard.DataValueField = "name";
ddlWard.DataBind();
//release the resources
ds.Dispose();
da.Dispose();
cn.Close();
}
Hope it is more helpful than confusing.
Jeff