【发布时间】:2011-07-22 15:02:07
【问题描述】:
我的代码中有错误:
ExecuteNonQuery:连接属性尚未初始化。
这可能是由于此代码中的行:
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");
完整代码:
{
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);
//FileUploadControl.SaveAs(Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/");
FileUploadControl.SaveAs(Path.Combine(fileuploadpath, filename));
StatusLabel.Text = "Upload status: File uploaded!";
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
还有一个问题,我不认为它是我想要的插入,因为这会在我的数据库中创建重复条目,是否只是将其从 INSERT INTO 更改为 UPDATE 的情况?
还有没有办法在上传图片时覆盖?自动取款机只是将图像保存到与我已有的文件夹相同的文件夹中吗?第一张图像或任何图像显然不会具有相同的文件名,那么我将如何用我上传的那个覆盖文件夹中的任何图像?
编辑:
新错误(fileupload 工作正常,因为它存储在正确的区域,但将 fileupload 传递给 insert 语句有点不靠谱)
我得到了错误
无法上传文件。发生以下错误:ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.5.9]您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''C:\Users\Garrith\Documents\Visual Studio 2010\WebSites\WebSite1\userdata\1\uplo' 附近使用正确的语法
这有什么奇怪的?
我想要做的就是将文件路径+文件名保存在 mydb 中,我试图传递给插入的尝试显然失败了。
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
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();
string filename = Path.GetFileName(FileUploadControl.FileName);
//FileUploadControl.SaveAs(Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/");
FileUploadControl.SaveAs(Path.Combine(fileuploadpath, filename));
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;
}
}
}
}
正如你在这一行看到的:
VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn);
我错过了“文件名”我试过这个:
VALUES ('" + theUserId + "' , '" + fileuploadpath, filename + "')", cn);
便宜的镜头哈哈,但我想值得一试,它像往常一样哭泣!
【问题讨论】:
-
您遇到了一些问题。该命令必须与连接相关联。此外,将 fileuploadpath 作为插入中的值传递将不起作用。您需要读取文件 (File.ReadAllBytes) 并将 byte[] 作为参数传递给插入。
-
他并没有尝试将文件字节存储在数据库中......只是他保存文件的路径。
-
是的,反理智是正确的,但我又犯了一个错误,新的东西会更新帖子
-
您的第二个错误可能是因为您没有正确转义路径。更好的方法是使用 OdbcParameters,它应该为您处理转义。
-
@Garrith:如果你指的是我,那么我已经做到了...... =]