Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

c# - Cannot insert more than one row into MySQL database

I'm trying to insert few rows at one time to the MySql database. When I do it over localhost it works well. But when I run it connecting my pc and server to the for example mobile network, it doesn't insert anything to the table. Same for my home network.

This is how I insert data:

void importData()
{
   using (MySqlConnection conn = new MySqlConnection(constring))
   {
      string data = null;
      conn.Open();
      for (int i = 0; i < dataGridView1.Rows.Count; i++)
       {
         if (dataGridView1.Rows[i].Cells[0].Value != null)
         {
           data = dataGridView1.Rows[i].Cells[0].Value.ToString();
         }
         else
         {
            break;
         }
         string query = "ALTER TABLE zlecenia_dzis DROP id_zlecenia;ALTER TABLE zlecenia_dzis ADD id_zlecenia int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
          INSERT INTO zlecenia_dzis(numer_zlecenia, data_zlecenia, model) SELECT * FROM (SELECT '" + dataGridView1.Rows[i].Cells[1].Value + "', 
           STR_TO_DATE('" + data + "', '%d.%m.%Y'), '" + dataGridView1.Rows[i].Cells[2].Value + "') AS tmp WHERE NOT EXISTS( SELECT numer_zlecenia 
          FROM zlecenia_dzis WHERE numer_zlecenia='" + dataGridView1.Rows[i].Cells[1].Value + "')LIMIT 1;";
          using (MySqlCommand cmd = new MySqlCommand(query, conn))
          {
           cmd.ExecuteNonQuery();
          }
        }
     conn.Close();
   }
}
question from:https://stackoverflow.com/questions/66046017/cannot-insert-more-than-one-row-into-mysql-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...