views:

139

answers:

3

Hello everyone,

When I call the datacontextInstance.Insertonsubmit() and datacontextinstance.submitChanges(), it clears all the existing data in the database before inserting the new data.

How do I perform a real insert operation?

I want to add new data to the existing table without clearing out the existing data.

Thanks

Edit:

Here's my test code which i tried...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            DataClasses1DataContext entities = new DataClasses1DataContext();
            for (int i = 1; i <= 100; i++)
            {
                textdata dt = new textdata();
                dt.id = i;
                dt.ipaddress = "172.168.3.2";
                dt.pcname = "testusr";
                dt.publicip = "test pub ip";
                dt.username = "testusr";
                dt.textdata1 = "Some DATA";
                dt.dttime = DateTime.Now;
                entities.textdatas.InsertOnSubmit(dt);
                entities.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            }
            foreach (textdata dtdata in entities.textdatas)
            {
                Console.WriteLine(dtdata.dttime.Value.ToString());

            }
            Console.ReadLine();


        }
    }
}

I do change the loop from 100 to 200,300 to 400 etc before i run my app. But,the new records appear in the database and the old records are gone.

Edit again:

Here's my App.Config...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="ConsoleApplication2.Properties.Settings.Database1ConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
+1  A: 

Unless you've also retrieved all the data and called DeleteOnSubmit for each of the retrieved elements, it shouldn't be deleting any data when you do an InsertOnSubmit. Items that are inserted result in SQL insert statements being generated when you call SubmitChanges. The only way that I know of to get a delete statement is to call DeleteOnSubmit. Perhaps, you are thinking that you need to remove the items from the table before calling submit changes to reduce the amount of data pushed back to the server. This isn't correct. Only the new or changed data will be sent back -- and calling DeleteOnSubmit will force the data to be removed when SubmitChanges is called.

tvanfosson
I've posted my code....
Josh
+1  A: 

Can you just post your insert method? I think the problem is from your code not VS2010.

Ramezanpour
posted my full code
Josh
+1  A: 

My guess is that id is the key in database, but it is not autogenerated. so when you create your new entities and assign them keys that already exist in db - db entries just get overriden by new ones.

Alexander Taran