I'm trying to develop a program that will connect to a Progress Database (9.1E) using C# (Visual Studio 2010 Ultimate), but I first need to get a connection string to the Progress Database from the C# program.
I have tried the following, but I'm unsuccessful in establishing a connection to the Progress database. I'm not sure what the connection string should look like, but here's what I have before I start expanding everything. Also, I'm not sure what the DSN name should be.
private void downloadData_Click(object sender, RoutedEventArgs e)
{
try
{
string connectString = "DSN = QADDB; Host = ipaddress; DB = dbname; UID = user; PWD = password;";
IDbConnection dbConn = new OdbcConnection(connectString);
dbConn.Open();
IDbCommand dbCommand = dbConn.CreateCommand();
string sqlstr = "SELECT pt_part FROM pt_mstr";
dbCommand.CommandText = sqlstr;
IDataReader reader = dbCommand.ExecuteReader();
while (reader.Read())
{
string part = (string)reader["pt_part"];
gridview.Items.Add(part);
}
reader.Close();
reader = null;
dbCommand.Dispose();
dbCommand = null;
dbConn.Close();
dbConn = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
The error message says:
System.Data.Odbc.OdbcException (0X80131937): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
string connectString = "DSN=QADDB;uid=username;pwd=password;host=hostname;port=port#;db=dbname;";
using (OdbcConnection dbConn = new OdbcConnection(connectString));
{
try
{
dbConn.Open();
} catch (Exception)
{
mRtnVar = "Couldn't Connect";
return mRtnVar
}
string sqlstr = string.Format(@"SELECT ""FieldName"" FROM PUB.DataBase WHERE ""FieldName"" = 'value'");
using (OdbcCommand comm = new OdbcCommand(sqlstr, dbConn))
{
using (OdbcDataReader reader = dbConn.ExecuteReader())
{
if (reader.Read())
{
mRtnVar = reader["FieldName"].ToString();
}
}
}
return mRtnVar
}
In your connection string, set the port of your Progress database:
PORT=20931;
来源:https://stackoverflow.com/questions/30870818/c-sharp-to-connect-to-a-progress-database