Upload images to SQL Server 2005 using ASP.Net MVC?

前端 未结 4 1046
闹比i
闹比i 2021-01-14 06:53

I know there is a way to upload images to the database as image type or varbinary type, however, I searched around the entire week, I am unable to find anything that can hel

4条回答
  •  情深已故
    2021-01-14 07:29

    Assuming you have a stored procedure called TestProc which takes a single argument called @data of type IMAGE, your C# code could be as follows:

    SqlConnection conn = new SqlConnection("");
    conn.Open();
    
    SqlCommand cmd = new SqlCommand("TestProc", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    
    SqlParameter param = new SqlParameter("@data", SqlDbType.Image);
    param.Value = System.IO.File.ReadAllBytes("any_file.jpg");
    cmd.Parameters.Add(param);
    
    cmd.ExecuteNonQuery();
    

    Let me know if you want the stored procedure code as well.

提交回复
热议问题