📄 loadimages.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
namespace LoadImagesApp
{
class LoadImages
{
string imageFileLocation =
@"C:\Program Files\Microsoft Visual Studio .NET\" +
@"FrameworkSDK\Samples\QuickStart\aspplus\images\";
string imageFilePrefix = "cereal";
int numberImageFiles = 7;
string imageFileType = ".gif";
int maxImageSize = 10000;
SqlConnection imageConnection = null;
SqlCommand imageCommand = null;
void openConnection()
{
// create SqlConnection object
imageConnection = new SqlConnection(
@"Server=(local);" +
"Integrated Security=true;" +
"Connection Timeout=5;" +
"Database=NorthWind"
);
// Open connection
imageConnection.Open();
}
void closeConnection()
{
// close connection
imageConnection.Close();
// close was successful
Console.WriteLine("Connection Successfully Closed!");
}
void createCommand()
{
imageCommand = new SqlCommand();
imageCommand.Connection = imageConnection;
}
void executeCommand(string commandText)
{
int commandResult;
imageCommand.CommandText = commandText;
Console.WriteLine("Executing command:");
Console.WriteLine(imageCommand.CommandText);
commandResult = imageCommand.ExecuteNonQuery();
Console.WriteLine("ExecuteNonQuery returns {0}.", commandResult);
}
void createImageTable()
{
executeCommand(
"IF EXISTS ( " +
" SELECT TABLE_NAME " +
" FROM INFORMATION_SCHEMA.TABLES " +
" WHERE TABLE_NAME = 'imagetable' " +
") DROP TABLE imagetable "
);
executeCommand(
"CREATE TABLE imagetable ( " +
" imagefile NVARCHAR(20), " +
" imagedata IMAGE" +
")"
);
}
void prepareInsertImages()
{
imageCommand.CommandText =
"INSERT INTO imagetable VALUES (@ImageFile, @ImageData)";
imageCommand.Parameters.Add("@ImageFile", SqlDbType.NVarChar);
imageCommand.Parameters.Add("@ImageData", SqlDbType.Image);
}
void executeInsertImages(int imageFileNumber)
{
string imageFileName = null;
byte[] imageImageData = null;
imageFileName = imageFilePrefix+ imageFileNumber.ToString()+ imageFileType;
imageImageData = loadImageFile(imageFileName, imageFileLocation, maxImageSize);
imageCommand.Parameters["@ImageFile"].Value = imageFileName;
imageCommand.Parameters["@ImageData"].Value = imageImageData;
executeCommand(imageCommand.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);
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(maxImageSize);
Console.WriteLine("Imagebytes has length {0} bytes.", imagebytes.GetLength(0));
return imagebytes;
}
static void Main()
{
LoadImages loader = new LoadImages();
try
{
// open connection to database
loader.openConnection();
// create command object
loader.createCommand();
// create table
loader.createImageTable();
// prepare insert command
loader.prepareInsertImages();
int i;
for (i = 1; i<=loader.numberImageFiles; i++)
{
loader.executeInsertImages(i);
}
}
catch (SqlException ex) // catch exception if database error
{
Console.WriteLine(ex.ToString()); // display details of error
}
finally
{
loader.closeConnection(); // close connection
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -