Team LiB
Previous Section Next Section

Working with Data in a Database

To work with databases, there are several SQL commands you must know:

You can also use CommandBuilder to insert, update, and delete data in databases. The following examples demonstrate how to perform these tasks with SQL statements and with CommandBuilder, as well as how to use transactions, find rows in a database, use DataTableMappings, and call a stored procedure.

Code Example: Inserting Data into a Database Using SQL Statements
Start example
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class InsertingDataUsingSQLStatements
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager;"
                         + "Integrated Security=true");
                       MyConnection.Open();
                       String MyString = @"INSERT INTO Test(ID, Contact, Email)"
                         + @"VALUES(2, 'Greg', 'MacBeth')";
                       SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);
                       MyCmd.ExecuteScalar();
                       MyConnection.Close();
                }
         }
}
End example
Code Example: Inserting Data into a Database Using CommandBuilder
Start example

using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class InsertingDataUsingCommandBuilder
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager;"
                         + "Integrated Security=true");
                       SqlDataAdapter MyDataAdapter =
                       new SqlDataAdapter("SELECT ID, Contact, Email FROM Test",
                       MyConnection);

                       SqlCommandBuilder MyCmd =
                         new SqlCommandBuilder(MyDataAdapter);
                       DataSet MyDataSet = new DataSet();
                       MyDataAdapter.Fill(MyDataSet);
                       DataRow MyRow = MyDataSet.Tables[0].NewRow();
                       MyRow["ID"] = 200;
                       MyRow["Contact"] = "Greg";
                       MyRow["Email"] = "MacBeth";
                       MyDataSet.Tables[0].Rows.Add(MyRow);
                       MyDataAdapter.Update(MyDataSet);
                }
          }
}
End example
Code Example: Updating Data in a Database Using SQL Statements
Start example
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class UpdatingDataUsingSqlStatements
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager;"
                         + "Integrated Security=true");
                       MyConnection.Open();
                       String MyString = "UPDATE Test SET Contact = 'Lee'";
                       SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);
                       MyCmd.ExecuteScalar();
                       MyConnection.Close();
                }
          }
}
End example
Code Example: Using Transactions
Start example
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class UpdatingDataUsingTransactions
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager;"
                         + "Integrated Security=true");
                       MyConnection.Open();
                       String MyString = "INSERT INTO Test(ID, Contact, Email)"
                         + @"VALUES(1, 'Greg', 'Mac')";
                       SqlTransaction MyTransaction =
                         MyConnection.BeginTransaction();
                       SqlCommand MyCmd =
                         new SqlCommand(MyString, MyConnection, MyTransaction);
                       MyCmd.ExecuteScalar();
                       MyTransaction.Commit();
                       MyConnection.Close();
                }
          }
}
End example
Note 

This is a useful shortcut that allows you to avoid using the SQL statements when you are working with just one table.

Code Example: Updating Data in a Database Using CommandBuilder
Start example
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class UpdatingDataUsingCommandBuilder
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager; "
                         + "Integrated Security=true");
                       SqlDataAdapter MyDataAdapter =
                         new SqlDataAdapter("SELECT ID, Contact,"
                          + "Email FROM Test", MyConnection);
                       DataSet MyDataSet = new DataSet();
                       MyDataAdapter.Fill(MyDataSet);
                       MyDataSet.Tables[0].Rows[0][0] = 55;
                       SqlCommandBuilder MyCmd =
                         new SqlCommandBuilder(MyDataAdapter);
                       MyDataAdapter.Update(MyDataSet);
                }
          }
}
End example
Code Example: Deleting Data from a Database Using SQL Statements
Start example

using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class DeletingDataUsingSQLStatements
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + @"Initial Catalog = CaseManager; "
                         + "Integrated Security=true");
                       MyConnection.Open();
                       String MyString = "DELETE Test";
                       SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);
                       MyCmd.ExecuteScalar();
                       MyConnection.Close();
                }
          }
}
End example
Code Example: Deleting Data from a Database Using a CommandBuilder
Start example
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class DeletingDataUsingCommandBuilder
          {
                static void Main(string[] args)
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager;"
                         + "Integrated Security=true");
                       SqlDataAdapter MyDataAdapter =
                         new SqlDataAdapter("SELECT * FROM Test", MyConnection);
                       SqlCommandBuilder MyCmd =
                         new SqlCommandBuilder(MyDataAdapter);
                       DataSet MyDataSet = new DataSet();
                       MyDataAdapter.Fill(MyDataSet);
                       DataColumn[] MyKey = new DataColumn[1];
                       MyKey[0] = MyDataSet.Tables[0].Columns[0];
                       MyDataSet.Tables[0].PrimaryKey = MyKey;
                       DataRow FindMyRow = MyDataSet.Tables[0].Rows.Find(1);

                       FindMyRow.Delete();
                       MyDataAdapter.Update(MyDataSet);
                }
          }
}
End example
Code Example: Finding Rows in a Database
Start example

using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class FindingRowsInData
          {
                public static void Main()
                {
                       SqlConnection MyConnection =
                         new SqlConnection(@"Data Source=(local);"
                         + "Initial Catalog = CaseManager;"
                         + "Integrated Security=true");
                       SqlDataAdapter MyDataAdapter =
                       new SqlDataAdapter("SELECT * FROM Test", MyConnection);
                       SqlCommandBuilder MyCmd =
                         new SqlCommandBuilder(MyDataAdapter);
                       DataSet MyDataSet = new DataSet();
                       MyDataAdapter.Fill(MyDataSet);
                       DataColumn[] MyKey = new DataColumn[1];
                       MyKey[0] = MyDataSet.Tables[0].Columns[0];
                       MyDataSet.Tables[0].PrimaryKey = MyKey;
                       DataRow FindMyRow = MyDataSet.Tables[0].Rows.Find(1);
                }
          }
}
End example
Code Example: Using DataTableMappings
Start example
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class UsingDataTableMappings
          {
                 static void Main(string[] args)
                 {
                        SqlConnection SConn =
                          new SqlConnection("Data Source=(local);" +
                          "Initial Catalog = CaseManager;" +
                          "Integrated Security=true");
                        SqlDataAdapter da =
                          new SqlDataAdapter("SELECT ID, Contact,"
                          + "Email FROM CaseInfo", SConn);
                        DataSet ds = new DataSet();
                        DataTableMapping custMap =
                          da.TableMappings.Add("CaseInfo", "MyDatabase");
                        custMap.ColumnMappings.Add("ID", "CaseNumber");
                        custMap.ColumnMappings.Add("Contact", "MyContact");
                        custMap.ColumnMappings.Add("Email", "Email Address");
                        da.Fill(ds);
                 }
          }
}
End example
Code Example: Calling a Stored Procedure
Start example

using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
          class Client
          {
                static void Main(string[] args)
                {
                       SqlConnection cn = new SqlConnection(
                         @"Data Source=(local); Initial Catalog = MyDatabase;" +
                         "User ID=sa;Password=");
                       SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn);
                       cmd.CommandType = CommandType.StoredProcedure;
                       SqlParameter param =
                         new SqlParameter("@ReturnValue", SqlDbType.Int);
                       cmd.Parameters.Add(param);
                       cmd.Parameters.Add("MyFirstParameter", SqlDbType.Int);
                       cmd.Parameters.Add("MySecondParameter",
                          SqlDbType.Int).Direction =
                          ParameterDirection.Output;
                         SqlDataAdapter da = new SqlDataAdapter(cmd);
                }
          }
}
End example

Here is how the stored procedure in this example was created:

CREATE PROCEDURE MyStoredProcedure(
                                    @MyFirstParameter int,
                                    @MySecondParameter int OUTPUT)
AS
SELECT @MySecondParameter =
(SELECT Count FROM MyDB where ID LIKE @MyFirstParameter)
RETURN 1

Team LiB
Previous Section Next Section