| Marco Rossi |
Update Database
Ciao!
Nelle varie prove che faccio su Ado.Net mi imbatto sempre in problemi troppo grandi per me! Sto provando l'aggiornamento dei dati su un DB di SQLServer. Ho preso in considerazione due tabelle di Northwind (Categories Products), le ho riportate all'interno di un DataSet attraverso il command di select, e mi sono costruito i Command di Insert per entrambe le tabelle. Inoltre ho anche messo in relazione le due tabelle nel Dataset. Ho fatto le seguenti prove: 1. Inserimento di un nuovo record nella tabella Categories: RIUSCITO! 2.Inserimento di un nuovo record nella tabella Products: NON RIUSCITO (System error) 3. Inserimento diun nuovo record nella tabella Categories e di un record correlato nella tabella Products: ovvimente come prima NON RIUSCITO Inserisco qui sotto il codice! (se qlc mi dà un consiglio....!) Grazie Marco 'codice per creare la tabella Categories e relativo comando di Insert comd.CommandText = "Select categoryID,CategoryName, Description from Categories" comdins.Connection = conn comdins.CommandText = "INSERT INTO Categories(CategoryName, Description) VALUES (@CategoryName, @Description); SELECT CategoryID, CategoryName, Description FROM Categories WHERE (CategoryID = @@IDENTITY)" comdins.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName")) comdins.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description")) DataAdap.SelectCommand = comd DataAdap.InsertCommand = comdins DataAdap.FillSchema(DatSet, SchemaType.Source, "Categories") DataAdap.Fill(DatSet, "Categories") 'codice per creare la tabella Products e relativo comando di Insert comd1.Connection = conn comd1.CommandText = "Select ProductID, ProductName,CategoryID from Products" ', CategoryID from Products" comdins1.Connection = conn comdins1.CommandText = "INSERT INTO Products (ProductName, CategoryID) VALUES (@ProductName, @CategoryID)" comdins1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ProductName", System.Data.SqlDbType.NVarChar, 40, "ProductName")) comdins1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Bit, 1, "CategoryID")) DataAdap1.SelectCommand = comd1 DataAdap1.InsertCommand = comdins1 DataAdap1.FillSchema(DatSet, SchemaType.Source, "Products") DataAdap1.Fill(DatSet, "Products") 'Relazione tra le due tabelle. Indipendentemente da questa i punti 1 e 2 non riescono DatSet.Relations.Add("CategoriesProducts", DatSet.Tables("Categories").Columns("CategoryID"), DatSet.Tables("Products").Columns("CategoryID")) 'associo il Dataset a una datagrid DataGrid1.DataSource = DatSet All evento click di un bottone tento l'aggiornamento DataAdap.Update(DatSet.Tables("Categories")) DataAdap.Update(DatSet.Tables("Products")) |