Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Modificare DataTable e aggiornarlo sul database

    Ciao io ho un oggetto DataTable che ho precedentemente riempito con il metodo Fill() della classe SqlDataAdapter con dei dati provenienti da un database. Ora io vorrei modificare i valori presenti in alcune colonne di alcune righe e aggiornare la tabella sul database con il metodo Update() dell'SqlDataAdapter. Come posso fare? Grazie!!!
    Lunga vita e prosperità!!
    Usa Mozilla! http://www.mozilla.com

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,887
    Osserva questo codice...
    http://blogs.msdn.com/spike/archive/...ql-server.aspx


    codice:
    static void Main(string[] args)
    
            {
    
                string cs = @"Data Source=<your server>;Initial Catalog=<your database>;Integrated Security=SSPI";
    
                string sql = "SELECT id, fname, lname FROM TestTable";
    
     
    
                using (SqlConnection con = new SqlConnection(cs))
    
                {
    
                    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    
                    DataTable dt = new DataTable("Persons");
    
                    da.Fill(dt);
    
     
    
                    Console.WriteLine("Rows are untouched...");
    
                    ViewDataTableRowStatus(dt);
    
     
    
                    // Modify a row.
    
                    Console.WriteLine("Modifying the first row...");
    
                    dt.Rows[0][1] = "Jake";
    
                    ViewDataTableRowStatus(dt);
    
     
    
                    // Add a row.
    
                    Console.WriteLine("Adding a row");
    
                    dt.Rows.Add(new object[]{100, "Paul", "Paulson..."});
    
                    ViewDataTableRowStatus(dt);
    
     
    
                    // Now, if calling AcceptChanges() all rows will be set to Unchanged.
    
                    Console.WriteLine("Calling AcceptChanges()...");
    
                    dt.AcceptChanges();
    
                    ViewDataTableRowStatus(dt);
    
     
    
                    // This means that when we call update on the DataAdapter, it will return 0
    
                    // since no rows has actuall been sent to the database. (first we need to build the INSERT and UPDATE commands)
    
                    SqlCommandBuilder builder = new SqlCommandBuilder(da);
    
                    da.InsertCommand = builder.GetInsertCommand();
    
                    da.UpdateCommand = builder.GetUpdateCommand();
    
                    int rows = da.Update(dt);
    
                    Console.WriteLine("Updated rows: {0}", rows);
    
                }
    
            }
    
     
    
            private static void ViewDataTableRowStatus(DataTable dt)
    
            {
    
                foreach (DataRow dr in dt.Rows)
    
                {
    
                    Console.WriteLine("Id:{0}\tFirstName: {1}\tRowState: {2}", dr[0].ToString(), dr[1].ToString(), dr.RowState.ToString());
    
                }
    
                Console.WriteLine();
    
            }


  3. #3
    Ciao ho provato a fare come nell'esempio ma non funziona. Ti posto il codice.

    codice:
    SqlCommandBuilder Bldr = new SqlCommandBuilder(Adptr);
    Adptr.UpdateCommand = Bldr.GetUpdateCommand();
    Adptr.Update(tb);
    Il server restituisce questo errore:
    codice:
    Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
    Come posso fare??
    Lunga vita e prosperità!!
    Usa Mozilla! http://www.mozilla.com

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.