遇不明代码,求大侠们拔刀相助,这是个往数据库存储图片的代码
代码如下,问题嵌套其中,请老老师慷慨相助,最好分点解答,这样菜鸟我比较明白
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 PrepareInsert