c# - 如何将文件路径从我的代码中取出并放入 mysql 的插入语句中?

标签 c# asp.net mysql sql html

如何将文件路径从我的代码中取出并放入 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/

相关文章:

c# - ASP.NET Identity 2 记住我 - 用户正在注销

c# - DataGridAutoGeneratingColumnEventArgs.PropertyDescriptor 是什么类型的?

php - 如何加入帖子元和分类表wpdb mysql

MySQL比较字符串与国家字符

c# - 从字符串中获取随机的单词序列?

c# - 我可以在 Xamarin、C#、iOS 中保存对象数组(或 json 数组、字典数组)吗?

javascript - 如何使用 JavaScript 在 ASP.net 中显示动态内容

javascript - 根据 SQL 值更改 css

c# - 将 Id 或其他属性添加到 ASP.NET MVC 3 Html Helper Textbox

asp.net - ASP .NET - 配置 SQLDataSource 以使用 MySQL .NET 连接器