读书人

null的用途感觉不到它的存在价值,该

发布时间: 2012-04-21 14:34:44 作者: rapoo

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();
}

void ExecuteInsertImages(int imageFileNumber)
{
string imageFileName = null;


byte[] imageImageData = null;

imageFileName =
imageFilePrefix + imageFileNumber.ToString() + imageFileType;
imageImageData =
LoadImageFile(imageFileName, imageFileLocation, maxImageSize);

cmd.Parameters["@imagefile"].Value = imageFileName;
cmd.Parameters["@imagedata"].Value = imageImageData;

ExecuteCommand(cmd.CommandText);
}

byte[] LoadImageFile(
string fileName,
string fileLocation,
int maxImageSize
)
{
byte[] imagebytes = null;
string fullpath = fileLocation + fileName;
Console.WriteLine("Loading File:");
Console.WriteLine(fullpath);
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(maxImageSize);

Console.WriteLine(
"Imagebytes has length {0} bytes.",
imagebytes.GetLength(0)
);

return imagebytes;
}
}
}


[解决办法]
NULL OBJECT 空对象。。
[解决办法]

[解决办法]
SqlConnection conn 定义一个变量,内存里没有分配空间,
SqlConnection conn = null 定义一个变量,已经分配了空间,像空杯子。
不写null是可以编译,不过养成良好的编程习惯,合理分配内存,不要用到的时候再找杯子,而且有利于你判断对象是否为空。
[解决办法]
看你怎么用
[解决办法]

探讨

SqlConnection conn 定义一个变量,内存里没有分配空间,
SqlConnection conn = null 定义一个变量,已经分配了空间,像空杯子。
不写null是可以编译,不过养成良好的编程习惯,合理分配内存,不要用到的时候再找杯子,而且有利于你判断对象是否为空。

[解决办法]
初始化在内存

读书人网 >SQL Server

热点推荐