Postgresql 在Xamarin中打开Postgres连接时返回错误

Postgresql 在Xamarin中打开Postgres连接时返回错误,postgresql,xamarin.forms,Postgresql,Xamarin.forms,我正在尝试将我的Android应用程序连接到Postgres,但似乎不起作用 异常消息为:连接时异常 这是我的密码 private void Login_Clicked(object sender, EventArgs e) { DBInterface<DBLogicInput, DBLogicResult> dbLoginLogic = new DBLoginLogic(); DBLogicInput userInp

我正在尝试将我的Android应用程序连接到Postgres,但似乎不起作用

异常消息为:
连接时异常

这是我的密码

 private void Login_Clicked(object sender, EventArgs e)
        {
            DBInterface<DBLogicInput, DBLogicResult> dbLoginLogic = new DBLoginLogic();
            DBLogicInput userInput = new DBLogicInput();
            DBLogicResult DBResult = new DBLogicResult();
            LoginModel useCredentials = new LoginModel()
            {
                userName = txtUsername.Text,
                passWord = txtPassword.Text
            };

            userInput[typeof(LoginModel).FullName] = useCredentials;

            try
            {
                DBResult = dbLoginLogic.DoProcess(userInput);
                bool userExisting = DBResult.ResultCode != DBLogicResult.RESULT_CODE_ERR_DATA_NOT_EXIST;
                if (userExisting)
                {
                    Application.Current.MainPage = new NavigationPage(new IndexPage());
                }
                else
                {

                    _ = DisplayAlert("Login Error", "User does not exist", "Ok");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
private void登录\u已单击(对象发送者,事件参数e)
{
DBInterface dbLoginLogic=新的dbLoginLogic();
DBLogicInput userInput=新的DBLogicInput();
DBLogicResult DBResult=新的DBLogicResult();
LoginModel useCredentials=新LoginModel()
{
userName=txtUsername.Text,
passWord=txtPassword.Text
};
userInput[typeof(LoginModel).FullName]=使用凭据;
尝试
{
DBResult=dbLoginLogic.DoProcess(userInput);
bool userExisting=DBResult.ResultCode!=DBLogicResult.RESULT\u代码\u错误\u数据\u不存在;
如果(用户现有)
{
Application.Current.MainPage=新导航页面(new IndexPage());
}
其他的
{
_=显示警报(“登录错误”、“用户不存在”、“确定”);
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
这是我创建用来连接数据库的类

  public abstract class DBLogic : DBInterface<DBLogicInput, DBLogicResult>
    {
        public string connectionString = "Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=proyektoNijuan";
        public DBLogicResult DoProcess(DBLogicInput inOut)
        {
            //throw new NotImplementedException();
            DBLogicResult result = default(DBLogicResult);
            NpgsqlConnection connection = null;
            NpgsqlTransaction transaction = null;
            try {
                connection = new NpgsqlConnection(connectionString);
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
                transaction = connection.BeginTransaction();
                result = Process(connection, inOut);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                transaction.Rollback();
            } finally {
                if (connection != null)
                {
                    connection.Close();
                }
            }
            return result;
        }

        protected abstract DBLogicResult Process(NpgsqlConnection conn, DBLogicInput InOuT);
    }
公共抽象类DBLogic:DBInterface
{
公共字符串connectionString=“服务器=localhost;端口=5432;用户Id=postgres;密码=postgres;数据库=proyektoNijuan”;
公共DBLogicResult数据处理(DBLogicInput inOut)
{
//抛出新的NotImplementedException();
DBLogicResult=默认值(DBLogicResult);
NpgsqlConnection=null;
NpgsqlTransaction=null;
试一试{
连接=新的NpgsqlConnection(connectionString);
if(connection.State!=System.Data.ConnectionState.Open)
{
connection.Open();
}
事务=连接。BeginTransaction();
结果=过程(连接,输入输出);
Commit();
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
transaction.Rollback();
}最后{
if(连接!=null)
{
connection.Close();
}
}
返回结果;
}
受保护的抽象DBLogicResult进程(NpgsqlConnection conn、DBLogicInput InOuT);
}
调试器点击代码
connection.Open()后存在错误

我是否应该添加一个web服务来将postgres连接到我在xamarin表单中构建的android应用程序

我只是Xamarin形式的初学者。我只是想创建一个自我应用程序。我需要一些帮助来学习一个新的编程平台

谢谢大家,大家好,


如何修复它?

嗯,我想我做错了

也许连接PostgreSQL的正确方法是使用WEB API

将该web API调用到Xamarin表单

我真的不知道它是否正确,但我会试试看


完成该WEB API的开发后,我将更新正确答案,以便其他初学者会发现此答案很有帮助。

感谢您的分享,请不要忘记接受您的答案,它将帮助其他有类似问题的人。目前,WEB API正在开发中。一旦完成。我会把它贴在这里。