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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Ado_DATASET_Mieux
{
class Program
{
static void Main(string[] args)
{
string strConnexion = "Data Source=MAWOULE\\SQLEXPRESS; Integrated Security=True;" + "Initial
Catalog=Northwind";
try
{
SqlConnection oConnection = new SqlConnection(strConnexion);
oConnection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
// // // // // // // // // // // // // // // //
// Create the InsertCommand //
// // // // // // // // // // // // // // // //
command = new SqlCommand(
"INSERT INTO Categories(CategoryName, Description, Picture) " +
"Values(@CategoryName,@Description,@Picture)", oConnection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
command.Parameters.Add("@Description", SqlDbType.NText, 16, "Description");
command.Parameters.Add("@Picture", SqlDbType.Image, 16, "Picture");
adapter.InsertCommand = command;
DataRow oDataRow;
byte[] byteArray = { 0x00, 0x00 };
oDataRow = oDataSet.Tables["Categories"].NewRow();
oDataRow["CategoryName"] = "Wine2";
oDataRow["Description"] = "French Wine";
oDataRow["Picture"] = byteArray;
oDataSet.Tables["Categories"].Rows.Add(oDataRow);
// Mise à jour de la source de données à partir du DataSet
adapter.Update(oDataSet, "Categories");
// Rechargement des données de la source mise à jour
oDataSet.Clear();
adapter.Fill(oDataSet, "Categories");
// Affichage du contenu de oDataSet après insertion de données
Console.WriteLine(" *** Liste des catégories après l'insertion *** ");
for (int i = 0; i < oDataSet.Tables["Categories"].Rows.Count; i++)
{
Console.WriteLine("\t{0}\t{1}", oDataSet.Tables["Categories"].Rows[i][0].ToString(),
oDataSet.Tables["Categories"].Rows[i][1].ToString());
}
Console.WriteLine("\n");
// // // // // // // // // // // // // // // // //
// Create the UpdateCommand //
// // // // // // // // // // // // // // // // //
command = new SqlCommand(
"UPDATE Categories SET CategoryName = @CategoryName " +
"WHERE CategoryID = '10'", oConnection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
adapter.UpdateCommand = command;
DataRow[] CategoriesRow = oDataSet.Tables["Categories"].Select("CategoryID = 14 ");
CategoriesRow[0]["CategoryName"] = "tata";
CategoriesRow[0].AcceptChanges();
// Mise à jour de la source de données à partir du DataSet
adapter.Update(oDataSet, "Categories");
// Rechargement des données de la source mise à jour
oDataSet.Clear();
adapter.Fill(oDataSet, "Categories");
// Affichage du contenu de oDataSet après mise à jour de données
Console.WriteLine(" *** Liste des catégories après la mise à jour *** ");
for (int i = 0; i < oDataSet.Tables["Categories"].Rows.Count; i++)
{
Console.WriteLine("\t{0}\t{1}", oDataSet.Tables["Categories"].Rows[i][0].ToString(),
oDataSet.Tables["Categories"].Rows[i][1].ToString());
}
Console.WriteLine("\n");
Console.ReadLine();
// // // // // // // // // // // // // // // //
// Create the DeleteCommand //
// // // // // // // // // // // // // // // //
command = new SqlCommand(
"DELETE FROM Categories WHERE CategoryName = @CategoryName", oConnection);
// Add the parameters for the DeleteCommand.
command.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
DataRow [] oDataRows;
oDataRows = oDataSet.Tables["Categories"].Select("CategoryName = 'tata'");
oDataRows[0].Delete();
oDataSet.AcceptChanges();
adapter.DeleteCommand = command;
oDataSet.Clear();
adapter.Fill(oDataSet, "Categories");
// Affichage du contenu de oDataSet après Suppression de données
Console.WriteLine(" *** Liste des catégories après Suppression *** ");
for (int i = 0; i < oDataSet.Tables["Categories"].Rows.Count; i++)
{
Console.WriteLine("\t{0}\t{1}", oDataSet.Tables["Categories"].Rows[i][0].ToString(),
oDataSet.Tables["Categories"].Rows[i][1].ToString());
}
oConnection.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("L'erreur suivante a été rencontrée :" + e.Message);
Console.ReadLine();
}
}
}
} |
Partager