C# to connect to a Progress Database

冷暖自知 提交于 2020-01-03 03:01:18

问题


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


回答1:


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
}      



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!