日期:2014-05-18  浏览次数:20460 次

null的用途,感觉不到它的存在价值
请问下null的用途,我这里的null删去了会有什么问题啊,我删去感觉不到有问题, 请教大家

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace LoadImages
{
  class LoadImages
  {
  string imageFileLocation =
  @"C:\Program Files\Microsoft.NET\SDK\v2.0\QuickStart\"
  + @"aspnet\samples\monitoring\tracing\Images\";

  string imageFilePrefix = "milk";
  int numberImageFiles = 8;
  string imageFileType = ".gif";
  int maxImageSize = 10000;
  SqlConnection conn = null;
SqlCommand cmd = null;

  static void Main()
  {
  LoadImages loader = new LoadImages();

  try
  {
  // Open connection
  loader.OpenConnection();
  // Create command
  loader.CreateCommand();
  // Create table
  loader.CreateImageTable();
  // Prepare insert
  loader.PrepareInsertImages();
  // Insert images
  int i;
  for (i = 1; i <= loader.numberImageFiles; i++)  
  {
  loader.ExecuteInsertImages(i);
  }
  }
  catch (SqlException ex)
  {
  Console.WriteLine(ex.ToString());
  }
  finally
  {
  loader.CloseConnection();
  }
  }

  void OpenConnection()
  {
  // Create connection
  conn = new SqlConnection(@"
  server = .\sqlexpress;
  integrated security = true;
  database = tempdb
  ");
  // Open connection
  conn.Open();
  }

  void CloseConnection()
  {
  // close connection
  conn.Close();
  Console.WriteLine("Connection Closed."); 
  }

  void CreateCommand()
  {
  cmd = new SqlCommand();
  cmd.Connection = conn;
  }

  void ExecuteCommand(string cmdText)
  {
  int cmdResult;
  cmd.CommandText = cmdText;
  Console.WriteLine("Executing command:");
  Console.WriteLine(cmd.CommandText);
  cmdResult = cmd.ExecuteNonQuery();
  Console.WriteLine("ExecuteNonQuery returns {0}.", cmdResult); 
  }

  void CreateImageTable()
  {
  ExecuteCommand(@"
  create table imagetable
  (
  imagefile nvarchar(20),
  imagedata varbinary(max)
  )
  ");
  }

  void PrepareInsertImages()
  {
  cmd.CommandText = @"
  insert into imagetable
  values (@imagefile, @imagedata)
  ";
  cmd.Parameters.Add("@imagefile", SqlDbType.NVarChar, 20);
  cmd.Parameters.Add("@imagedata", SqlDbType.Image, 1000000);

  cmd.Prepare();
  }