2012-08-14 109 views
0

我一直在试图发现的是,如何将图像的BLOB存储到我的数据库中,而不必先将它保存到文件系统中,直接从服务器的内存中保存。如何将图像的BLOB保存到sql数据库中而不保存到文件系统中?

我使用一个sql服务器和其他形式的信息,我有2个图像需要存储在数据库中。我还想知道如何将它们读出来并将它们转换回图像。

在数据库我有“缩略图”,它是类型“图像”。如果我没有错,那应该是正确的。

对于图像上传我用下面的ASP控制:

<asp:FileUpload ID="_imageUpload" runat="server" /> 

,因为我很新的使用数据库与网站一起特别是我还从来没有做过这样的事。

哦,对不起,如果这个问题已被问及已回答。

在此先感谢!

[编辑]

我的整个代码:

protected void _uploadImageBtn_Click(object sender, EventArgs e) 
{ 
    string extension; 

    // checks if file exists 
    if (!_imageUpload.HasFile) 
    { 
     _resultLbl.Text = "Please, Select a File!"; 
     return; 
    } 

    // checks file extension 
    extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower(); 

    if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png")) 
    { 
     _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed."; 
     return; 
    } 

    // checks if image dimensions are valid 
    if (!ValidateFileDimensions(140, 152)) 
    { 
     _resultLbl.Text = "Maximum allowed dimensions are: width 1520px and height <= 140px."; 
     return; 
    } 

    int fileLen; 
    string displayString = ""; 

    // Get the length of the file. 
    fileLen = _imageUpload.PostedFile.ContentLength; 

    // Create a byte array to hold the contents of the file. 
    byte[] input = new byte[fileLen - 1]; 
    input = _imageUpload.FileBytes; 

    // Copy the byte array to a string. 
    for (int loop1 = 0; loop1 < fileLen; loop1++) 
    { 
     displayString = displayString + input[loop1].ToString(); 
    } 

    try 
    { 

     SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial Catalog=database;User ID=user;Password=pw"); 

     string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)"; 

     SqlCommand sqlCom = new SqlCommand(qry, sqlCn); 

     sqlCom.Parameters.Add("@thumbnail", SqlDbType.Image, input.Length).Value = input; 

     sqlCn.Open(); 
     sqlCom.ExecuteNonQuery(); 
     sqlCn.Close(); 

    } 

    catch (Exception) 
    { 
     (...) 
    } 
} 

public bool ValidateFileDimensions(int aHeight, int aWidth) 
{ 
    using (System.Drawing.Image image = System.Drawing.Image.FromStream(_imageUpload.PostedFile.InputStream)) 
    { 
     return (image.Height == aHeight && image.Width == aWidth); 
    } 
} 

回答

4

您可以保存FileUpload.FileBytes返回字节数组。

if(_imageUpload.HasFile) 
{ 
byte[] imageData = _imageUpload.FileBytes; 

using(SqlConnection sqlCn = new SqlConnection("Server=localhost;database=databaseName;uid=userName;pwd=password")) 
    { 
    string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)"; 
    using(SqlCommand sqlCom = new SqlCommand(qry, sqlCn)) 
    { 
     sqlCom.Parameters.Add("@thumbnail", 
           SqlDbType.Image, 
           imageData.Length).Value=imageData; 
     sqlCn.Open(); 
     sqlCom.ExecuteNonQuery(); 
     sqlCn.Close(); 
    } 
    } 
} 

编辑:

protected void _uploadImageBtn_Click(object sender, EventArgs e) 
{ 
    string extension; 

    // checks if file exists 
    if (!_imageUpload.HasFile) 
    { 
     _resultLbl.Text = "Please, Select a File!"; 
     return; 
    } 

    // checks file extension 
    extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower(); 

    if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png")) 
    { 
     _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed."; 
     return; 
    } 

    // checks if image dimensions are valid 
    if (!ValidateFileDimensions(140, 152)) 
    { 
     _resultLbl.Text = "Maximum allowed dimensions are: width 1520px and height <= 140px."; 
     return; 
    } 


    byte []input = _imageUpload.FileBytes; 
    SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial 
           Catalog=database;User ID=user;Password=pw"); 

    string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)"; 
    SqlCommand sqlCom = new SqlCommand(qry, sqlCn); 
    sqlCom.Parameters.Add("@thumbnail", SqlDbType.Image, input.Length).Value = input; 
    sqlCn.Open(); 
    sqlCom.ExecuteNonQuery(); 
    sqlCn.Close(); 
} 
+0

哎,我试了好几次,我只是调试它,因为INSERT总是失败,发生这种情况,因为(在这里你的榜样)的“字节”数组包含无值,所以它试图填充NULL,这当然不起作用,因为它是不允许的。你确定这应该起作用吗? – webster69 2012-08-15 13:54:33

+0

我该怎么做?我上面更新了我的代码,所以你可以看到我做了什么 – webster69 2012-08-15 14:40:02

+0

我实际上已经这样做了:'if(!_imageUpload.HasFile) { _resultLbl.Text =“Please,Select a File!”; return; }' – webster69 2012-08-15 14:44:05