将图像和文件路径插入数据库

时间:2011-03-25 06:12:02

标签: asp.net

我正在设计我的网页:

namespace photoshops
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
             onflbload(sender, e);
        }
        public void onflbload(object sender, EventArgs e)
        {
            // Create a byte[] from the input file

            int len = flbload.PostedFile.ContentLength;
            byte[] pic = new byte[len];
            flbload.PostedFile.InputStream.Read(pic, 0, len);
            // Insert the image and comment into the database

            SqlConnection connection = new SqlConnection(@"Data Source=DEVI\SQLEXPRESS; 
                          Initial Catalog =cat; Integrated Security=SSPI");

            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("insert into tblphotosettings " +
                    "(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total)  
                    values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)", connection);
                cmd.Parameters.Add("@BillNo", SqlDbType.NVarChar).Value = TextBox1.Text;
                cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value =TextBox2.Text;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = TextBox3.Text;
                cmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value = Rdbsdate.SelectedDate;
                cmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value = Rdbddate.SelectedDate;
                cmd.Parameters.Add("@Systemurl", SqlDbType.Image).Value = pic;
                SqlParameter Src = new SqlParameter("@FilePath", SqlDbType.VarChar, 450);
                Src.Value = pic.GetName();
                cmd.Parameters.Add(Src);

                cmd.Parameters.Add("@Numberofcopies", SqlDbType.NVarChar).Value =TextBox7.Text;
                cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;
                cmd.Parameters.Add("@Total", SqlDbType.NVarChar).Value = TextBox9.Text;
                cmd.ExecuteNonQuery();
            }

            finally
            {
                connection.Close();
            }
        }
    }
 }

我的错误

Error   1   'System.Array' does not contain a definition for 'GetName'   
and no extension method 'GetName' accepting a first argument of type 'System.Array' could 
be found (are you missing a using directive or an assembly reference?)  
C:\Documents and Settings\Administrator\My Documents\Visual Studio 
2008\Projects\photoshops\photoshops\photosetting.aspx.cs    52  29  photoshops

3 个答案:

答案 0 :(得分:2)

听起来你想要数据库中的图像位置。

您的代码实际上在byte[]语句中使用了INSERT

 cmd.Parameters.Add("@Systemurl", SqlDbType.Image).Value = pic;

建议选择一个:

  • 继续将二进制文件保存到数据库中。
  • byte[]作为文件保存到磁盘上的某个位置。像这样使用该文件名:
cmd.Parameters.Add("@ImagePath", SqlDbType.NVarChar).Value = myImagePathOnDisk;

答案 1 :(得分:2)

您只需要执行代码2行,即可以从文件路径存储图像,并且可以存储到数据库中。

string flPath = "C:\\1\\noimg.png";
byte[] imageBytes =  File.ReadAllBytes(flPath);

imageBytes是您感兴趣的部分,包含图像的整个字节数组,您只需要将其插入到image数据类型表的列中。

答案 2 :(得分:0)

string filename = FileUpload1.FileName.ToString();
         if (filename != "")
            {

                ImageName = FileUpload1.FileName.ToString();

                ImagePath = Server.MapPath("Images");
                SaveLocation = ImagePath + "\\" + ImageName;
                SaveLocation1 = "~/Image/" + ImageName;
                sl1 = "Images/" + ImageName;
                FileUpload1.PostedFile.SaveAs(SaveLocation);

            }...........try it for image