如何在c#中使用循环插入查询

时间:2015-05-31 03:55:51

标签: c# mysql loops

我是一名职业高中学生和新手程序员。 我在插入查询中使用循环有一些问题.. 我有两个表,Book和Book_detail,与外键的链接,我想将数据保存到两个表,问题是每次我添加一本书,我有一个名为copies_txt的文本框,这个想法是我想要保存book_detail表中的book_id和相同的location_id相同,但是我使用不同的boookdetail_id和我在拷贝_txt中插入的数量一样多,并实现了我尝试使用for循环,但我给了我错误

  

重要主要

的重复条目

,这是我的完整代码

int copies = Convert.ToInt32(copies_txt.Text);                      
        int i;


        string constring = "datasource=localhost;port=3306;username=root;password=root";
        string Query = "insert into simbada_perpustakaan.book(book_id,title,release_date,genre,author,copies,create_by,create_date) values ('" + this.book_id.Text + "','" + this.title_txt.Text + "','" + this.time.Text + "','" + this.genre_txt.Text + "','" + this.author_txt.Text + "','" + this.copies_txt.Text + "','" + this.username_lbl.Text + "','" + DateTime.Now.ToString("yyyy/MM/dd/hh/mm/ss") + "') ; ";
        string Query2 = "insert into simbada_perpustakaan.book_detail(id_bookdetail,book_id,location_id) values('" + Guid.NewGuid() + "','" + this.book_id.Text + "','" + this.textBox1.Text + "') on duplicate key update id_bookdetail=(id_bookdetail=('"+Guid.NewGuid()+"'))";            
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlCommand cmdDataBase2 = new MySqlCommand(Query2, conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();
            conDataBase.Close();                
            for (i = 0; i <= copies; i++)
            {
                conDataBase.Open();
                myReader = cmdDataBase2.ExecuteReader();
                conDataBase.Close();              

            }              
            MessageBox.Show("saved"); 
            while (myReader.Read())
            {

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

有人可以指出我的错误......谢谢。

2 个答案:

答案 0 :(得分:0)

让,

first Guid.NewGuid() = 'zxcvbnm'

second Giud.NewGuid() = 'asdfghjkl'

book_id = 2

location_id = 3

执行Query2后,我们得到像

这样的字符串
insert into book_detail(id_bookdetail,book_id,location_id) values('zxcvbnm','2'
,'3') on duplicate key update id_bookdetail='asdfghjkl';

当我们尝试在for循环中执行它时

When i = 0

它将数据精确地插入id_bookdetail('zxcvbnm') is unique.

When i = 1

它还可以很好地插入数据,因为id_bookdetail('zxcvbnm')之前已经使用过,但是使用了唯一的重复键id_bookdetail set a new value('asdfghjkl')

But When i = 2

它会给你错误,因为首先他将id_bookdetaidid('zxvbnm')作为重复,然后他尝试使用second one('asdfghjkl'),这也是重复的。所以它给你这个错误。

答案 1 :(得分:0)

我认为您的问题在于第二个查询。在循环内移动查询并检查。此外,您不需要数据阅读器,因为它是一个选择语句。您还可以避免多个数据库打开和关闭语句。请尝试下面的代码

int copies = Convert.ToInt32(copies_txt.Text);                      
        int i;
        string constring = "datasource=localhost;port=3306;username=root;password=root";
        string Query = "insert into simbada_perpustakaan.book(book_id,title,release_date,genre,author,copies,create_by,create_date) values ('" + this.book_id.Text + "','" + this.title_txt.Text + "','" + this.time.Text + "','" + this.genre_txt.Text + "','" + this.author_txt.Text + "','" + this.copies_txt.Text + "','" + this.username_lbl.Text + "','" + DateTime.Now.ToString("yyyy/MM/dd/hh/mm/ss") + "') ; ";

        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);

        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
           cmdDataBase.ExecuteNonquery();

            for (i = 0; i <= copies; i++)
            {

              string a = Guid.NewGuid().ToString();
              string Query2 = "insert into simbada_perpustakaan.book_detail(id_bookdetail,book_id,location_id) values('" + a + "','" + this.book_id.Text + "','" + this.textBox1.Text + "') on duplicate key update id_bookdetail=(id_bookdetail=('"+a+"'))"; 
               MySqlCommand cmdDataBase2 = new MySqlCommand(Query2, conDataBase);
               cmdDataBase2.ExecuteNonQuery();


            }              
            MessageBox.Show("saved"); 
            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

也总是尝试进行参数化查询。这可以避免sql注入问题。