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=s erver8.cs.uofs.edu;ByteaAsLongVarBinary=0;TextAsLo ngVarchar=1;DRIVER=PostgreSQL;ReadOnly=0;FakeOidIn dex=0;ConnSettings=;MaxLongVarcharSize=8190;MaxVar charSize=254;Socket=4096;TrueIsMinus1=0;DisallowPr emature=0;DATABASE=se521grp3;Optimizer=1;UID=se521 grp3;ShowOidColumn=0;UseDeclareFetch=0;CancelAsFre eStmt=0;BI=0;CommLog=0;Fetch=100;UpdatableCursors= 0;ExtraSysTablePrefixes=dd_;UnknownsAsLongVarchar= 0;RowVersioning=0;PORT=5432;BoolsAsChar=1;ShowSyst emTables=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
On 25 Sep 2005 12:05:56 -0500,
jo*@invalid.address wrote:
I'm pretty new to C# so forgive me if I missed something obvious. I'm
trying to find out if C# .NET can be used to access something other
than SQL Server, using ODBC. I haven't seen anything that talks about
ODBC in the book I have or from looking around on Microsoft.com.
If someone could point me to some documentation about this I'd
appreciate it.
Joe