2011-03-24 6 views
0

ファイルパスを自分のコードから抜き出し、mysqlのinsert文に入れますか?ファイルパスを自分のコードから抜き出し、mysqlの挿入ステートメントに入れますか?

string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/"); 
FileUploadControl.SaveAs(Path.Combine(fileuploadpath , filename)); 

 protected void UploadButton_Click(object sender, EventArgs e) 
      { 
      string theUserId = Session["UserID"].ToString(); 
      { 
       OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"); 
       cn.Open(); 
      } 
      if (FileUploadControl.HasFile) 
         { 
          try 
          { 
           string filename = Path.GetFileName(FileUploadControl.FileName); 
           string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/"); 
           FileUploadControl.SaveAs(Path.Combine(fileuploadpath, + filename)); 
//error on this line best overload method has some invalid arguements 
           StatusLabel.Text = "Upload status: File uploaded!"; 

           //some kind of function to take the path then enter it into my insert syntax? 

           OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('"+ theUserId +"' , '"+ fileuploadpath +"')", cn); 
           cmd.ExecuteNonQuery(); 
          } 

          catch (Exception ex) 
          { 
           StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

          } 

         } 
        } 

       } 

答えて

1

変更

FileUploadControl.SaveAs(Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename); 

あなたのクエリに入れて、次いでfileuploadpathが利用できるようになります。

もちろん、あなたの質問はあまり詳しくはありません。クライアントがファイルをマシンに保存していた場所からのローカルパスが必要でしたか?

FYI:連結の代わりにパラメータを使用します。

+0

私はこの行でエラーが発生します:FileUploadControl.SaveAs(Path.Combine(fileuploadpath、+ filename)); (括弧内のエラー) –

+0

最適なオーバーロードメソッドに無効な引数があります –

+0

oops、typo。一定 –

関連する問題