如何将文件路径从我的代码中取出并放入 mysql 的插入语句中?
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;
}
}
}
}
最佳答案
改变
FileUploadControl.SaveAs(Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
至
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/");
FileUploadControl.SaveAs(Path.Combine(fileuploadpath , filename));
然后 fileuploadpath 将可供您输入查询。
当然,你的问题不是很详细。您是否想要客户端将文件存储在其计算机上的本地路径?
仅供引用:使用参数而不是串联。
关于c# - 如何将文件路径从我的代码中取出并放入 mysql 的插入语句中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425449/