SQL SERVER TYPES, COMPATIBLE Sql* TYPES, AND GetSql* METHODS

   


SQL SERVER TYPE    Sql* TYPE             GetSql* METHOD

bigint             SqlInt64              GetSqlInt64()

int                SqlInt32              GetSqlInt32()

smallint           SqlInt16              GetSqlInt16()

tinyint            SqlByte               GetSqlByte()

bit                SqlBoolean            GetSqlBoolean()

decimal            SqlDecimal            GetSqlDecimal()

numeric            SqlDecimal            GetSqlDecimal()

money              SqlMoney              GetSqlMoney()

smallmoney         SqlMoney              GetSqlMoney()

float              SqlDouble             GetSqlDouble()

real               SqlSingle             GetSqlSingle()

datetime           SqlDateTime           GetSqlDateTime()

smalldatetime      SqlDateTime           GetSqlDateTime()

char               SqlString             GetSqlString()

varchar            SqlString             GetSqlString()

text               SqlString             GetSqlString()

nchar              SqlString             GetSqlString()

nvarchar           SqlString             GetSqlString()

ntext              SqlString             GetSqlString()

binary             SqlBinary             GetSqlBinary()

varbinary          SqlBinary             GetSqlBinary()

image              SqlBinary             GetSqlBinary()

sql_varient        object                GetSqlValue()

timestamp          SqlBinary             GetSqlBinary()

uniqueidentifier   SqlGuid               GetSqlGuid() 
           
          


Shows the SQL server types, the corresponding Sql types, and the GetSql*** methods used to read a column as the Sql type

   


Sql* TYPE            VALUES               
===============================================================================
Sql Binary           A variable-length string of binary data.

SqlBoolean           An integer with either a 1 or 0 value.

SqlByte              An 8-bit unsigned integer value between 0 and 2^8 - 1 (255).

SqlDateTime          A date and time between 12:00:00 AM January 1, 1753 and 11:59:59 PM December 31, 9999. This is accurate to 3.33 milliseconds.

SqlDecimal           Fixed precision and scale numeric value between -1038 + 1 and 1038 - 1.

SqlDouble            A 64-bit floating-point number between -1.79769313486232E308 and 1.79769313486232E308 with 15 significant figures of precision.

SqlGuid              A 128-bit integer value (16 bytes) that that is unique across all computers and networks.

SqlInt16             A 16-bit signed integer between -2^15 (-32,768) and 2^15 - 1 (32,767).

SqlInt32             A 32-bit signed integer between-2^31 (-2,147,483,648) and 2^31 - 1 (2,147,483,647).

SqlInt64             A 64-bit signed integer between -2^63 (-9,223,372,036,854,775,808) and 2^63 - 1 (9,223,372,036,854,775,807).

SqlMoney             A currency value between -922,337,203,685,477.5808 and 922,337,203,685,477.5807. This is accurate to 1/10,000th of a currency unit.

SqlSingle            A 32-bit floating-point number between -3.402823E38 and 3.402823E38 with seven significant figures of precision.

SqlString            A variable-length string of characters.
           
          


how to execute a SELECT statement using a SqlCommand object

   
 


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

class ExecuteSelect {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.CommandText =
          "SELECT TOP 5 CustomerID, CompanyName, ContactName, Address " +
          "FROM Customers " +
          "ORDER BY CustomerID";

        mySqlConnection.Open();
        SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
        while (mySqlDataReader.Read()) {
            Console.WriteLine("mySqlDataReader[" CustomerID"] = " + mySqlDataReader["CustomerID"]);
            Console.WriteLine("mySqlDataReader[" CompanyName"] = " + mySqlDataReader["CompanyName"]);
            Console.WriteLine("mySqlDataReader[" ContactName"] = " + mySqlDataReader["ContactName"]);
            Console.WriteLine("mySqlDataReader[" Address"] = " + mySqlDataReader["Address"]);
        }
        mySqlDataReader.Close();
        mySqlConnection.Close();
    }
}

    


How to control the command behavior to return a single row

   


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

class SingleRowCommandBehavior
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText ="SELECT ID, FirstName, LastName FROM Employee";

    mySqlConnection.Open();

    SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.SingleRow);

    while (mySqlDataReader.Read()){
      Console.WriteLine("mySqlDataReader[" ID"] = " +
        mySqlDataReader["ID"]);
      Console.WriteLine("mySqlDataReader[" FirstName"] = " +
        mySqlDataReader["FirstName"]);
      Console.WriteLine("mySqlDataReader[" LastName"] = " +
        mySqlDataReader["LastName"]);
    }

    mySqlDataReader.Close();
    mySqlConnection.Close();
  }
}
           
          


Populate a DataSet object with a range of rows from a SELECT statement

   

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

class PopulateDataSetUsingRange
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText =
      "SELECT TOP 5 ID, FirstName, LastName " +
      "FROM Employee " +
      "ORDER BY ID";
    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();
    mySqlConnection.Open();

    Console.WriteLine("Retrieving rows from the Employee table");
    int numberOfRows = mySqlDataAdapter.Fill(myDataSet, 1, 3, "Employee");
    Console.WriteLine("numberOfRows = " + numberOfRows);

    mySqlConnection.Close();

    DataTable myDataTable = myDataSet.Tables["Employee"];

    foreach (DataRow myDataRow in myDataTable.Rows)
    {
      Console.WriteLine("ID = " + myDataRow["ID"]);
      Console.WriteLine("FirstName = " + myDataRow["FirstName"]);
      Console.WriteLine("LastName = " + myDataRow["LastName"]);
    }
  }
}
           
          


Finding Rows In DataSet

   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
    public 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);
        }
    }
}




           
          


illustrates how to perform a SELECT statement using ADO.NET

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example23_1.cs illustrates how to perform a SELECT statement
using ADO.NET
*/

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

public class Example23_1
{

public static void Main()
{

// step 1: formulate a string containing the details of the
// database connection
string connectionString =
“server=localhost;database=Northwind;uid=sa;pwd=sa”;

// step 2: create a SqlConnection object to connect to the
// database, passing the connection string to the constructor
SqlConnection mySqlConnection =
new SqlConnection(connectionString);

// step 3: formulate a SELECT statement to retrieve the
// CustomerID, CompanyName, ContactName, and Address
// columns for the first ten rows from the Customers table
string selectString =
“SELECT CustomerID, CompanyName, ContactName, Address ” +
“FROM Customers ” +
“WHERE CustomerID < 'BSBEV'"; // step 4: create a SqlCommand object to hold the SELECT statement SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); // step 5: set the CommandText property of the SqlCommand object to // the SELECT string mySqlCommand.CommandText = selectString; // step 6: create a SqlDataAdapter object SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); // step 7: set the SelectCommand property of the SqlAdapter object // to the SqlCommand object mySqlDataAdapter.SelectCommand = mySqlCommand; // step 8: create a DataSet object to store the results of // the SELECT statement DataSet myDataSet = new DataSet(); // step 9: open the database connection using the // Open() method of the SqlConnection object mySqlConnection.Open(); // step 10: use the Fill() method of the SqlDataAdapter object to // retrieve the rows from the table, storing the rows locally // in a DataTable of the DataSet object Console.WriteLine("Retrieving rows from the Customers table"); string dataTableName = "Customers"; mySqlDataAdapter.Fill(myDataSet, dataTableName); // step 11: get the DataTable object from the DataSet object DataTable myDataTable = myDataSet.Tables[dataTableName]; // step 12: display the columns for each row in the DataTable, // using a DataRow object to access each row in the DataTable foreach (DataRow myDataRow in myDataTable.Rows) { Console.WriteLine("CustomerID = " + myDataRow["CustomerID"]); Console.WriteLine("CompanyName = " + myDataRow["CompanyName"]); Console.WriteLine("ContactName = " + myDataRow["ContactName"]); Console.WriteLine("Address = " + myDataRow["Address"]); } // step 13: close the database connection using the Close() method // of the SqlConnection object created in Step 2 mySqlConnection.Close(); } } [/csharp]