【问题标题】:How to Save Incremented value in Postgres using C#如何使用 C# 在 Postgres 中保存增量值
【发布时间】:2015-07-22 10:39:35
【问题描述】:

我有一个 DNN 应用程序,并且我设法放置了一个自定义模块。该模块基本上是在标签中增加和减少值。还有一个名为 submit 的按钮,它只在标签中显示一个正值,这也是正在递增和递减的值。目的只是为了计算用户在应用程序中的下载量。

现在这是我的减量代码: public void doDecrement(string propertyKey, UserInfo userInformation, int portalID) { int newDownloadCount = GetProfilePropertyValueAsInt(propertyKey, userInformation); ; 整数结果 = 0; 结果 = newDownloadCount - 1; userInformation.Profile.SetProfileProperty(propertyKey, result.ToString()); UserController.UpdateUser(portalID, userInformation); }

这是我的增量代码:

public void doIncrement(string propertyKey, UserInfo userInformation, int portalID)
{
    int newDownloadCount = GetProfilePropertyValueAsInt(propertyKey, userInformation);
    int result = 0; //initialize to zero
    result = newDownloadCount + 1; //assign new value
                                   //set the profile property to RAM... no need to test because Get will default to zero 
    userInformation.Profile.SetProfileProperty(propertyKey, result.ToString());
    //call update user to write the changes to disk
    UserController.UpdateUser(portalID, userInformation);
}

这是我的提交代码,因此我可以更新值:

public void UpdatePropertyValue(string propertyKey, UserInfo userInformation, int portalID, string txtNewVale)
{
    int newDownloadCount = 0;
    int resultValue = 0;
    int.TryParse(txtNewVale, out newDownloadCount);
    if (String.IsNullOrEmpty(txtNewVale))
    {
        newDownloadCount = resultValue;
    }
    userInformation.Profile.SetProfileProperty(propertyKey, newDownloadCount.ToString());
    UserController.UpdateUser(portalID, userInformation);
}

我现在的问题是,我遇到了路障。我想使用 postgres 将增量值保存在数据库中。我希望每当值增加、减少或更新时,它都会保存到数据库中。

我非常感谢任何可以提供帮助的人。提前谢谢你。

【问题讨论】:

    标签: c# asp.net postgresql dotnetnuke monodevelop


    【解决方案1】:

    您可能知道,DNN 的数据访问层是为 MS SQL Server 编写的。没有对 Postgres 或 mySQL 等开源数据库的官方支持。但是,您可以编写将连接到外部数据库的代码。这比较困难,但可以做到。

    由于您已经在用户配置文件中更新了增量值(存储在 DNN SQL 数据库中),您需要编写另一个方法来获取用户配置文件值并更新外部数据库。

    从下载.NET Postgres data driver 开始。在 DNN web.config 中添加一个连接字符串作为应用密钥。

    <appSettings>
    ...
        <add key="PostgresDBConn" value="Server=my-postgres-svr;Port=9090;User Id=dbuser;Password=abc123;Database=dnnExternalTables;" />
    ...
    </appSettings>
    

    然后使用以下内容检索模块代码中的连接字符串:

    string connstring = DotNetNuke.Common.Utilities.Config.GetSetting("PostgresDBConn");
    NpgsqlConnection conn = new NpgsqlConnection(connstring);
    conn.Open();
    

    然后关注Npgsql example for doing an update like this one

    【讨论】:

    • 您提供的链接目前已损坏。是否有指向该示例的新链接?
    猜你喜欢
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多