日期:2014-05-18  浏览次数:20904 次

用C#做图书馆管理系统,如何将图片保存到SQL server 中的表中
我是一个才接触C#不久的学生,最近在做一个图书馆管理系统,不知道怎么将读者的图片保存到SQL server中的表中,谁可以帮帮我解决一下这个问题,非常感谢你的帮助

------解决方案--------------------
http://blog.csdn.net/sx811125/article/details/4408016
------解决方案--------------------
C# code


//图片保存到数据库中

public void AddPicture(string lastName, string firstName, string title, DateTime hireDate, int reportsTo, string photoFilePath, string connectionString)
{
  byte[] photo = GetPhoto(photoFilePath);

  SqlConnection connection = new SqlConnection(connectionString)

  SqlCommand command = new SqlCommand("INSERT INTO Employees (LastName, FirstName,Title,HireDate,ReportsTo, Photo) Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, @Photo)", connection); 

  command.Parameters.Add("@LastName",  SqlDbType.NVarChar, 20).Value = lastName;
  command.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = firstName;
  command.Parameters.Add("@Title", SqlDbType.NVarChar, 30).Value = title;
  command.Parameters.Add("@HireDate", SqlDbType.DateTime).Value = hireDate;
  command.Parameters.Add("@ReportsTo",  SqlDbType.Int).Value = reportsTo;

  command.Parameters.Add("@Photo",SqlDbType.Image, photo.Length).Value = photo;

  connection.Open();
  command.ExecuteNonQuery();
  }


// 读二进制流的方法。

public static byte[] GetPhoto(string filePath)
{
  FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  BinaryReader reader = new BinaryReader(stream);

  byte[] photo = reader.ReadBytes((int)stream.Length);

  reader.Close();
  stream.Close();

  return photo;
}

//从数据库读图片到picturebox
public void ReaderPicture()

{
SqlConnection conn=new SqlConnectio(@"data source=chenyuming2004VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;

   pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//拉伸图片

------解决方案--------------------
一般都不把图片存到数据库里面去,那样数据库负重会太大,可以把图片放到硬盘上,在数据库中仅仅存图片绝对地址就行了。
------解决方案--------------------
个人建议不要把图片存在数据库,从数据库存取图片效率非常低,最好直接存在目录下,然后进行检索。
探讨
我是一个才接触C#不久的学生,最近在做一个图书馆管理系统,不知道怎么将读者的图片保存到SQL server中的表中,谁可以帮帮我解决一下这个问题,非常感谢你的帮助