none
access to datacontext.table RRS feed

  • Question

  • Hello

    I have data like this

    int Number = 2;
    string Name = "smith";
    
    


    in database i have the same columns and i use ado.net entities for access database

    for access data base in current project i write this code

    CDBEntities db = new CDBEntities();
    

    in db i have tbluser like this

    db.tbluser

    ok

    now i want fill column in database after

    db.SaveChanges();
    
    thanks


    Hossein Aftabi http://www.internationalsell.com
    Wednesday, July 6, 2011 9:42 AM

Answers

  • I do it

    it was god for me but i have a problem with store data i have about 300000 Records and when i do it it get around one hour

    how can i do it faster

    it's my code

     using(StreamReader astr = new StreamReader(ofd.OpenFile()))
                {
                  string line;
                  while ((line =astr.ReadLine())!= null)
                  {
                    db = new CDBEntities();
                                    
                    ama.dur = "";
                    ama.fn = "";
                    ama.n_cld = "";
                    ama.n_clg = "";
                    ama.tarf = "";
                    
                    
                    textBlock2.Text ="Start read data time"+ DateTime.Now.ToShortTimeString();
                     if (line.Contains("blk_no"))
                     {
                       int index_fn = line.IndexOf("fn");
                       fn = line.Substring(index_fn + 3, 6);
                       
                       ama.fn = fn;
                      // textBlock2.Text +="\n"+"fn = " +fn;
                       found++;
                       //exline = found;
                     }
                     if (line.Contains("n_clg") && line.Contains("n_cld"))
                     {
                       int index_clg = line.LastIndexOf("n_clg");
                       n_clg = line.Substring(index_clg+6,10);
                       ama.n_clg = n_clg;
    
                       int index_cld = line.IndexOf("n_cld");
                       n_cld= line.Substring(index_cld + 6, 11);
                       ama.n_cld = n_cld;
    
                       //textBlock2.Text += "\n" + "n_clg = " + n_clg + " " + "n_cld = " +n_cld;
                     }
                     if (line.Contains("date") && line.Contains("tm") && line.Contains("dur"))
                     {
                       int index_date = line.IndexOf("date");
                       n_date = line.Substring(index_date + 5,8);
                       ama.c_date =DateTime.Parse(n_date);
    
                       int index_tm = line.IndexOf("tm");
                       n_time = line.Substring(index_tm + 3, 8);
                       ama.tm =TimeSpan.Parse(n_time);
    
                       int index_dur = line.IndexOf("dur");
                       n_dur = line.Substring(index_dur +4, 7);
                       ama.dur = n_dur;
    
                       //textBlock2.Text += "\n" + "date = " + n_date + " " + "tm = "+n_time+ " "+"dur = "+n_dur;
    
                     }
                     if (line.Contains("tarf"))
                     {
                       int index_tarf = line.IndexOf("tarf");
                       n_traf = line.Substring(index_tarf + 5, 7);
                       ama.tarf = n_traf;
                       
                      // textBlock2.Text += "\n" + "n_traf = " + n_traf;
                     }
                     
                    
                     if (fn!= null && n_cld!= null && n_date != null && n_dur!=null&&
                       n_clg!= null && n_traf!= null&& n_time!= null)
                     {
                       tbl_ama ara = new tbl_ama
                       {
                         c_date =DateTime.Parse(n_date),
                         fn = fn,
                         dur = n_dur,
                         n_cld = n_cld,
                         n_clg = n_clg,
                         tarf = n_traf,
                         tm =TimeSpan.Parse(n_time)
                       };
                       db.tbl_ama.AddObject(ara);
                       db.SaveChanges();
                       fn = null;
                       n_cld = null;
                       n_clg = null;
                       n_date = null;
                       n_dur = null;
                       n_time = null;
                       n_traf = null;
                       db.AcceptAllChanges();
                     }
                    
                  }
    textBlock2.Text +="\nتNumber of records"+ found.ToString();
    textBlock2.Text+="\nfinish time "+DateTime.Now.ToShortTimeString();


    if you want i can send all my project to you .

    thanks

     


    Hossein Aftabi http://www.internationalsell.com
    • Marked as answer by H.Aftabi Monday, July 18, 2011 5:58 PM
    Friday, July 8, 2011 6:12 PM
  • Hello again,

    AFAIK, Entity Framework inserts entities one by one, whether that is through the view or stored procedure. If you want to inset large rows, I suggest you can check ADO.NET (DbCommand, etc.) which should define an SQL statement or stored procedure to execute. Please check this document for more methods about DbCommand. http://msdn.microsoft.com/en-us/library/system.data.common.dbcommand.aspx

    Please feel free to let me know if you have any questions.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, July 11, 2011 7:58 AM

All replies

  • Hello,

    Thank you for posting.

    From your description, I do not really understand why would you want to fill column in database after SaveChanges method. Based on my understanding, would you want to insert new data into database? If I misunderstood, please feel free to elaborate your question kindly.

    I made a test on my side as follows. If you inserted data after SaveChanges method, it will not be saved into database.

    using (XueyunEntities db = new XueyunEntities())
    {
       Parent p = new Parent
       {
          endDate = null,
          id = 10,
          ParentName = "Larcolais",
          Price = 120,
          startDate = 3
       };
    
    
    db.Parents.AddObject(p);
    
    db.SaveChanges();
    //p2 represent second data will be inserted into database after SaveChanges, but cannot be successful.
    //db.Parents.AddObject(p2);
    var query = db.Parents.ToList();
    }
    

    If you want to fill column or insert data into database, you just like the above code snippet executed AddObject after SaveChanges method, but they weren't saved into database before it called SaveChange.

    Hope this helps.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by H.Aftabi Thursday, July 7, 2011 7:55 PM
    • Unmarked as answer by H.Aftabi Friday, July 8, 2011 6:12 PM
    Thursday, July 7, 2011 7:47 AM
  • hello and thanks for your answer

    it's Ok , you ask me about save data after SaveChanges()

    now i want just save data in database ,

    Ok

    i do the same method that you told

    this method is in a While loop and in first time it run but in second time when pointer arrived in 

    db.AddTotbl_ama(ama);
    

     


    again i get an error that

     

    An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state.

     


    how can i remove ama from db after saveChanges() method

    and in next if i want save data about 300000 time in While loop ! can do it without Threading or i must use Threading

    and if i must use Threading please write a simple code that i set SaveChanges() in Threading

    thanks again

     


    Hossein Aftabi http://www.internationalsell.com
    Thursday, July 7, 2011 8:02 AM
  • Glad to hear it helps. If you have any question in the later, welcome to post back.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 8, 2011 2:30 AM
  • I do it

    it was god for me but i have a problem with store data i have about 300000 Records and when i do it it get around one hour

    how can i do it faster

    it's my code

     using(StreamReader astr = new StreamReader(ofd.OpenFile()))
                {
                  string line;
                  while ((line =astr.ReadLine())!= null)
                  {
                    db = new CDBEntities();
                                    
                    ama.dur = "";
                    ama.fn = "";
                    ama.n_cld = "";
                    ama.n_clg = "";
                    ama.tarf = "";
                    
                    
                    textBlock2.Text ="Start read data time"+ DateTime.Now.ToShortTimeString();
                     if (line.Contains("blk_no"))
                     {
                       int index_fn = line.IndexOf("fn");
                       fn = line.Substring(index_fn + 3, 6);
                       
                       ama.fn = fn;
                      // textBlock2.Text +="\n"+"fn = " +fn;
                       found++;
                       //exline = found;
                     }
                     if (line.Contains("n_clg") && line.Contains("n_cld"))
                     {
                       int index_clg = line.LastIndexOf("n_clg");
                       n_clg = line.Substring(index_clg+6,10);
                       ama.n_clg = n_clg;
    
                       int index_cld = line.IndexOf("n_cld");
                       n_cld= line.Substring(index_cld + 6, 11);
                       ama.n_cld = n_cld;
    
                       //textBlock2.Text += "\n" + "n_clg = " + n_clg + " " + "n_cld = " +n_cld;
                     }
                     if (line.Contains("date") && line.Contains("tm") && line.Contains("dur"))
                     {
                       int index_date = line.IndexOf("date");
                       n_date = line.Substring(index_date + 5,8);
                       ama.c_date =DateTime.Parse(n_date);
    
                       int index_tm = line.IndexOf("tm");
                       n_time = line.Substring(index_tm + 3, 8);
                       ama.tm =TimeSpan.Parse(n_time);
    
                       int index_dur = line.IndexOf("dur");
                       n_dur = line.Substring(index_dur +4, 7);
                       ama.dur = n_dur;
    
                       //textBlock2.Text += "\n" + "date = " + n_date + " " + "tm = "+n_time+ " "+"dur = "+n_dur;
    
                     }
                     if (line.Contains("tarf"))
                     {
                       int index_tarf = line.IndexOf("tarf");
                       n_traf = line.Substring(index_tarf + 5, 7);
                       ama.tarf = n_traf;
                       
                      // textBlock2.Text += "\n" + "n_traf = " + n_traf;
                     }
                     
                    
                     if (fn!= null && n_cld!= null && n_date != null && n_dur!=null&&
                       n_clg!= null && n_traf!= null&& n_time!= null)
                     {
                       tbl_ama ara = new tbl_ama
                       {
                         c_date =DateTime.Parse(n_date),
                         fn = fn,
                         dur = n_dur,
                         n_cld = n_cld,
                         n_clg = n_clg,
                         tarf = n_traf,
                         tm =TimeSpan.Parse(n_time)
                       };
                       db.tbl_ama.AddObject(ara);
                       db.SaveChanges();
                       fn = null;
                       n_cld = null;
                       n_clg = null;
                       n_date = null;
                       n_dur = null;
                       n_time = null;
                       n_traf = null;
                       db.AcceptAllChanges();
                     }
                    
                  }
    textBlock2.Text +="\nتNumber of records"+ found.ToString();
    textBlock2.Text+="\nfinish time "+DateTime.Now.ToShortTimeString();


    if you want i can send all my project to you .

    thanks

     


    Hossein Aftabi http://www.internationalsell.com
    • Marked as answer by H.Aftabi Monday, July 18, 2011 5:58 PM
    Friday, July 8, 2011 6:12 PM
  • Hello again,

    AFAIK, Entity Framework inserts entities one by one, whether that is through the view or stored procedure. If you want to inset large rows, I suggest you can check ADO.NET (DbCommand, etc.) which should define an SQL statement or stored procedure to execute. Please check this document for more methods about DbCommand. http://msdn.microsoft.com/en-us/library/system.data.common.dbcommand.aspx

    Please feel free to let me know if you have any questions.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, July 11, 2011 7:58 AM