简体   繁体   English

在MySQL数据库中存储数据列表?

[英]Store A List Of Data In Mysql DataBase?

i have a list of String Data i would Like to Store in my MySQL database Table in Column "Categories". 我在“类别”列中有一个要存储在我的MySQL数据库表中的字符串数据列表。

Is there a way to store it at a go because its a long list.` 有没有办法一口气存储它,因为它的清单很长。

  public Class PickerView{
  List<string> CategoriesPicker = new List<string>();
  public Button SaveItemsButton = new Button();
  public  PickerView()
    {

        CategoriesPicker.Items.Add("Hotels & Travel");
        CategoriesPicker.Items.Add("Restaurant");
        CategoriesPicker.Items.Add("Wholesalers");
        CategoriesPicker.Items.Add("Automotives");
        CategoriesPicker.Items.Add("Pets");
        CategoriesPicker.Items.Add("Musical Instruments Services");
        CategoriesPicker.Items.Add("Specialty Food");
        CategoriesPicker.Items.Add("Food");
        CategoriesPicker.Items.Add("Boutique");
        CategoriesPicker.Items.Add("Home & Gardens");
        CategoriesPicker.Items.Add("Shopping");
        CategoriesPicker.Items.Add("Education");
        CategoriesPicker.Items.Add("Books,Mags,Music & Video");
        CategoriesPicker.Items.Add("Fashion");
        CategoriesPicker.Items.Add("Event Planning & Services");
        CategoriesPicker.Items.Add("Arts & Craft");
        CategoriesPicker.Items.Add("Local Services");
        CategoriesPicker.Items.Add("NightLife(Bars..)");

        SaveItemsButton.Clicked += SavedItemsButton_Clicked
    }
    private void SavedItemsButton_Clicked(object sender, System.EventArgs e)
    {
         string sqlstring = "server=; port= ; user id =;Password= ;Database=test;";
        MySqlConnection conn = new MySqlConnection(sqlstring);
        try
        {
            conn.Open();
        }
        catch (MySqlException ex)
        {
            throw ex;
        }
        string Query = "INSERT INTO test.maintable (Categories)values('" +(**//I DONT KNOW WHAT TO WRITE HERE TO SAVE ALL AT ONCE**) + "');";
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        cmd.ExecuteReader();
        conn.Close();
    }
 }`

How do i save the list of items in CategoriesPicker in database when SaveItemsButton is clicked. 单击SaveItemsButton时,如何在数据库的CategoriesPicker中保存项目列表。

Simple use the mysql insert into statement. 简单地使用mysql insert into语句。

insert into tbl1 values (1, 'Name1', 1, null), (2, 'Name2', 2, null), (3, 'Name3', 1, null); 插入tbl1值(1,'Name1',1,null),(2,'Name2',2,null),(3,'Name3',1,null);

(3, 'Name3', 1, null) is of course the structure of tbl1 This will work in any language you use or even in comand line (3,'Name3',1,null)当然是tbl1的结构。它可以在您使用的任何语言中使用,甚至可以在命令行中使用

MySQL (and MariaDB) don't have list-valued column types since that goes against first normal form in database design. MySQL(和MariaDB) 没有列表值列类型,因为这与数据库设计中的第一种普通形式背道而驰。

What you do instead is what @nbk suggested and, using whatever database framework you chose, insert multiple rows. @nbk建议您执行的操作,然后使用您选择的任何数据库框架插入多行。 Here's a sample table definition for MariaDB/MySQL if you don't have one already: 如果您还没有一个示例表定义,那么这是MariaDB / MySQL的表定义:

Microsoft's current recommended way of interacting with a database is Entity Framework Core . Microsoft当前推荐的与数据库交互的方式是Entity Framework Core The documentation there can help you with connecting to a database, creating a table, adding rows to a table and saving all of that to the database. 那里的文档可以帮助您连接到数据库,创建表,向表中添加行并将所有这些内容保存到数据库。

Hope that helps! 希望有帮助!

I added using blocks to your code to ensure that your database objects are closed and disposed even if there is an error. 我在代码中添加了using块,以确保即使有错误,数据库对象也可以关闭和处置。

Set the query text only once, it stays the same for each iteration of the loop. 仅将查询文本设置一次,每次循环迭代时都保持相同。 Likewise the parameter is added outside the loop. 同样,将参数添加到循环外部。 Only the value of the parameter is changed inside the loop. 循环内仅更改参数的值。 We use .ExecuteNonQuery (not reader) for Insert, Update or Delete. 我们使用.ExecuteNonQuery(而非阅读器)进行插入,更新或删除。 .ExecuteReader is used for returning data. .ExecuteReader用于返回数据。

    private List<string> CategoriesPicker = new List<string>();
    //add all the items to the list
    private void SavedItemsButton_Clicked(object sender, System.EventArgs e)
    {
        string sqlstring = "server=; port= ; user id =;Password= ;Database=test;";
        using (MySqlConnection conn = new MySqlConnection(sqlstring))
        {
            string Query = "INSERT INTO test.maintable (Categories)values(@Category);";
            using (MySqlCommand cmd = new MySqlCommand(Query, conn))
            {
                cmd.Parameters.Add("@Category", MySqlDbType.VarChar);
                try
                {
                    conn.Open();
                }
                catch (MySqlException ex)
                {
                    throw ex;
                }
                foreach (String item in CategoriesPicker)
                {
                    cmd.Parameters["@Category"].Value = item;
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM