envoyer le contenu d'un dataset dans une base SQL SERVER
Bonjour,Je travaille actuellement sur un projet utilisant une base sqlserver que je manipule à l'aide de dataset : je m'explique.Je souhaite créer un dataset en utilisant la structure de ma base sur sql server(une seule table) afin de remplir mon dataset et d'envoyer le contenu du dataset dans la base.`Voilà mon code pour récupérer la structure de ma base :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static DataSet CreateDataset()
{
SqlConnection connection = new SqlConnection("Server = localhost;Database=nomBase;Trusted_Connection=True;");
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT * FROM dbo.Employee;";
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet dataset = new DataSet();
DataTable oDT = new DataTable();
connection.Open();
adapter.Fill(dataset, "dbo.Employee");
return dataset;
} |
Là pas de problème, je récupère bien la structure de ma base sql server
maintenant je souhaite ajouter des lignes à mon dataset, je rencontre quelques problèmes.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public static string CreateTestRow(int nbLignes, DataSet data)
{
SqlConnection connection = new SqlConnection("Server = localhost;Database=nomBase;Trusted_Connection=True;");
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT * FROM dbo.Employee ;";
SqlDataAdapter adapter = new SqlDataAdapter(command);
DateTime debut;
DateTime fin;
DataSet set = data;
DataRow row = set.Tables["dbo.Employee"].NewRow();
string time = "";
debut = DateTime.Now;
for (int i = 0; i < nbLignes; i++)
{
row["ID"] = 1;
row["Nom"] = 1;
row["Valeur"] = 1;
row["DateSimple"] = DateTime.Today;
row["DatePrecise"] = DateTime.Now;
set.Tables["dbo.Employee"].Rows.Add(row);
adapter.Update(set);
}
fin = DateTime.Now;
TimeSpan tpsEx = fin - debut;
time = tpsEx.Minutes + "min " + tpsEx.Seconds + "sec" + tpsEx.Milliseconds + "'";
return time;
} |
Une exception se déclenche à ce niveau la : adapter.Update(set);
Pouvez-vous m'aider ?