Kbase P132748: SQL: How to connect a C# (C Sharp) client to a Progress database using an ODBC DSN?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  18/01/2010 |
|
Status: Verified
GOAL:
SQL: How to connect a C# (C Sharp) client to a Progress database using an ODBC DSN?
GOAL:
How to construct the ODBC connection string for a Visual Studio .NET C# (C Sharp) client to an OpenEdge database using ODBC?
GOAL:
How to execute an SQL query against an OpenEdge database table using the OpenEdge ODBC driver from a Visual Studio .NET C# (C Sharp) client?
GOAL:
Sample C# (C Sharp) program that connects to an OpenEdge database via ODBC and executes an SQL query against one of its tables.
FACT(s) (Environment):
Windows
OpenEdge 10.x
FIX:
The following C# code demonstrates how to connect a Visual Studio .NET C# (C Sharp) client to an OpenEdge database using ODBC. It shows how to construct the ODBC connection string for a C# .NET client:
using System.Data;
using System.Data.Odbc;
class OdbcProvider{
static void Main(string[] args){
// Set up connection string
string connString = @"dsn=sports2000;UID=userid;PWD=password";
// Set up query string
string sql = @"select custnum , name, Balance from pub.customer where custnum < 5";
// Declare connection and data reader variables
OdbcConnection conn = null;
OdbcDataReader reader = null;
try{
// Open connection
conn = new OdbcConnection(connString);
conn.Open();
// Execute the query
OdbcCommand cmd = new OdbcCommand(sql, conn);
reader = cmd.ExecuteReader();
// Display output header
Console.WriteLine(
"This program demonstrates the use of the ODBC Data Provider."
);
Console.WriteLine(
"Database = {0} \nDriver = {1} \nQuery {2}\nConnection String = {3}\nServer Version = {4}\nDataSource = {5}",
conn.Database, conn.Driver, cmd.CommandText, conn.ConnectionString, conn.ServerVersion, conn.DataSource);
// Console.WriteLine("CustNum \tAccount\n");
// Process the result set
while (reader.Read())
{
Console.WriteLine("{0}|{1}|{2}", reader["CustNum"].ToString().PadLeft(8), reader["Name"].ToString().PadLeft(8), reader["Balance"].ToString().PadLeft(8));
}
}
catch (Exception e){
Console.WriteLine("Error: " + e);
}
finally{
// Close connection
reader.Close();
conn.Close();
}
}
}