简体   繁体   English

用于在 npgsql 中插入元素的查询字符串

[英]query string for inserting elements in npgsql

I want to insert data into a row in PostgreSQL from NPGSQL, but there is something wrong with my query string.我想从 NPGSQL 将数据插入 PostgreSQL 中的一行,但是我的查询字符串有问题。 Can you please suggest modify it?你能建议修改它吗?

   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));

    }

You can do it in this way.你可以这样做。 You have forgotten to add "," between the values and also add quotes if the value is string or date.您忘记在值之间添加“,”,如果值是字符串或日期,还要添加引号。

    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