C#SQL帮助! Insert Into正在覆盖现有数据

时间:2016-07-16 21:10:55

标签: c# sql insert-into

我在C#中学习SQL,但遇到了一些麻烦。我无法弄清楚如何在不覆盖现有数据的情况下将数据插入到我的sql表中。

我可以轻松地在表格中添加值,我甚至创建了一个方法来填充数据进行测试,并且可以轻松快速地添加500个值,但是当我再次运行它时,它会覆盖现有值而不是添加到它

这是我的代码:

public static void AddSongViaClass(Songs s)
    {
        string conStr = "TestSQL.Properties.Settings.ArtistConnectionString";
        using (SqlConnection sqlcon = new SqlConnection(GetConnectionString(conStr)))
        {

            sqlcon.Open();

            try
            {
                string query = "INSERT INTO Songs (Name, Id, Album, TrackNumber, TrackNumberCount, Genre, Rating, Tags, Subject, Categories, Comments, FileName, FolderName, FolderPath, FullPath, Length, PlayCount, SkipCount, LastPlayed)";
                query += " VALUES (@Name, @Id, @Album, @TrackNumber, @TrackNumberCount, @Genre, @Rating, @Tags, @Subject, @Categories, @Comments, @FileName, @FolderName, @FolderPath, @FullPath, @Length, @PlayCount, @SkipCount, @LastPlayed)";

                using (SqlCommand cmd = new SqlCommand(query, sqlcon))
                {
                    cmd.Parameters.Add(new SqlParameter("Name", s.Name));
                    cmd.Parameters.Add(new SqlParameter("Id", s.Id));
                    cmd.Parameters.Add(new SqlParameter("Album", s.Album));
                    cmd.Parameters.Add(new SqlParameter("TrackNumber", s.TrackNumber));
                    cmd.Parameters.Add(new SqlParameter("TrackNumberCount", s.TrackNumberCount));
                    cmd.Parameters.Add(new SqlParameter("Genre",s.Genre));
                    cmd.Parameters.Add(new SqlParameter("Rating", s.Rating));
                    cmd.Parameters.Add(new SqlParameter("Tags", s.Tags));
                    cmd.Parameters.Add(new SqlParameter("Subject", s.Subject));
                    cmd.Parameters.Add(new SqlParameter("Categories", s.Categories));
                    cmd.Parameters.Add(new SqlParameter("Comments", s.Comments));
                    cmd.Parameters.Add(new SqlParameter("FileName", s.FileName));
                    cmd.Parameters.Add(new SqlParameter("FolderName", s.FolderName));
                    cmd.Parameters.Add(new SqlParameter("FolderPath", s.FolderPath));
                    cmd.Parameters.Add(new SqlParameter("FullPath", s.FullPath));
                    cmd.Parameters.Add(new SqlParameter("Length", s.Length));
                    cmd.Parameters.Add(new SqlParameter("PlayCount", s.PlayCount));
                    cmd.Parameters.Add(new SqlParameter("SkipCount", s.SkipCount));
                    cmd.Parameters.Add(new SqlParameter("LastPlayed", s.LastPlayed));

                    cmd.ExecuteNonQuery();
                    sqlcon.Close();

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Count not insert. {0}", s.Name);
                Console.WriteLine("Error Message {0}", ex.Message);
            }


        }
    }

如果有人可以告诉我我需要做什么才能简单地添加到现有表而不会覆盖它,任何帮助都会被大量占用。谢谢!

如果你很好奇,这是我快速填充表格的方法:

 public static void JustDoIt()
    {
        Songs s = new Songs();
        for (int i = 1; i <= 500; i++)
        {

            s.Name = "My Song Name " + i.ToString("D3");
            s.Id = i.ToString();
            s.Album = "My Song's Album " + i.ToString("D3");
            s.TrackNumber = (i%13 == 0 ? 1: i%13).ToString("D3");
            s.TrackNumberCount = "12";
            s.Genre = "Something";
            s.Rating = (i%20/100*i).ToString("D3");
            s.Tags = "Tags " + i.ToString("D3") + " and more tags";
            s.Subject = "Subjects";
            s.Categories = "Categories";
            s.Comments = "Comments";
            s.FileName = "Ashes Remain - Christmas  - 02 - Gift of Love.mp3";
            s.FolderName = "My folder name";
            s.FolderPath = @"L:\Dropbox\Music\00- Sync'd Music\Ashes Remain";
            s.FullPath = s.FolderName + "\\" + s.FolderPath;
            s.Length = "3:" + i/60;
            s.PlayCount = "12";
            s.SkipCount = "1";
            s.LastPlayed = DateTime.Now.ToString();

            SQL_AddNewRecord.AddSongViaClass(s);
            Console.WriteLine(i);

        }
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

试一试:

            using (SqlCommand cmd = new SqlCommand(query, sqlcon))
            {
                cmd.Parameters.Add(new SqlParameter("Name", s.Name));
                cmd.Parameters.Add(new SqlParameter("Id", s.Id));
                cmd.Parameters.Add(new SqlParameter("Album", s.Album));
                cmd.Parameters.Add(new SqlParameter("TrackNumber", s.TrackNumber));
                cmd.Parameters.Add(new SqlParameter("TrackNumberCount", s.TrackNumberCount));
                cmd.Parameters.Add(new SqlParameter("Genre", s.Genre));
                cmd.Parameters.Add(new SqlParameter("Rating", s.Rating));
                cmd.Parameters.Add(new SqlParameter("Tags", s.Tags));
                cmd.Parameters.Add(new SqlParameter("Subject", s.Subject));
                cmd.Parameters.Add(new SqlParameter("Categories", s.Categories));
                cmd.Parameters.Add(new SqlParameter("Comments", s.Comments));
                cmd.Parameters.Add(new SqlParameter("FileName", s.FileName));
                cmd.Parameters.Add(new SqlParameter("FolderName", s.FolderName));
                cmd.Parameters.Add(new SqlParameter("FolderPath", s.FolderPath));
                cmd.Parameters.Add(new SqlParameter("FullPath", s.FullPath));
                cmd.Parameters.Add(new SqlParameter("Length", s.Length));
                cmd.Parameters.Add(new SqlParameter("PlayCount", s.PlayCount));
                cmd.Parameters.Add(new SqlParameter("SkipCount", s.SkipCount));
                cmd.Parameters.Add(new SqlParameter("LastPlayed", s.LastPlayed));

                cmd.ExecuteNonQuery();
                for (int i = 1; i <= 500; i++)
                {
                    cmd.Parameters["Name"].Value = "My Song Name " + i.ToString("D3");
                    cmd.Parameters["Id"].Value = i.ToString();
                    ...All parameters...
                    cmd.ExecuteNonQuery();
                }

                sqlcon.Close();