c# - 如何使用 C# 在 Postgres 中保存增量值

标签 c# asp.net postgresql dotnetnuke monodevelop

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

现在这是我的递减代码: 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 将增量值保存在数据库中。我希望每当值增加、减少或更新时,它都会保存到数据库中。

如果有人能提供帮助,我将不胜感激。提前谢谢你。

最佳答案

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

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

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

<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 .

关于c# - 如何使用 C# 在 Postgres 中保存增量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560752/

相关文章:

php - echo json_encode($return_data, JSON_NUMERIC_CHECK);数字中的字符 'e' 不起作用

python - 在 Django 1.10 中使用 Postgres 搜索时出现 NotImplementedError

c# - 为什么通过反射找不到IList类型上的GetEnumerator()方法?

asp.net - MVC4 IEnumerable 模型绑定(bind)始终为 null

javascript - Web 服务在 chrome 中工作正常,但在 firefox 和 IE 中不工作

c# - 与野田时间的日期差是正确的吗?

c# - 使用 HeatDirectory 从 wix 中的多个目录中收集文件

c# - 如何序列化列表?

c# - 访问在 C#.net 中的 webbrowser 控件中呈现的帧

php - 运行 Laravel 的 artisan session 时出错 :table with pgSQL