Csharp/C Sharp/Database ADO.net/DataRow

Материал из .Net Framework эксперт
Версия от 11:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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();
    }
}


Find, filter, and sort DataRow objects

   
using System;
using System.Data;
using System.Data.SqlClient;
class FindFilterAndSortDataRows {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText =
          "SELECT TOP 10 ProductID, ProductName " +
          "FROM Products " +
          "ORDER BY ProductID;" +
          "SELECT TOP 10 OrderID, ProductID, UnitPrice, " +
          " Quantity FROM [Order Details] " +
          "ORDER BY OrderID";
        SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
        mySqlDataAdapter.SelectCommand = mySqlCommand;
        DataSet myDataSet = new DataSet();
        mySqlConnection.Open();
        mySqlDataAdapter.Fill(myDataSet);
        mySqlConnection.Close();
        myDataSet.Tables["Table"].TableName = "Products";
        myDataSet.Tables["Table1"].TableName = "Order Details";
        DataTable productsDataTable = myDataSet.Tables["Products"];
        productsDataTable.PrimaryKey = new DataColumn[] { productsDataTable.Columns["ProductID"] };
        DataTable orderDetailsDataTable = myDataSet.Tables["Order Details"];
        orderDetailsDataTable.Constraints.Add("Primary key constraint on the OrderID" +
          "and ProductID columns", new DataColumn[]
      {orderDetailsDataTable.Columns["OrderID"],
        orderDetailsDataTable.Columns["ProductID"]
      },
          true
        );
        DataRow productDataRow = productsDataTable.Rows.Find("3");
        foreach (DataColumn myDataColumn in productsDataTable.Columns) {
            Console.WriteLine(myDataColumn + " = " + productDataRow[myDataColumn]);
        }
        object[] orderDetails = new object[] { 10248, 11 };
        DataRow orderDetailDataRow = orderDetailsDataTable.Rows.Find(orderDetails);
        foreach (DataColumn myDataColumn in
          orderDetailsDataTable.Columns) {
            Console.WriteLine(
              myDataColumn + " = " +
              orderDetailDataRow[myDataColumn]);
        }
        DataRow[] productDataRows = productsDataTable.Select("ProductID <= 5", "ProductID DESC", DataViewRowState.OriginalRows);
        foreach (DataRow myDataRow in productDataRows) {
            foreach (DataColumn myDataColumn in productsDataTable.Columns) {
                Console.WriteLine(myDataColumn + " = " + myDataRow[myDataColumn]);
            }
        }
        productDataRows = productsDataTable.Select("ProductName LIKE "Cha*"", "ProductID ASC, ProductName DESC");
        foreach (DataRow myDataRow in productDataRows) {
            foreach (DataColumn myDataColumn in productsDataTable.Columns) {
                Console.WriteLine(myDataColumn + " = " + myDataRow[myDataColumn]);
            }
        }
    }
}