⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 report.cs

📁 微软系列丛书<<C#2005从入门到精通>>
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ReportOrders
{
	class Report
	{
		static void Main(string[] args)
		{
            SqlConnection dataConnection = new SqlConnection();

			try
			{	
				dataConnection.ConnectionString = "Integrated Security=true;" +
												  "Initial Catalog=Northwind;" +
												  "Data Source=.\\SQLExpress";
				dataConnection.Open();

				Console.Write("Please enter a customer ID (5 characters) ");
				string customerId = Console.ReadLine();

				SqlCommand dataCommand = new SqlCommand();
				dataCommand.Connection = dataConnection;
				dataCommand.CommandText =
					"SELECT OrderID, OrderDate, " +
					"ShippedDate, ShipName, ShipAddress, ShipCity, " +
					"ShipCountry ";
				dataCommand.CommandText +=
					"FROM Orders WHERE CustomerID='" +
					customerId + "'";
				Console.WriteLine("About to execute: {0}\n\n", dataCommand.CommandText);
				SqlDataReader dataReader = dataCommand.ExecuteReader();

				while (dataReader.Read())
				{
					int orderId = dataReader.GetInt32(0);
					if (dataReader.IsDBNull(2))
					{
						Console.WriteLine("Order {0} not yet shipped\n\n", orderId);
					}
					else
					{
						DateTime orderDate = dataReader.GetDateTime(1);
						DateTime shipDate = dataReader.GetDateTime(2);
						string shipName = dataReader.GetString(3);
						string shipAddress = dataReader.GetString(4);
						string shipCity = dataReader.GetString(5);
						string shipCountry = dataReader.GetString(6);
						Console.WriteLine(
							"Order {0}\nPlaced {1}\nShipped {2}\n" +
							"To Address {3}\n{4}\n{5}\n{6}\n\n", orderId, orderDate,
							shipDate, shipName, shipAddress, shipCity, shipCountry);
					}
				}
                dataReader.Close();
            }

			catch(Exception e)
			{
				Console.WriteLine("Error accessing the database " + e.Message);
			}

            finally
            {
                dataConnection.Close();
            }
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -