Csharp/C Sharp by API/System.Data/DataTable
Содержание
DataTable.Columns
using System;
using System.Data;
using System.Data.SqlClient;
class PopDataset
{
static void Main()
{
string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql = @"select * from employee";
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employee");
DataTable dt = ds.Tables["employee"];
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
Console.WriteLine("".PadLeft(20, "="));
}
}
catch(Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
conn.Close();
}
}
}
DataTable.Columns.Add
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");
}
}
DataTable.MinimumCapacity
using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.ruponentModel;
using System.Windows.Forms;
public class GridBind : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1 = new System.Windows.Forms.DataGrid ();
private DataSet dataset = new DataSet("ContactData");
public GridBind()
{
dataGrid1.BeginInit ();
dataGrid1.Location = new System.Drawing.Point (8, 16);
dataGrid1.Size = new System.Drawing.Size (472, 224);
dataGrid1.DataMember = "";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.ClientSize = new System.Drawing.Size (486, 251);
this.Controls.Add (this.dataGrid1);
dataGrid1.EndInit ();
DataTable t=new DataTable("Contacts");
t.Columns.Add("First",typeof(System.String));
t.Columns.Add("Name",typeof(System.String));
t.Columns.Add("Company",typeof(System.String));
t.Columns.Add("Title",typeof(System.String));
t.Columns.Add("Phone",typeof(System.String));
t.MinimumCapacity=100;
dataset.Tables.Add(t);
this.dataGrid1.SetDataBinding(dataset.Tables["Contacts"],"");
}
static void Main()
{
Application.Run(new GridBind());
}
}
DataTable.NewRow()
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");
}
}
DataTable.Rows
using System;
using System.IO;
using System.Data;
public class MainClass {
static void Main(string[] args) {
if (args.Length != 1)
return;
FileStream fs = new FileStream(args[0], FileMode.Open);
DataSet ds = new DataSet();
ds.ReadXml(fs);
// Use a DataTable to display the members.
DataTable mt = ds.Tables["member"];
for (int row = 0; row < mt.Rows.Count; row++) {
for (int col = 0; col < mt.Columns.Count - 1; col++) {
Console.WriteLine("{0,-10}{1}",
mt.Columns[col].Caption,
mt.Rows[row][col].ToString().Trim());
}
Console.WriteLine();
}
fs.Close();
}
}
DataTable.Rows.Add
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");
}
}