除非数据库表为空,否则C#INSERT命令不起作用

时间:2015-12-25 12:31:14

标签: c# sql-server sql-insert sqlcommand

我遇到了学校作业的问题。 出于某种原因,除非数据库表为空,否则我的sql INSERT命令不会被wotk。 如果它是空的,它可以正常工作,但如果我使用if那里已有条目,它会移动到我的“catch”块。

我的初始连接和从数据库读取以加载项目是成功的。 如果删除第一个“for”块,下面的代码也会成功(例如,如果我通过删除表中的所有数据并在此之后插入新数据来启动我的连接。请阅读长期评论,为什么我做了两个相同的“为“开始的块”。

如果我删除第一个“for”块并删除“DELETE”命令,它仍然不起作用(即问题不是两个相同的“for”块,而是阻止INSERT命令的其他内容从工作,除非表是空的。)

使用MessageBox.Show我已经测试了“try”块在进入“catch”块之前走了多远。我在以下代码中将// - //插入到该位置。如您所见,最后一个成功的行在INSERT命令之前。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Favorite fav;
            SqlCommand delfav = new SqlCommand("DELETE FROM tblFavorites", conn);
            SqlCommand addfav = new SqlCommand("INSERT INTO tblFavorites (Title, url) VALUES (@title, @url)", conn);
            addfav.Parameters.Add("@title", SqlDbType.NVarChar, 100);
            addfav.Parameters.Add("@url", SqlDbType.NVarChar, 300);
            try
            {
                conn.Open();
                for (int i = 0; i < favorites.Items.Count; i++)
                {
                    fav = (Favorite)favorites.Items[i];
                    addfav.Parameters["@title"].Value = fav.getsettitle;
                    addfav.Parameters["@url"].Value = fav.getseturl;
                    //--//
                    addfav.ExecuteNonQuery();
                }
                /*
                At first I didn't have the above section. The result was that each time the browser closed,
                it first deleted the database table and then if there was a problem uploading the new data,
                the old list was already gone.
                To correct for this, I first attempted to add the favorites to the list (despite the fact
                that for a short time it will have double the amount of information). If this process is
                unsuccessful, it will skip to the catch block and the original data will be saved.
                Only if the INSERT command is successful, I delete the data, then repeat the INSERT command,
                already knowing that it is successful.
                */
                delfav.ExecuteNonQuery();
                for (int i = 0; i < favorites.Items.Count; i++)
                {
                    fav = (Favorite)favorites.Items[i];
                    addfav.Parameters["@title"].Value = fav.getsettitle;
                    addfav.Parameters["@url"].Value = fav.getseturl;
                    addfav.ExecuteNonQuery();
                }
            }
            catch
            {
                DialogResult dialogResult = MessageBox.Show("There was a problem loading your favorites to the database. Do you still wish to quit? \n (All changes to the favorites list will be lost)", "", MessageBoxButtons.YesNo);
                if (dialogResult != DialogResult.Yes)
                {
                    e.Cancel = true;
                }
            }
            finally
            {
                conn.Close();
            }
        }

Thanx给你的帮助很多人!

0 个答案:

没有答案