使用GPConnNet.dll的SQL Server连接

问题描述 投票:0回答:2

我正在尝试使用GP中当前登录的凭据连接到Dynamics GP SQL Server数据库。 (对于上下文http://blogs.msdn.com/b/developingfordynamicsgp/archive/2008/10/02/why-does-microsoft-dynamics-gp-encrypt-passwords.aspx

使用GPConnNet.dll文档中提供的代码,我应该能够建立连接,但对于非sa用户而言,则无法建立连接,sa和dynsa可以正常工作。我收到登录失败的SQL Server错误。

SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(connectionString);
SqlConnection sqlConn = new SqlConnection();
if (sqlConn.State != ConnectionState.Open)
{
    GPConnection.Startup();
    var gpconn = new GPConnection();
    gpconn.Init(<Key1>, <Key2>);
try
{
    sqlConn.ConnectionString = string.Format("database={0}", cb.InitialCatalog);
    gpconn.LoginCompatibilityMode = false;
    gpconn.Connect(sqlConn, cb.DataSource, cb.UserID, cb.Password);
    if (gpconn.ReturnCode != 1)
         throw new AuthenticationException("Could not authenticate with the GP credentials.");
}
catch (System.Runtime.InteropServices.SEHException)
{
    throw new AuthenticationException("Could not authenticate with the GP credentials.");
}
}

连接字符串中的信息来自Microsoft Dexterity工具包。

public class GPUser
{
    public readonly static string DataBase = Dynamics.Globals.IntercompanyId.Value;
    public readonly static string UserID = Dynamics.Globals.UserName.Value;
    public readonly static string Password = Dynamics.Globals.SqlPassword.Value;
    public readonly static string DataSource = Dynamics.Globals.SqlDataSourceName.Value;
    public readonly static string ApplicationName = string.Format("{0}{1}", App.ProductName, "(gp)");
    public static string Server
    {
        get
        {
            //Returns the Server from the ODBC DSN
        }
    }
    public static SqlConnectionStringBuilder ConnectionString
    {
        get 
        {
            return new SqlConnectionStringBuilder
            {
                DataSource = Server,
                UserID = UserID,
                Password = Password,
                ApplicationName = ApplicationName,
                InitialCatalog = DataBase
            };
        }

    }
}

用户是否需要某些东西?我缺少GPConnection代码中的某些内容吗?

谢谢

c# microsoft-dynamics dynamics-gp econnect
2个回答
0
投票

此类将检索连接所需的正确数据。

public class GPUser
{
    public readonly static string DataBase = Dynamics.Globals.IntercompanyId.Value;
    public readonly static string UserID = Dynamics.Globals.UserId.Value;
    public readonly static string Password = Dynamics.Globals.SqlPassword.Value;
    public readonly static string DataSource = Dynamics.Globals.SqlDataSourceName.Value;
    public readonly static string ApplicationName = string.Format("{0}{1}", App.ProductName, "(gp)");
    public static SqlConnectionStringBuilder ConnectionString
    {
        get 
        {
            return new SqlConnectionStringBuilder
            {
                DataSource = DataSource,
                UserID = UserID,
                Password = Password,
                ApplicationName = ApplicationName,
                InitialCatalog = DataBase
            };
        }

    }
    public readonly static short CompanyId = Dynamics.Globals.CompanyId.Value;
    public readonly static DateTime UserDate = Dynamics.Globals.UserDate.Value;
}

将GPUser.ConnectionString传递到此代码中将创建一个有效的SQLConnection对象,可用于连接到GP数据库。

SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(connectionString);
SqlConnection sqlConn = new SqlConnection();
if (sqlConn.State != ConnectionState.Open)
{
    GPConnection.Startup();
    var gpconn = new GPConnection();
    gpconn.Init(<Key1>, <Key2>);
    try
    {
        sqlConn.ConnectionString = string.Format("database={0}", cb.InitialCatalog);
        gpconn.LoginCompatibilityMode = false;
        gpconn.Connect(sqlConn, cb.DataSource, cb.UserID, cb.Password);
        if (gpconn.ReturnCode != 1)
            throw new AuthenticationException("Could not authenticate with the GP    credentials.");
    }
    catch (System.Runtime.InteropServices.SEHException)
    {
        throw new AuthenticationException("Could not authenticate with the GP credentials.");
    }
}

-1
投票

我在最后尝试了您的代码,我的代码总是返回(gpconn.ReturnCode = 2)我不确定我错过了什么,我已经验证了数据源,用户ID和密码,一切都很好,但是我是gpconn。 ReturnCode始终为return = 2。

请告诉我App.ProductName,我将应用程序名称硬编码为“ Dynamics”。

请给我您的建议。

关于,卡尔提克。

© www.soinside.com 2019 - 2024. All rights reserved.