Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 59 additions & 59 deletions snippets/csharp/System/DBNull/Overview/DBNullExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,68 @@ public class DBNullExample
{
public static void Main()
{
DBNullExample ex = new DBNullExample();
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataSet ds = new DataSet();
string dbFilename = @"c:\Data\contacts.mdb";
DBNullExample ex = new();
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataSet ds = new();
string dbFilename = @"c:\Data\contacts.mdb";

// Open database connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
dbFilename + ";";
conn.Open();
// Define command : retrieve all records in contact table
cmd.CommandText = "SELECT * FROM Contact";
cmd.Connection = conn;
adapter.SelectCommand = cmd;
// Fill dataset
ds.Clear();
adapter.Fill(ds, "Contact");
// Close connection
conn.Close();
// Output labels to console
ex.OutputLabels(ds.Tables["Contact"]);
}
// Open database connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
dbFilename + ";";
conn.Open();
// Define command : retrieve all records in contact table
cmd.CommandText = "SELECT * FROM Contact";
cmd.Connection = conn;
adapter.SelectCommand = cmd;
// Fill dataset
ds.Clear();
adapter.Fill(ds, "Contact");
// Close connection
conn.Close();
// Output labels to console
ex.OutputLabels(ds.Tables["Contact"]);
}

// <Snippet1>
private void OutputLabels(DataTable dt)
{
string label;
// <Snippet1>
private void OutputLabels(DataTable dt)
{
string label;

// Iterate rows of table
foreach (DataRow row in dt.Rows)
{
int labelLen;
label = String.Empty;
label += AddFieldValue(label, row, "Title");
label += AddFieldValue(label, row, "FirstName");
label += AddFieldValue(label, row, "MiddleInitial");
label += AddFieldValue(label, row, "LastName");
label += AddFieldValue(label, row, "Suffix");
label += "\n";
label += AddFieldValue(label, row, "Address1");
label += AddFieldValue(label, row, "AptNo");
label += "\n";
labelLen = label.Length;
label += AddFieldValue(label, row, "Address2");
if (label.Length != labelLen)
// Iterate rows of table
foreach (DataRow row in dt.Rows)
{
int labelLen;
label = string.Empty;
label += AddFieldValue(label, row, "Title");
label += AddFieldValue(label, row, "FirstName");
label += AddFieldValue(label, row, "MiddleInitial");
label += AddFieldValue(label, row, "LastName");
label += AddFieldValue(label, row, "Suffix");
label += "\n";
label += AddFieldValue(label, row, "Address1");
label += AddFieldValue(label, row, "AptNo");
label += "\n";
label += AddFieldValue(label, row, "City");
label += AddFieldValue(label, row, "State");
label += AddFieldValue(label, row, "Zip");
Console.WriteLine(label);
Console.WriteLine();
}
}
labelLen = label.Length;
label += AddFieldValue(label, row, "Address2");
if (label.Length != labelLen)
label += "\n";
label += AddFieldValue(label, row, "City");
label += AddFieldValue(label, row, "State");
label += AddFieldValue(label, row, "Zip");
Console.WriteLine(label);
Console.WriteLine();
}
}

private string AddFieldValue(string label, DataRow row,
string fieldName)
{
if (!DBNull.Value.Equals(row[fieldName]))
return (string) row[fieldName] + " ";
else
return String.Empty;
}
// </Snippet1>
private string AddFieldValue(string label, DataRow row,
string fieldName)
{
if (!DBNull.Value.Equals(row[fieldName]))
return (string)row[fieldName] + " ";
else
return string.Empty;
}
// </Snippet1>
}
116 changes: 58 additions & 58 deletions snippets/csharp/System/DateTime/.ctor/ctorexample1.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
using System;

public class Example
{
public static void Main()
{
ShowYMD();
Console.WriteLine("-----");
ShowYMDHMS();
Console.WriteLine("-----");
ShowYMDHMSMs();
Console.WriteLine("-----");
ShowYMDHMSKind();
Console.WriteLine("-----");
ShowYMDHMSMsKind();
public static void Main()
{
ShowYMD();
Console.WriteLine("-----");
ShowYMDHMS();
Console.WriteLine("-----");
ShowYMDHMSMs();
Console.WriteLine("-----");
ShowYMDHMSKind();
Console.WriteLine("-----");
ShowYMDHMSMsKind();

void ShowYMD()
{
// <Snippet1>
DateTime date1 = new DateTime(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
// 8/18/2010 12:00:00 AM
// </Snippet1>
}
void ShowYMD()
{
// <Snippet1>
DateTime date1 = new(2010, 8, 18);
Console.WriteLine(date1.ToString());
// The example displays the following output:
// 8/18/2010 12:00:00 AM
// </Snippet1>
}

void ShowYMDHMS()
{
// <Snippet3>
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0);
Console.WriteLine(date1.ToString());
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:00 PM
// </Snippet3>
}
void ShowYMDHMS()
{
// <Snippet3>
DateTime date1 = new(2010, 8, 18, 16, 32, 0);
Console.WriteLine(date1.ToString());
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:00 PM
// </Snippet3>
}

void ShowYMDHMSMs()
{
// <Snippet5>
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500);
Console.WriteLine(date1.ToString("M/dd/yyyy h:mm:ss.fff tt"));
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:18.500 PM
// </Snippet5>
}
void ShowYMDHMSMs()
{
// <Snippet5>
DateTime date1 = new(2010, 8, 18, 16, 32, 18, 500);
Console.WriteLine(date1.ToString("M/dd/yyyy h:mm:ss.fff tt"));
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:18.500 PM
// </Snippet5>
}

void ShowYMDHMSKind()
{
// <Snippet7>
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 0, DateTimeKind.Local);
Console.WriteLine("{0} {1}", date1, date1.Kind);
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:00 PM Local
// </Snippet7>
}
void ShowYMDHMSKind()
{
// <Snippet7>
DateTime date1 = new(2010, 8, 18, 16, 32, 0, DateTimeKind.Local);
Console.WriteLine($"{date1} {date1.Kind}");
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:00 PM Local
// </Snippet7>
}

void ShowYMDHMSMsKind()
{
// <Snippet8>
DateTime date1 = new DateTime(2010, 8, 18, 16, 32, 18, 500,
DateTimeKind.Local);
Console.WriteLine("{0:M/dd/yyyy h:mm:ss.fff tt} {1}", date1, date1.Kind);
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:18.500 PM Local
// </Snippet8>
}
}
void ShowYMDHMSMsKind()
{
// <Snippet8>
DateTime date1 = new(2010, 8, 18, 16, 32, 18, 500,
DateTimeKind.Local);
Console.WriteLine($"{date1:M/dd/yyyy h:mm:ss.fff tt} {date1.Kind}");
// The example displays the following output, in this case for en-us culture:
// 8/18/2010 4:32:18.500 PM Local
// </Snippet8>
}
}
}
63 changes: 27 additions & 36 deletions snippets/csharp/System/DateTime/.ctor/ctorexample2.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// <Snippet2>
using System;
using System.Globalization;
Expand All @@ -6,46 +6,37 @@

public class Example
{
public static void Main()
{
Console.WriteLine("Using the Persian Calendar:");
PersianCalendar persian = new PersianCalendar();
DateTime date1 = new DateTime(1389, 5, 27, persian);
Console.WriteLine(date1.ToString());
Console.WriteLine("{0}/{1}/{2}\n", persian.GetMonth(date1),
persian.GetDayOfMonth(date1),
persian.GetYear(date1));
public static void Main()
{
Console.WriteLine("Using the Persian Calendar:");
PersianCalendar persian = new();
DateTime date1 = new(1389, 5, 27, persian);
Console.WriteLine(date1.ToString());
Console.WriteLine($"{persian.GetMonth(date1)}/{persian.GetDayOfMonth(date1)}/{persian.GetYear(date1)}\n");

Console.WriteLine("Using the Hijri Calendar:");
// Get current culture so it can later be restored.
CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("Using the Hijri Calendar:");
// Get current culture so it can later be restored.
CultureInfo dftCulture = Thread.CurrentThread.CurrentCulture;

// Define Hijri calendar.
HijriCalendar hijri = new HijriCalendar();
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SY");
CultureInfo current = CultureInfo.CurrentCulture;
current.DateTimeFormat.Calendar = hijri;
string dFormat = current.DateTimeFormat.ShortDatePattern;
// Ensure year is displayed as four digits.
dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy");
current.DateTimeFormat.ShortDatePattern = dFormat;
DateTime date2 = new DateTime(1431, 9, 9, hijri);
Console.WriteLine("{0} culture using the {1} calendar: {2:d}", current,
GetCalendarName(hijri), date2);
// Define Hijri calendar.
HijriCalendar hijri = new();
// Make ar-SY the current culture and Hijri the current calendar.
Thread.CurrentThread.CurrentCulture = new("ar-SY");
CultureInfo current = CultureInfo.CurrentCulture;
current.DateTimeFormat.Calendar = hijri;
string dFormat = current.DateTimeFormat.ShortDatePattern;
// Ensure year is displayed as four digits.
dFormat = Regex.Replace(dFormat, "/yy$", "/yyyy");
current.DateTimeFormat.ShortDatePattern = dFormat;
DateTime date2 = new(1431, 9, 9, hijri);
Console.WriteLine($"{current} culture using the {GetCalendarName(hijri)} calendar: {date2:d}");

// Restore previous culture.
Thread.CurrentThread.CurrentCulture = dftCulture;
Console.WriteLine("{0} culture using the {1} calendar: {2:d}",
CultureInfo.CurrentCulture,
GetCalendarName(CultureInfo.CurrentCulture.Calendar),
date2);
}
// Restore previous culture.
Thread.CurrentThread.CurrentCulture = dftCulture;
Console.WriteLine($"{CultureInfo.CurrentCulture} culture using the {GetCalendarName(CultureInfo.CurrentCulture.Calendar)} calendar: {date2:d}");
}

private static string GetCalendarName(Calendar cal)
{
return Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
}
private static string GetCalendarName(Calendar cal) => Regex.Match(cal.ToString(), "\\.(\\w+)Calendar").Groups[1].Value;
}
// The example displays the following output:
// Using the Persian Calendar:
Expand Down
Loading
Loading