Csharp/C Sharp by API/System.Data/DataColumn — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:12, 26 мая 2010
Содержание
DataColumn.AllowDBNull
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Car {
public string carPetName, carMake, carColor;
public Car(string petName, string make, string color) {
carPetName = petName;
carColor = color;
carMake = make;
}
}
public class MainForm : Form {
private List<Car> arTheCars = new List<Car>();
private DataTable inventoryTable = new DataTable("Inventory");
DataView coltsOnlyView; // I only show red colts.
public MainForm() {
InitializeComponent();
arTheCars.Add(new Car("C", "BMW", "Green"));
arTheCars.Add(new Car("T", "Y", "White"));
arTheCars.Add(new Car("AAA", "Jeep", "Tan"));
arTheCars.Add(new Car("PInducer", "Caravan", "Pink"));
arTheCars.Add(new Car("F", "BMW", "Pea Soup Green"));
arTheCars.Add(new Car("B", "BMW", "Black"));
arTheCars.Add(new Car("M", "DDD", "Red"));
arTheCars.Add(new Car("S", "Colt", "Black"));
CreateDataTable();
CreateDataView();
}
private void CreateDataTable() {
DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
carIDColumn.ReadOnly = true;
carIDColumn.Caption = "Car ID";
carIDColumn.AllowDBNull = false;
carIDColumn.Unique = true;
carIDColumn.AutoIncrement = true;
carIDColumn.AutoIncrementSeed = 0;
carIDColumn.AutoIncrementStep = 1;
carIDColumn.ColumnMapping = MappingType.Attribute;
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Pet Name";
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });
inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
foreach (Car c in arTheCars) {
DataRow newRow = inventoryTable.NewRow();
newRow["Make"] = c.carMake;
newRow["Color"] = c.carColor;
newRow["PetName"] = c.carPetName;
inventoryTable.Rows.Add(newRow);
}
carInventoryGridView.DataSource = inventoryTable;
}
private void CreateDataView() {
coltsOnlyView = new DataView(inventoryTable);
coltsOnlyView.RowFilter = "Make = "Colt"";
dataGridColtsView.DataSource = coltsOnlyView;
}
private void btnRemoveRow_Click(object sender, EventArgs e) {
try {
inventoryTable.Rows[(int.Parse(txtRowToRemove.Text))].Delete();
inventoryTable.AcceptChanges();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void btnGetMakes_Click(object sender, EventArgs e) {
string filterStr = string.Format("Make= "{0}" ", txtMakeToGet.Text);
DataRow[] makes = inventoryTable.Select(filterStr, "PetName DESC");
if (makes.Length == 0)
MessageBox.Show("Sorry, no cars...", "Selection error!");
else {
string strMake = null;
for (int i = 0; i < makes.Length; i++) {
DataRow temp = makes[i];
strMake += temp["PetName"] + "\n";
}
MessageBox.Show(strMake, txtMakeToGet.Text + " type(s):");
}
}
private void btnChangeBeemersToColts_Click(object sender, EventArgs e) {
string filterStr = "Make="BMW"";
string strMake = null;
DataRow[] makes = inventoryTable.Select(filterStr);
for (int i = 0; i < makes.Length; i++) {
DataRow temp = makes[i];
strMake += temp["Make"] = "Colt";
makes[i] = temp;
}
}
private void ShowCarsWithIdLessThanFive() {
DataRow[] properIDs;
string newFilterStr = "ID > "5"";
properIDs = inventoryTable.Select(newFilterStr);
string strIDs = null;
for (int i = 0; i < properIDs.Length; i++) {
DataRow temp = properIDs[i];
strIDs += temp["PetName"]
+ " is ID " + temp["ID"] + "\n";
}
MessageBox.Show(strIDs, "Pet names of cars where ID > 5");
}
private void InitializeComponent() {
this.label1 = new System.Windows.Forms.Label();
this.carInventoryGridView = new System.Windows.Forms.DataGridView();
this.btnRemoveRow = new System.Windows.Forms.Button();
this.txtRowToRemove = new System.Windows.Forms.TextBox();
this.txtMakeToGet = new System.Windows.Forms.TextBox();
this.btnGetMakes = new System.Windows.Forms.Button();
this.btnChangeBeemersToColts = new System.Windows.Forms.Button();
this.dataGridColtsView = new System.Windows.Forms.DataGridView();
this.label2 = new System.Windows.Forms.Label();
((System.ruponentModel.ISupportInitialize)(this.carInventoryGridView)).BeginInit();
((System.ruponentModel.ISupportInitialize)(this.dataGridColtsView)).BeginInit();
this.SuspendLayout();
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(11, 76);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(102, 13);
this.label1.TabIndex = 0;
this.label1.Text = "All Cars in DataTable";
//
this.carInventoryGridView.Location = new System.Drawing.Point(12, 93);
this.carInventoryGridView.Name = "carInventoryGridView";
this.carInventoryGridView.Size = new System.Drawing.Size(392, 150);
this.carInventoryGridView.TabIndex = 1;
this.carInventoryGridView.Text = "dataGridView1";
//
this.btnRemoveRow.Location = new System.Drawing.Point(12, 13);
this.btnRemoveRow.Name = "btnRemoveRow";
this.btnRemoveRow.Size = new System.Drawing.Size(101, 23);
this.btnRemoveRow.TabIndex = 2;
this.btnRemoveRow.Text = "Remove Row #";
this.btnRemoveRow.Click += new System.EventHandler(this.btnRemoveRow_Click);
//
this.txtRowToRemove.Location = new System.Drawing.Point(120, 15);
this.txtRowToRemove.Name = "txtRowToRemove";
this.txtRowToRemove.Size = new System.Drawing.Size(100, 20);
this.txtRowToRemove.TabIndex = 3;
//
this.txtMakeToGet.Location = new System.Drawing.Point(120, 44);
this.txtMakeToGet.Name = "txtMakeToGet";
this.txtMakeToGet.Size = new System.Drawing.Size(100, 20);
this.txtMakeToGet.TabIndex = 5;
//
// btnGetMakes
//
this.btnGetMakes.Location = new System.Drawing.Point(12, 42);
this.btnGetMakes.Name = "btnGetMakes";
this.btnGetMakes.Size = new System.Drawing.Size(101, 23);
this.btnGetMakes.TabIndex = 4;
this.btnGetMakes.Text = "Get These Makes";
this.btnGetMakes.Click += new System.EventHandler(this.btnGetMakes_Click);
//
// btnChangeBeemersToColts
//
this.btnChangeBeemersToColts.Location = new System.Drawing.Point(245, 15);
this.btnChangeBeemersToColts.Name = "btnChangeBeemersToColts";
this.btnChangeBeemersToColts.Size = new System.Drawing.Size(159, 23);
this.btnChangeBeemersToColts.TabIndex = 6;
this.btnChangeBeemersToColts.Text = "Change BMW to Colts";
this.btnChangeBeemersToColts.Click += new System.EventHandler(this.btnChangeBeemersToColts_Click);
//
// dataGridColtsView
//
this.dataGridColtsView.Location = new System.Drawing.Point(12, 266);
this.dataGridColtsView.Name = "dataGridColtsView";
this.dataGridColtsView.Size = new System.Drawing.Size(392, 150);
this.dataGridColtsView.TabIndex = 8;
this.dataGridColtsView.Text = "dataGridView1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(11, 249);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(76, 13);
this.label2.TabIndex = 7;
this.label2.Text = "Colts Only View";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(417, 433);
this.Controls.Add(this.dataGridColtsView);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnChangeBeemersToColts);
this.Controls.Add(this.txtMakeToGet);
this.Controls.Add(this.btnGetMakes);
this.Controls.Add(this.txtRowToRemove);
this.Controls.Add(this.btnRemoveRow);
this.Controls.Add(this.carInventoryGridView);
this.Controls.Add(this.label1);
this.Name = "MainForm";
this.Text = "Car Data Table";
((System.ruponentModel.ISupportInitialize)(this.carInventoryGridView)).EndInit();
((System.ruponentModel.ISupportInitialize)(this.dataGridColtsView)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;
private System.Windows.Forms.DataGridView carInventoryGridView;
private System.Windows.Forms.Button btnRemoveRow;
private System.Windows.Forms.TextBox txtRowToRemove;
private System.Windows.Forms.TextBox txtMakeToGet;
private System.Windows.Forms.Button btnGetMakes;
private System.Windows.Forms.Button btnChangeBeemersToColts;
private System.Windows.Forms.DataGridView dataGridColtsView;
private System.Windows.Forms.Label label2;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
DataColumn.AutoIncrement
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
class Program {
static void Main(string[] args) {
DataSet carsInventoryDS = new DataSet("Inventory");
carsInventoryDS.ExtendedProperties["TimeStamp"] = DateTime.Now;
carsInventoryDS.ExtendedProperties["Company"] = "Name";
DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
carIDColumn.ReadOnly = true;
carIDColumn.Caption = "Car ID";
carIDColumn.AllowDBNull = false;
carIDColumn.Unique = true;
carIDColumn.AutoIncrement = true;
carIDColumn.AutoIncrementSeed = 0;
carIDColumn.AutoIncrementStep = 1;
carIDColumn.ColumnMapping = MappingType.Attribute;
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Name";
DataTable inventoryTable = new DataTable("Inventory");
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });
inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
DataRow carRow = inventoryTable.NewRow();
carRow["Make"] = "B";
carRow["Color"] = "C";
carRow["PetName"] = "A";
inventoryTable.Rows.Add(carRow);
carRow = inventoryTable.NewRow();
carRow["Make"] = "S";
carRow["Color"] = "R";
carRow["PetName"] = "E";
inventoryTable.Rows.Add(carRow);
carsInventoryDS.Tables.Add(inventoryTable);
PrintTable(carsInventoryDS.Tables["Inventory"]);
}
private static void PrintTable(DataTable dt) {
DataTableReader dtReader = dt.CreateDataReader();
while (dtReader.Read()) {
for (int i = 0; i < dtReader.FieldCount; i++) {
Console.Write("{0} = {1} ",
dtReader.GetName(i).Trim(),
dtReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
}
dtReader.Close();
}
}
DataColumn.AutoIncrementStep
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
class Program {
static void Main(string[] args) {
DataSet carsInventoryDS = new DataSet("Inventory");
carsInventoryDS.ExtendedProperties["TimeStamp"] = DateTime.Now;
carsInventoryDS.ExtendedProperties["Company"] = "Name";
DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
carIDColumn.ReadOnly = true;
carIDColumn.Caption = "Car ID";
carIDColumn.AllowDBNull = false;
carIDColumn.Unique = true;
carIDColumn.AutoIncrement = true;
carIDColumn.AutoIncrementSeed = 0;
carIDColumn.AutoIncrementStep = 1;
carIDColumn.ColumnMapping = MappingType.Attribute;
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Name";
DataTable inventoryTable = new DataTable("Inventory");
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });
inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
DataRow carRow = inventoryTable.NewRow();
carRow["Make"] = "B";
carRow["Color"] = "C";
carRow["PetName"] = "A";
inventoryTable.Rows.Add(carRow);
carRow = inventoryTable.NewRow();
carRow["Make"] = "S";
carRow["Color"] = "R";
carRow["PetName"] = "E";
inventoryTable.Rows.Add(carRow);
carsInventoryDS.Tables.Add(inventoryTable);
PrintTable(carsInventoryDS.Tables["Inventory"]);
}
private static void PrintTable(DataTable dt) {
DataTableReader dtReader = dt.CreateDataReader();
while (dtReader.Read()) {
for (int i = 0; i < dtReader.FieldCount; i++) {
Console.Write("{0} = {1} ",
dtReader.GetName(i).Trim(),
dtReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
}
dtReader.Close();
}
}
DataColumn.Caption
using System;
using System.Data;
using System.Data.SqlClient;
public class CreatingDataTablesandPopulatingThem
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");
SqlDataAdapter MyAdapter = new SqlDataAdapter("SELECT * FROM CaseInfo", MyConnection);
DataSet MyDataSet = new DataSet();
//Create a new DataTable
DataTable MyTable2 = MyDataSet.Tables.Add("My2ndTable");
//Adding Columns and Rows
DataColumn myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.Decimal");
myColumn.AllowDBNull = false;
myColumn.Caption = "Price";
myColumn.ColumnName = "Price";
myColumn.DefaultValue = 25;
// Add the column to the table.
MyTable2.Columns.Add(myColumn);
// Add 10 rows and set values.
DataRow myRow;
for (int i = 0; i < 10; i++)
{
myRow = MyTable2.NewRow();
myRow[0] = i + 1;
// Be sure to add the new row to the DataRowCollection.
MyTable2.Rows.Add(myRow);
}
SqlCommandBuilder Builder = new SqlCommandBuilder(MyAdapter);
MyAdapter.Update(MyDataSet, "My2ndTable");
}
}
DataColumn.ColumnMapping
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
class Program {
static void Main(string[] args) {
DataSet carsInventoryDS = new DataSet("Inventory");
carsInventoryDS.ExtendedProperties["TimeStamp"] = DateTime.Now;
carsInventoryDS.ExtendedProperties["Company"] = "Name";
DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
carIDColumn.ReadOnly = true;
carIDColumn.Caption = "Car ID";
carIDColumn.AllowDBNull = false;
carIDColumn.Unique = true;
carIDColumn.AutoIncrement = true;
carIDColumn.AutoIncrementSeed = 0;
carIDColumn.AutoIncrementStep = 1;
carIDColumn.ColumnMapping = MappingType.Attribute;
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Name";
DataTable inventoryTable = new DataTable("Inventory");
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });
inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
DataRow carRow = inventoryTable.NewRow();
carRow["Make"] = "B";
carRow["Color"] = "C";
carRow["PetName"] = "A";
inventoryTable.Rows.Add(carRow);
carRow = inventoryTable.NewRow();
carRow["Make"] = "S";
carRow["Color"] = "R";
carRow["PetName"] = "E";
inventoryTable.Rows.Add(carRow);
carsInventoryDS.Tables.Add(inventoryTable);
PrintTable(carsInventoryDS.Tables["Inventory"]);
}
private static void PrintTable(DataTable dt) {
DataTableReader dtReader = dt.CreateDataReader();
while (dtReader.Read()) {
for (int i = 0; i < dtReader.FieldCount; i++) {
Console.Write("{0} = {1} ",
dtReader.GetName(i).Trim(),
dtReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
}
dtReader.Close();
}
}
DataColumn.ColumnName
using System;
using System.Data;
using System.Data.SqlClient;
public class CreatingDataTablesandPopulatingThem
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");
SqlDataAdapter MyAdapter = new SqlDataAdapter("SELECT * FROM CaseInfo", MyConnection);
DataSet MyDataSet = new DataSet();
//Create a new DataTable
DataTable MyTable2 = MyDataSet.Tables.Add("My2ndTable");
//Adding Columns and Rows
DataColumn myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.Decimal");
myColumn.AllowDBNull = false;
myColumn.Caption = "Price";
myColumn.ColumnName = "Price";
myColumn.DefaultValue = 25;
// Add the column to the table.
MyTable2.Columns.Add(myColumn);
// Add 10 rows and set values.
DataRow myRow;
for (int i = 0; i < 10; i++)
{
myRow = MyTable2.NewRow();
myRow[0] = i + 1;
// Be sure to add the new row to the DataRowCollection.
MyTable2.Rows.Add(myRow);
}
SqlCommandBuilder Builder = new SqlCommandBuilder(MyAdapter);
MyAdapter.Update(MyDataSet, "My2ndTable");
}
}
DataColumn.DataType
using System;
using System.Data;
using System.Data.SqlClient;
public class CreatingDataTablesandPopulatingThem
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");
SqlDataAdapter MyAdapter = new SqlDataAdapter("SELECT * FROM CaseInfo", MyConnection);
DataSet MyDataSet = new DataSet();
//Create a new DataTable
DataTable MyTable2 = MyDataSet.Tables.Add("My2ndTable");
//Adding Columns and Rows
DataColumn myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.Decimal");
myColumn.AllowDBNull = false;
myColumn.Caption = "Price";
myColumn.ColumnName = "Price";
myColumn.DefaultValue = 25;
// Add the column to the table.
MyTable2.Columns.Add(myColumn);
// Add 10 rows and set values.
DataRow myRow;
for (int i = 0; i < 10; i++)
{
myRow = MyTable2.NewRow();
myRow[0] = i + 1;
// Be sure to add the new row to the DataRowCollection.
MyTable2.Rows.Add(myRow);
}
SqlCommandBuilder Builder = new SqlCommandBuilder(MyAdapter);
MyAdapter.Update(MyDataSet, "My2ndTable");
}
}
DataColumn.DefaultValue
using System;
using System.Data;
using System.Data.SqlClient;
public class CreatingDataTablesandPopulatingThem
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");
SqlDataAdapter MyAdapter = new SqlDataAdapter("SELECT * FROM CaseInfo", MyConnection);
DataSet MyDataSet = new DataSet();
//Create a new DataTable
DataTable MyTable2 = MyDataSet.Tables.Add("My2ndTable");
//Adding Columns and Rows
DataColumn myColumn = new DataColumn();
myColumn.DataType = System.Type.GetType("System.Decimal");
myColumn.AllowDBNull = false;
myColumn.Caption = "Price";
myColumn.ColumnName = "Price";
myColumn.DefaultValue = 25;
// Add the column to the table.
MyTable2.Columns.Add(myColumn);
// Add 10 rows and set values.
DataRow myRow;
for (int i = 0; i < 10; i++)
{
myRow = MyTable2.NewRow();
myRow[0] = i + 1;
// Be sure to add the new row to the DataRowCollection.
MyTable2.Rows.Add(myRow);
}
SqlCommandBuilder Builder = new SqlCommandBuilder(MyAdapter);
MyAdapter.Update(MyDataSet, "My2ndTable");
}
}
new DataColumn
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
class Program {
static void Main(string[] args) {
DataSet carsInventoryDS = new DataSet("Inventory");
carsInventoryDS.ExtendedProperties["TimeStamp"] = DateTime.Now;
carsInventoryDS.ExtendedProperties["Company"] = "Name";
DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
carIDColumn.ReadOnly = true;
carIDColumn.Caption = "Car ID";
carIDColumn.AllowDBNull = false;
carIDColumn.Unique = true;
carIDColumn.AutoIncrement = true;
carIDColumn.AutoIncrementSeed = 0;
carIDColumn.AutoIncrementStep = 1;
carIDColumn.ColumnMapping = MappingType.Attribute;
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Name";
DataTable inventoryTable = new DataTable("Inventory");
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });
inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
DataRow carRow = inventoryTable.NewRow();
carRow["Make"] = "B";
carRow["Color"] = "C";
carRow["PetName"] = "A";
inventoryTable.Rows.Add(carRow);
carRow = inventoryTable.NewRow();
carRow["Make"] = "S";
carRow["Color"] = "R";
carRow["PetName"] = "E";
inventoryTable.Rows.Add(carRow);
carsInventoryDS.Tables.Add(inventoryTable);
PrintTable(carsInventoryDS.Tables["Inventory"]);
}
private static void PrintTable(DataTable dt) {
DataTableReader dtReader = dt.CreateDataReader();
while (dtReader.Read()) {
for (int i = 0; i < dtReader.FieldCount; i++) {
Console.Write("{0} = {1} ",
dtReader.GetName(i).Trim(),
dtReader.GetValue(i).ToString().Trim());
}
Console.WriteLine();
}
dtReader.Close();
}
}