📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MobileDevelopersHandbook
{
public partial class Form1 : Form
{
// TODO: Change this to your own server name
private readonly string Hostname = "myServer";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Check that the Hostname is updated
System.Diagnostics.Debug.Assert(this.Hostname != "myServer", "You must change the value of the Hostname field to the name of your Database Server Instance");
}
private void buttonGetData_Click(object sender, EventArgs e)
{
// Get the user credentials, if not already set
if (NetworkCredentials.Instance.Username == null)
{
using (NetworkCredentialsForm dlg = new NetworkCredentialsForm())
{
dlg.ShowDialog();
}
}
SqlConnection conSql;
SqlDataReader rdrOvertimeRates = null;
listBoxResults.Items.Clear();
labelStatus.Text = "Connecting...";
this.Refresh();
string connString = "Server=" + Hostname + ";Database=DotNetCF;Integrated Security=true;UID="
+ NetworkCredentials.Instance.Username + ";Password=" + NetworkCredentials.Instance.UserPassword;
using (conSql = new SqlConnection(connString))
{
using (SqlCommand sqlGetOvertimeRates = new SqlCommand(
"SELECT OvertimeRateID,Description " +
"FROM OvertimeRates " +
"ORDER BY Description ",
conSql))
{
try
{
//open the connection
conSql.Open();
//get the records using a reader
rdrOvertimeRates = sqlGetOvertimeRates.ExecuteReader();
//put them into the list box
while (rdrOvertimeRates.Read())
{
listBoxResults.Items.Add(rdrOvertimeRates.GetString(1));
}
}
catch (SqlException errSql)
{
DisplaySQLErrors(errSql);
}
finally
{
//always close the connection - we dont have many to play with
if (rdrOvertimeRates != null)
rdrOvertimeRates.Close();
conSql.Close();
}
}
}
labelStatus.Text = "Done";
}
private void DisplaySQLErrors(SqlException e)
{
SqlErrorCollection errorCollection = e.Errors;
StringBuilder bld = new StringBuilder();
Exception inner = e.InnerException;
if (null != inner)
{
MessageBox.Show("Inner Exception: " + inner.ToString());
}
// Enumerate the errors to a message box.
foreach (SqlError err in errorCollection)
{
bld.Append("\n Error No: " + err.Number.ToString("X"));
bld.Append("\n Message : " + err.Message);
bld.Append("\n Server : " + err.Server);
bld.Append("\n Source : " + err.Source);
MessageBox.Show(bld.ToString());
bld.Remove(0, bld.Length);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -