在 Blazor、C# 和 Postgresql 中,我无法使用 Blazor EditForm 进行 INSERT INTO

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

在 Blazor EditForm 中,我使用 C# 和 Postgresql 从 EditForm (Blazor) 插入数据 进入数据库表。

但由于某种原因它不批准该数据。相反,C# 脚本有一条错误消息,指出

所以基本上我想要完成的是使用 EditForm 将数据插入 Postgres 数据库表。

我使用添加到 EditForm 的 Model 属性以及带有 @bind-Value 的 InputText,如代码中所示。

这是 Razor 文件

@page "/register"
@using Login.Models;
@using Npgsql;
@inject NavigationManager NavigationManager

<PageTitle>Register</PageTitle>

<EditForm method="post" Model="Register2" OnValidSubmit="RegisterForm" FormName="Register" Enhance>
    <label for="username">Username:</label>
    <InputText id="username" @bind-Value="Register2.username"/>
    <label for="email">Email:</label>
    <InputText id="email" @bind-Value="Register2.email"/>
    <label for="password">Password</label>
    <InputText type="password" id="password" @bind-Value="Register2.password"/>
    <button type="submit" class="btn btn-primary">Register</button>
</EditForm>

@code
{
    [SupplyParameterFromForm] private Register2 Register2 { get; set; } = new();
    
    private string connectionString;

    public string? username { get; } = "";
    public string? password { get; } = "";
    public string? email { get; } = "";
    
    private string localhost = "localhost";

    private string user = "hidden";

    private string pass = "hidden"

    private string db = "hidden";

    private string tbl = "hidden";

    public async Task RegisterForm()
    {
        connectionString = $"host={localhost};username={user};password={pass};database={db}";
        using var connection = new NpgsqlConnection(connectionString);
        connection.Open();
        var register = $@"insert into {tbl}(username, email, password) values(@username, @email, @password)";
        var cmd = new NpgsqlCommand(register, connection);
        cmd.Parameters.AddWithValue("@username", username);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@password", password);
        cmd.ExecuteNonQuery();
        NavigationManager.NavigateTo("/User");
    }
}

这是C#脚本

namespace Login.Models;
using Npgsql;

public class Register2
{
    private string connectionString;

    public string username { get; set; }
    public string password { get; set; }
    public string email { get; set; } = "";
    
    private string localhost = "localhost";

    private string user = "postgres";

    private string pass = "NewPass123";

    private string db = "individual_assignment_db";

    private string tbl = "users";
    
    //Summarized you basically validate input with two form fields...
    public Register2()
    {
        connectionString = $"host={localhost};username={user};password={pass};database={db}";
        using var connection = new NpgsqlConnection(connectionString);
        connection.Open();
        var register = $@"insert into {tbl}(username, email, password) values(@username, @email, @password)";
        var cmd = new NpgsqlCommand(register, connection);
        cmd.Parameters.AddWithValue("@username", "Nick");
        cmd.Parameters.AddWithValue("@email", "[email protected]");
        cmd.Parameters.AddWithValue("@password", "abcdefgh");
        cmd.ExecuteNonQuery();
    }    
}
c# postgresql blazor blazor-editform
1个回答
0
投票

根据此GitHub问题,根本原因可能是未推断参数的类型。

一:指定类型:

您应该明确指定参数的类型。有多种方法可以实现:
一种方法是:

cmd.Parameters.Add(new NpgsqlParameter("@username", NpgsqlTypes.NpgsqlDbType.Varchar) { Value = "Nick" });

二:使用DbNull,而不是null!

正如这里提到的,

要表示空参数值,请将 Value 设置为

DBNull.Instance
,而不是空。 您应该使用
DbNull
来分配空值:

这里有一个实用的扩展方法,它可以为你做这两件事:

private static NpgsqlParameter safeNpgsqlParameter<T>( string parameterName, T? data ) where T : struct => data switch { decimal => new NpgsqlParameter() { ParameterName = parameterName, NpgsqlDbType = NpgsqlDbType.Numeric, Value = data }, Enum => new NpgsqlParameter() { ParameterName = parameterName, Value = data }, null => new NpgsqlParameter() { ParameterName = parameterName, Value = DBNull.Value }, LocalDate => new NpgsqlParameter() { ParameterName = parameterName, NpgsqlDbType = NpgsqlDbType.Date, Value = data }, _ => throw new DbUpdateException() };
    
© www.soinside.com 2019 - 2024. All rights reserved.