2011-03-25 93 views
0

我设计我的网页:插入图像和thenfilepath到数据库

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 
+2

一个offtopic的评论︰System.Windows.Forms.MessageBox.Show - 将无法在ASPX – Anuraj 2011-03-25 06:26:49

+0

@vs dev它不是真实的,它会工作,但它会显示在服务器端 – 2011-03-25 06:45:56

+0

这里没有问题。 – carlsb3rg 2011-03-25 06:49:30

回答

2

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

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

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

建议选择一个:

  • 继续与二进制保存到数据库中,你有以上。
  • byte[]作为文件保存到磁盘上的某个位置。使用该文件名,如下所示:
cmd.Parameters.Add("@ImagePath", SqlDbType.NVarChar).Value = myImagePathOnDisk; 
+0

@ p.campbell我使用但错误是无法转换参数值从一个字节[]到一个字符串。 – vimal 2011-04-02 05:23:17

+0

@Sachin Shanbhag错误无法将参数值从字节[]转换为字符串。 – vimal 2011-04-02 05:29:13

+1

@vimal:你需要实现一些代码来实际保存图像到磁盘,确定磁盘上的文件名,并将其用作'myImagePathOnDisk'。我的回答只是试图引导你一个全面的解决方案。 – 2011-04-02 05:29:29

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 
+0

谢谢,但只有iwant图像位置 – vimal 2011-04-02 06:43:53

+0

没有工作超过错误 – vimal 2011-04-02 11:50:16

2

你只需要通过就可能把店面形象从文件路径做代码2线和可以能够存储到数据库中。

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

imageBytes是你感兴趣的牵着你的图像的字节数组整体的一部分,你只需要插入该图像数据类型的表列。

相关问题