📄 bookmanager.cs
字号:
// }
// }
// return book;
// }
// }
//}
//增加类别
public static void AddBookClass(string name, string remark,byte [] orginal)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("AddCategory", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@name", name));
command.Parameters.Add(new SqlParameter("@remark", remark));
command .Parameters .Add (new SqlParameter("@image",ResizeImageFile(orginal,100)));
connection.Open();
command.ExecuteNonQuery();
}
}
}
//增加图书
public static void AddBook(string BookName,int CategoryID,byte[]orginal,string ISDN,string BookTips)
{
MembershipUser user=Membership.GetUser ();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("AddBook", connection))
{
command.CommandType = CommandType.StoredProcedure;
command .Parameters .Add (new SqlParameter ("@BookName",BookName ));
command.Parameters.Add(new SqlParameter("@CategoryID", CategoryID));
command.Parameters.Add(new SqlParameter("@BookImageS", ResizeImageFile(orginal, 100)));
command.Parameters.Add(new SqlParameter("@BookImageL", ResizeImageFile(orginal, 250)));
command.Parameters.Add(new SqlParameter("@ISBN", ISDN ));
command .Parameters .Add (new SqlParameter ("@UserID",user .UserName .ToString ()));
command.Parameters.Add(new SqlParameter("@BookTips", BookTips));
command.Parameters.Add(new SqlParameter("@AddDateTime", DateTime.Now));
connection.Open();
command.ExecuteNonQuery();
}
}
}
//修改图书
public static void EditBook(int BookID,string BookName, int CategoryID, byte[] orginal, string ISDN, string BookTips)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("EditBook", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@BookID", BookID));
command.Parameters.Add(new SqlParameter("@BookName", BookName));
command.Parameters.Add(new SqlParameter("@CategoryID", CategoryID));
command.Parameters.Add(new SqlParameter("@BookImageS", ResizeImageFile(orginal, 100)));
command.Parameters.Add(new SqlParameter("@BookImageL", ResizeImageFile(orginal, 250)));
command.Parameters.Add(new SqlParameter("@ISBN", ISDN));
command.Parameters.Add(new SqlParameter("@BookTips", BookTips));
connection.Open();
command.ExecuteNonQuery();
}
}
}
//移除类别
public static void RemoveCategory(int CategoryID)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("RemoveCategory", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@categoryID", CategoryID));
connection.Open();
command.ExecuteNonQuery();
}
}
}
//移除图书
public static void RemoveBook(int BookID)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("RemoveBook", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@BookID", BookID));
connection.Open();
command.ExecuteNonQuery();
}
}
}
//获得图书封面图像
public static Stream GetBookImage(int bookid, ImageSize size)
{
using ( SqlConnection connection = new SqlConnection (ConfigurationManager .ConnectionStrings ["BookShow"].ConnectionString ))
{
using (SqlCommand command = new SqlCommand ("GetBookImage",connection ))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@BookID", bookid));
command .Parameters .Add (new SqlParameter ("@size",(int)size));
connection.Open();
object result = command.ExecuteScalar();
try
{
return new MemoryStream((byte[])result);
}
catch
{
return null;
}
}
}
}
//获得类别图片
public static Stream GetClassImage(int categoryid, ImageSize size)
{
using (SqlConnection connection = new SqlConnection (ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString ))
{
using(SqlCommand command = new SqlCommand ("GetClassImage",connection ))
{
command .CommandType=CommandType.StoredProcedure;
command .Parameters .Add (new SqlParameter ("@categoryid",categoryid ));
connection .Open ();
object result=command .ExecuteScalar();
try
{
return new MemoryStream ((byte[])result );
}
catch
{return null;}
}
}
}
//当无图像时,获得无图像封面
public static Stream GetImage(ImageSize size)
{
string path =HttpContext.Current .Server.MapPath("~/Images/");
switch (size)
{
case ImageSize .Small :
path += "placeholder-100.jpg";
break;
case ImageSize .Large:
path += "placeholder-600.jpg";
break;
default :
path += "placeholder-600.jpg";
break;
}
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
//计算维度, 当高比宽高时,按高为基础计算比例,当宽更大时,刚以宽为基础
private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();
if (oldSize.Height > oldSize.Width)
{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
newSize.Height = targetSize;
}
else
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
return newSize;
}
//比例缎缩放图像。
private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width ,newSize.Height ,System .Drawing .Imaging.PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas .DrawImage(oldImage ,new Rectangle(new Point (0,0),newSize ));
MemoryStream m = new MemoryStream ();
newImage.Save (m,System .Drawing .Imaging .ImageFormat.Jpeg );
return m.GetBuffer ();
}
}
}
}
//修改类别
public static void EidtCategory(string CategoryName, string CategoryRemark, byte[] orginal, int CategoryID)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShow"].ConnectionString))
{
using (SqlCommand command = new SqlCommand ("EidtCategory",connection ))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters .Add (new SqlParameter("@CategoryID",CategoryID));
command.Parameters.Add(new SqlParameter("@CategoryName", CategoryName));
command.Parameters.Add(new SqlParameter("@CategoryRemark", CategoryRemark));
command.Parameters.Add(new SqlParameter("@CategoryImage",ResizeImageFile (orginal ,100)));
connection.Open();
command.ExecuteNonQuery();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -