简体   繁体   中英

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".

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.

Simple use the mysql insert into statement.

insert into tbl1 values (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

MySQL (and MariaDB) don't have list-valued column types since that goes against first normal form in database design.

What you do instead is what @nbk suggested and, using whatever database framework you chose, insert multiple rows. Here's a sample table definition for MariaDB/MySQL if you don't have one already:

Microsoft's current recommended way of interacting with a database is 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.

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. .ExecuteReader is used for returning data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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