SELECT COUNT(*)返回一个数组? (CockroachDB C#)

时间:2019-06-02 04:52:28

标签: c# sql cockroachdb

我正在将使用MySQL数据库的项目迁移到CockroachDB。在此查询中,我只是试图查看帐户表中是否存在用户名。当我在CLI中运行相同的查询时,它返回计数1。

我正在尝试在C#应用程序中获取此整数,但是我不太了解在查询中添加参数的问题。如果我的SELECT COUNT(*)查询中有一个参数,这是否总是返回一个数组而不是整数?从此查询中获取计数int的最佳方法是什么?

public bool accExists(string id, string UserName)
        {
            int totalCount = 0;
            using (var conn = new NpgsqlConnection(connstr.getConnStr(id)))
            {
                //SSL Connection Init
                conn.ProvideClientCertificatesCallback += ProvideClientCertificatesCallback;
                conn.UserCertificateValidationCallback += UserCertificateValidationCallback;
                conn.Open();
                //Check if Account Exists
                NpgsqlCommand accExists = new NpgsqlCommand("SELECT COUNT(*) FROM accounts WHERE username LIKE @username", conn);
                accExists.Parameters.AddWithValue("@username", UserName);
                //Querey Executions:
                totalCount = Convert.ToInt32(accExists.ExecuteScalar());
            }
            //Return Value
            switch (totalCount)
            {
                case 1: //Account Exists
                    return true;
                case 0: //Account Does NOT Exist
                    return false;
                default: //Protective Code - Shouldn't be hit
                    addManageLog("SYSTEM", id, "MORE THAN ONE OF THE SAME USERNAME " + UserName);
                    return false;
            }
        }

捕获到异常:System.NotSupportedException: 'Npgsql 3.x removed support for writing a parameter with an IEnumerable value, use .ToList()/.ToArray() instead'

谢谢您的帮助!

0 个答案:

没有答案