簡體   English   中英

用於在 npgsql 中插入元素的查詢字符串

[英]query string for inserting elements in npgsql

我想從 NPGSQL 將數據插入 PostgreSQL 中的一行,但是我的查詢字符串有問題。 你能建議修改它嗎?

   public IActionResult Create(string item_name, string item_count, string item_size)
    {
        using var connection = new NpgsqlConnection(connString);
        connection.Open();
        string query = @"INSERT INTO public.""items""(""item_count"",""item_name"",""item_size"")VALUES ('"+item_count+item_count+item_count+"')";
        using var command = new NpgsqlCommand(query, connection);
        int result = command.ExecuteNonQuery();



        if (result < 0)
        {
            return Error();
        }
        return View(nameof(Create));

    }

你可以這樣做。 您忘記在值之間添加“,”,如果值是字符串或日期,還要添加引號。

    public IActionResult Create(string item_name, string item_count, string item_size)
    {
        using var connection = new NpgsqlConnection(connString);
        connection.Open();
        string query =  String.Format(@"INSERT INTO public.""items""(""item_count"",""item_name"",""item_size"")VALUES('{0}','{1}','{2}');" , item_count , item_count ,item_count);
        using var command = new NpgsqlCommand(query, connection);
        int result = command.ExecuteNonQuery();



        if (result < 0)
        {
            return Error();
        }
        return View(nameof(Create));

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM