Mega Code Archive

 
Categories / C# / ADO Database
 

Adding Data

using System; using System.Data;            // Use ADO.NET namespace using System.Data.SqlClient;  // Use SQL Server data provider namespace using System.Collections.Generic; using System.Text; class Program {     static void Main(string[] args) {         SqlConnection thisConnection = new SqlConnection(              @"Server=(local)\sqlexpress;Integrated Security=True;" +             "Database=northwind");         SqlDataAdapter thisAdapter = new SqlDataAdapter(              "SELECT CustomerID, CompanyName FROM Customers", thisConnection);         SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);         DataSet thisDataSet = new DataSet();         thisAdapter.Fill(thisDataSet, "Customers");         Console.WriteLine("# rows before change: {0}",                            thisDataSet.Tables["Customers"].Rows.Count);         DataRow thisRow = thisDataSet.Tables["Customers"].NewRow();         thisRow["CustomerID"] = "ZACZI";         thisRow["CompanyName"] = "Zachary Zithers Ltd.";         thisDataSet.Tables["Customers"].Rows.Add(thisRow);         Console.WriteLine("# rows after change: {0}",                           thisDataSet.Tables["Customers"].Rows.Count);         thisAdapter.Update(thisDataSet, "Customers");         thisConnection.Close();     } }