Monday, September 12, 2011

Monday 9.12.11

using System;
using System.Data;

namespace CreateAutoIncrementColumn
{
class Program
{
static void Main(string[] args)
{
//create a table and add two columns
DataTable dt = new DataTable();
DataColumn pkCol = dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Field1", typeof(string)).MaxLength = 50;


//make the Id column the primary key
dt.PrimaryKey = new DataColumn[] {dt.Columns["Id"]};

//make the primary key autoincrementing starting at 100
// and incrementing by 10 with each new record added
pkCol.AutoIncrement = true;
pkCol.AutoIncrementSeed = 100;
pkCol.AutoIncrementStep = 10;

//add five rows to the table
for (int i = 1; i <= 5; i++)
dt.Rows.Add(new object[] {null, "Value " + i});
//output the table rows to the console
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("Id = {0}\tField1 = {1}", row["Id"], row["Field1"]);
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
}
}


using System;
using System.Data;

namespace CreateForeignKeyConstraint
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();

//create the parent table and add to the DataSet
DataTable dt1 = new DataTable("Table-1");
dt1.Columns.Add("Id1", typeof(int));
dt1.Columns.Add("Field1", typeof(string)).MaxLength = 50;
ds.Tables.Add(dt1);

//create the child table and add to the DataSet
DataTable dt2 = new DataTable("Table-2");
dt2.Columns.Add("Id2", typeof(int));
dt2.Columns.Add("Id1", typeof(int));
dt2.Columns.Add("Field2", typeof(string)).MaxLength = 50;
ds.Tables.Add(dt2);

//create the foreign key constraint and add to the child table
ForeignKeyConstraint fk = new ForeignKeyConstraint("ForeignKey", dt1.Columns["Id1"], dt2.Columns["Id1"]);
dt2.Constraints.Add(fk);

try
{
AddParentRecord(dt1, 1, "Value 1.1");
AddParentRecord(dt1, 2, "Value 1.2");

AddChildRecord(dt2, 10, 1, "Value 2.10");
AddChildRecord(dt2, 11, 2, "Value 2.11");
AddChildRecord(dt2, 12, 3, "Value 2.12");

}
catch (Exception ex)
{
Console.WriteLine("Error: {0}\n", ex.Message);
}
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}

private static void AddParentRecord(DataTable dt, int id1, string field1)
{
Console.WriteLine("Adding parent record: {0}, {1}", id1, field1);
dt.Rows.Add(new object[] { id1, field1 });
Console.WriteLine("Done.\n");
}

private static void AddChildRecord(DataTable dt, int id2, int id1, string field2)
{
Console.WriteLine("Add child record: {0}, {1}, {2}", id2, id1, field2);
dt.Rows.Add(new object[] { id2, id1, field2 });
Console.WriteLine("Done.\n");
}
}
}



using System;
using System.Data;

namespace CreateDataRelation
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();

//create the parent table and add to the DataSet
DataTable dt1 = new DataTable("Table-1");
dt1.Columns.Add("Id1", typeof(int));
dt1.Columns.Add("Id2", typeof(int));
dt1.Columns.Add("Field1", typeof(string)).MaxLength = 50;
ds.Tables.Add(dt1);

//creat eth child table and add to the dataset
DataTable dt2 = new DataTable("Table-2");
dt2.Columns.Add("Id3", typeof(int));
dt2.Columns.Add("Id1", typeof(int));
dt2.Columns.Add("Id2", typeof(int));
dt2.Columns.Add("Field2", typeof(string)).MaxLength = 50;
ds.Tables.Add(dt2);

//create the data relation and add to the DataSet
DataRelation dr = new DataRelation("DataRelation",
new DataColumn[] { dt1.Columns["Id1"], dt1.Columns["Id2"] },
new DataColumn[] { dt2.Columns["Id1"], dt2.Columns["Id2"] },
true);
ds.Relations.Add(dr);

try
{
AddParentRecord(dt1, 1, 10, "Value 1.1");
AddParentRecord(dt1, 2, 20, "Value 1.2");

AddChildRecord(dt2, 100, 1, 10, "Value 2.100");
AddChildRecord(dt2, 101, 2, 20, "Value 2.101");
AddChildRecord(dt2, 102, 3, 30, "Value 2.102");
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}\n", ex.Message);
}
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}

private static void AddParentRecord(DataTable dt, int id1, int id2, string field1)
{
Console.WriteLine("Adding parent record: {0}, {1}. {2}", id1, id2, field1);
dt.Rows.Add(new object[] { id1, id2, field1 });
Console.WriteLine("Done.\n");
}

public static void AddChildRecord(DataTable dt, int id3, int id1, int id2, string field2)
{
Console.WriteLine("Add child record: {0}, {1}, {2}, {3}", id3, id1, id2, field2);
dt.Rows.Add(new object[] { id3, id1, id2, field2 });
Console.WriteLine("Done.\n");
}
}
}


Public Class Form1

Private Function RateArray(ByVal elementCount As Integer) As Decimal()
Dim rates(elementCount - 1) As Decimal
For i As Integer = 0 To rates.Length - 1
rates(i) = (i + 1) / 100D
Next
Return rates 'returns array
End Function

Private Sub ConvertToCentimeters(ByRef measurements() As Double)
For i As Integer = 0 To measurements.Length - 1
measurements(i) *= 2.54
Next
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rates() As Decimal = Me.RateArray(4)
txtDisplay.Multiline = True
txtDisplay.Height = 100

For Each r As String In rates
txtDisplay.Text &= r & vbCrLf
Next

Dim measurements() As Double = {1, 2, 3}
Me.ConvertToCentimeters(measurements)

End Sub
End Class


Dim numbers As New List(Of Integer)


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
numbers.Add(3)
numbers.Add(70)
Dim sum As Integer = 0
Dim number As Integer
For i As Integer = 0 To numbers.Count - 1
number = numbers(i) ' no cast is required
sum += number
Next
MsgBox(sum)
End Sub


Dim titles As New List(Of String)
Dim prices As New List(Of Decimal)
Dim lastNames As New List(Of String)(3)



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lastNames.Add("Boehm")
lastNames.Add("Prince")
lastNames.Add("Murach")
lastNames.Add("Taylor")
lastNames.Add("Vasquez")
lastNames.Add("Steelman")
lastNames.Add("Slivkoff")
For Each lname As String In lastNames
MsgBox(lname)
Next
End Sub


Imports System
Imports System.IO

Namespace Apress.VisualBasicRecipes.Chapter05
Public Class Recipe05_11
Public Shared Sub Main()
Dim args(1) As String
args(0) = "C:\inetpub\"
args(1) = "*.txt"

If args.Length = 2 Then
Dim dir As New DirectoryInfo(args(0))
Dim files As FileInfo() = dir.GetFiles(args(1))
'Display the name of all the files
For Each File As FileInfo In files
Console.Write("Name: " & File.Name + " ")
Console.WriteLine("Size: " & File.Length.ToString)
Next

'wait to continue
Console.WriteLine(Environment.NewLine)
Console.WriteLine("Main method complete. Press Enter.")
Console.ReadLine()

Else
Console.WriteLine("USAGE: Recipe05-11 [directory]" & "[filterExpression]")
End If
End Sub
End Class
End Namespace


Imports System
Imports System.IO
Imports System.Security.Cryptography

Namespace Apress.VisualBasicRecipes.Chapter05

Public Class Recipe05_12
Public Shared Sub Main()
Dim args(1) As String
args(0) = "C:\inetpub\test.txt"
args(1) = "C:\inetpub\Test2.txt"

If args.Length = 2 Then
Console.WriteLine("comparing {0} and {1}", args(0), args(1))
'creating the hashing object
Using hashAlg As HashAlgorithm = HashAlgorithm.Create
Using fsA As New FileStream(args(0), FileMode.Open), fsB As New FileStream(args(1), FileMode.Open)
'Calculate the has for the files
Dim hashBytesA As Byte() = hashAlg.ComputeHash(fsA)
Dim hashBytesB As Byte() = hashAlg.ComputeHash(fsB)

'compare the hashes
If BitConverter.ToString(hashBytesA) = BitConverter.ToString(hashBytesB) Then
Console.WriteLine("Files match.")
Else
Console.WriteLine("No match.")
End If
End Using

'wait to continue
Console.WriteLine(Environment.NewLine)
Console.WriteLine("Main method complete. Press Enter")
Console.ReadLine()
End Using
End If
End Sub
End Class

No comments: