遇不明代码,求大侠们拔刀相助,这是个往数据库存储图片的代码
代码如下,问题嵌套其中,请老老师慷慨相助,最好分点解答,这样菜鸟我比较明白
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);[b][/b]
问题1:这里的loader.PrepareInsertImages();才执行了一次,那么久代表创建的imagetable表,只有一行才对啊,怎么执行loader.ExecuteInsertImages(i)切是8次,那么不是插入8行记录的吗,可是我们才剪了一个只有一行的表,怎么解释呢
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
loader.CloseConnection();
}
}
void OpenConnection()
{
// Create connection
conn = new SqlConnection(@"
[b]server = .\sqlexpress;
integrated security = true;
database = tempdb[/b]
");
/书给的结论是:(连接的数据库是temp,所以数据库的表总是临时的,即他们总是在SQL Server停止时删除)
请问这句话什么意思:A即自动执行代码中的conn.close()
B:自己打开的sql server 2008应用程序此时会自动关闭
C:右击我的电脑,查看sql server服务,你会看到sql server服务已经关闭
D:填写您的答案
// 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);
问题2:maximageSize=10000(这个赋值在开头就以声明),imagebytes = br.ReadBytes(maxImageSize);它代表什么啊,有什么意义
A:读入数据库的单个图片大小为10000字节
B:只读文件夹中的前10000个图片
C: 填写您的答案
Console.WriteLine(
"Imagebytes has length {0} bytes.",
imagebytes.GetLength(0)
);
return imagebytes;
}
}
}
执行代码后,我打开sql server2008点击temp数据库,里面根本没有表,我插入的图片也不知去哪里了!!!!!
从来没看过这种读取数据库的方法,求大侠指点:FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(maxImageSize);
给大家添乱了,希望老师指点
[解决办法]
1(连接的数据库是temp,所以数据库的表总是临时的,即他们总是在SQL Server停止时删除)
temp数据库是sqlserver系统数据库,作用是保存在sql中所使用到的临时表,临时表的生命周期很短,一个数据库连接结束就没有了,不会像实表一样长期保存,保存数据一定要自己建数据库并且使用实表存储。
2 maximageSize=10000(这个赋值在开头就以声明),imagebytes = br.ReadBytes(maxImageSize);它代表什么
读取字节长度
3 从来没看过这种读取数据库的方法,求大侠指点
这不是数据库读取方式,流的读取。是把文件转为字节流,通过流通道传输,这些基本操作你应该多看看资料。
[解决办法]
FileStream 文件流读取
[解决办法]
for (i = 1; i <= loader.imagetable.Rows.Count; i++)