2012-06-14 39 views
0

我想从ASP.Net后端检索图像。使用SQL SERVER 2005作为后端。我已经尝试了n个代码,包括在线提供的代码。任何人都可以指导我解决这个问题。无法检索或存储数据库中的图像SQL Server 2005

我的代码如下

表设计: -

create table Image 
(
ImageId Int identity (1,1),ImageName Varchar(50), Image image 
) 

代码: -

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindGridData(); 
     } 
    } 
    string strcon = "Data Source=SUJITHA\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (FileUpload1.HasFile) 
     { 
      //getting length of uploaded file 
      int length = FileUpload1.PostedFile.ContentLength; 
      //create a byte array to store the binary image data 
      byte[] imgbyte = new byte[length]; 
      //store the currently selected file in memeory 
      HttpPostedFile img = FileUpload1.PostedFile; 
      //set the binary data 
      img.InputStream.Read(imgbyte, 0, length); 
      string imagename =TextBox1.Text; 
      //use the web.config to store the connection string 
      SqlConnection connection = new SqlConnection(strcon); 
      connection.Open(); 
      SqlCommand cmd = new SqlCommand("INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)", connection); 
      cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename; 
      cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte; 
      int count = cmd.ExecuteNonQuery(); 
      connection.Close(); 
      if (count == 1) 
      { 
       BindGridData(); 
       TextBox1.Text = string.Empty; 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true); 
      } 
     } 
    } 

    private void BindGridData() 
    { 
     SqlConnection connection = new SqlConnection(strcon); 
     SqlCommand command = new SqlCommand("SELECT ImageName,Image from [Image]", connection); 
     SqlDataAdapter daimages = new SqlDataAdapter(command); 
     DataTable dt = new DataTable(); 
     daimages.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
     GridView1.Attributes.Add("bordercolor", "black"); 
    } 
+0

有一些特定的错误或异常类?你有什么迹象表明它不起作用?当你调试代码时,观察到的行为在什么时候偏离了预期的行为? – David

+0

这是你在找什么:http://www.codeproject.com/Articles/10861/Storing-and-Retrieving-Images-from-SQL-Server-usin – Rumplin

+5

你应该使用'VARBINARY(MAX)'数据类型 - 'IMAGE'已被SQL Server 2005弃用。 –

回答

0
*/Your code Like this*   

    **//Insert the file into database** 

      string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"; 

      SqlCommand cmd = new SqlCommand(strQuery); 

      cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename; 

      cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-word"; 

      cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 

      InsertUpdateData(cmd); 

    **//Select the file from database** 
     string strQuery = "select Name, ContentType, Data from tblFiles where [email protected]"; 

     SqlCommand cmd = new SqlCommand(strQuery); 

     cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1; 

     DataTable dt = GetData(cmd); 

     if (dt != null) 

     { 

      download(dt); 

     } 

     private void download (DataTable dt) 

     { 

      Byte[] bytes = (Byte[])dt.Rows[0]["Data"]; 

      Response.Buffer = true; 

      Response.Charset = ""; 

      Response.Cache.SetCacheability(HttpCacheability.NoCache); 

      Response.ContentType = dt.Rows[0]["ContentType"].ToString(); 

      Response.AddHeader("content-disposition", "attachment;filename=" 

      + dt.Rows[0]["Name"].ToString()); 

      Response.BinaryWrite(bytes); 

      Response.Flush(); 

      Response.End(); 

     } 
private DataTable GetData(SqlCommand cmd) 
    { 
     DataTable dt = new DataTable(); 
     String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     try 
     { 
      con.Open(); 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
      return dt; 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      con.Close(); 
      sda.Dispose(); 
      con.Dispose(); 
     } 
    } 
+0

亲爱的Nikhil,你可以告诉你是否使用任何名称空间/程序集来使用getdata() – Arjun

+0

我忘了对不起:我添加了Getdata函数:chk我的编辑 –

1
SqlConnection con = new SqlConnection(@"Data Source=AMAR-PC\SQLEXPRESS;Initial Catalog=a;User ID=sa;Password=amar"); 
string path = Server.MapPath("Images/"); 

if (FileUpload1.HasFile) 
{ 
    byte[] img = new byte[FileUpload1.PostedFile.ContentLength]; 
    HttpPostedFile myimage = FileUpload1.PostedFile; 
    myimage.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength); 
    SqlCommand cmd = new SqlCommand("insert into images values ('" + TextBox1.Text + "','" + FileUpload1.PostedFile + "')", con); 

    con.Open(); 

这是我做了什么......我也知道有很多其他的方式来上传文件.... 现在我想的是一个上传的PIC应该在图像控制来dispayed ....你可以帮我

0

的东西夫妇担心我

  • 使用master数据库

  • 你的表名是image这是SQL Server

如果您需要命名与保留名称,你NE表中的保留字ED与方括号引用他们:INSERT INTO [Image] ...

相关问题