C# and ODBC?

  • Thread starter Thread starter joe
  • Start date Start date
J

joe

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
 
Hi,

You can also use OLEDB, however the ODBC provider can be used. For the most
part you should first determine if there is not
a native managed provider for your target database if there is use that. If
not then try for an OLEDB provider and lastly go the ODBC route.

Hope this helps
 
Chris Taylor said:
You can also use OLEDB, however the ODBC provider can be used. For
the most part you should first determine if there is not a native
managed provider for your target database if there is use that. If
not then try for an OLEDB provider and lastly go the ODBC route.

Thanks, do you know of a web page that gives more details about how to
do this? I'm working through a book called "Microsoft Visual C# .NET
Step by Step" but it doesn't go into anything but SQL Server.

Joe
 
Thanks, do you know of a web page that gives more details about how to
do this? I'm working through a book called "Microsoft Visual C# .NET
Step by Step" but it doesn't go into anything but SQL Server.

Ahh, never mind. I found some stuff on the MSDN web site.

Thanks

Joe
 
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
 
Jeff said:
It can probably be simpler but here is what I and my colleagues did on
a small school project.

Thanks, postgres was what I had in mind as well. Appreciate it.

Joe
 
Back
Top