我的代码出错:
ExecuteOnQuery:连接属性尚未初始化。
这可能是由于代码中的行:
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;
}
}
}
}
还有一个问题,我不认为这是我想要的一个插入,因为这将在我的数据库中产生一个重复的条目,这只是一个将它从插入更改为更新的情况吗?
还有一种方法可以在上传图像时覆盖它吗?自动取款机它只是把图像保存到和我已有的相同的文件夹里?第一个图像或任何图像显然都不会有相同的文件名,所以我该如何使用一个im上载覆盖文件夹中的任何图像?
编辑:
新错误(fileupload的工作原理是它存储在正确的区域中,但是将fileupload传递给insert语句有点不稳定)
我明白错误
无法上载文件。出现以下错误:error[42000][MySQL][ODBC 3.51 Driver][mysqld-5.5.9]您的SQL语法有错误;请检查与MySQL服务器版本对应的手册,以获取在第1行“C:\ Users\Garrith\Documents\Visual Studio 2010\WebSite1\userdata\1\uplo”附近使用的正确语法
哪个有点奇怪?
我所要做的就是将文件路径+文件名保存在mydb中,我试图传递到insert显然失败了。
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);
廉价的枪声哈哈,但值得一试,我想它哭了,因为它总是这样!
最佳答案
您想使用OdbcCommand
上的CreateCommand
创建OdbcConnection
。所发布的代码不会将cmd
绑定到cn
。另外,应该使用CommandParameters而不是内联值(以防止SQL注入攻击)。
OdbcCommand cmd = cn.CreateCommand();
cmd.CommandText = "INSERT INTO Pictures (UserID, picturepath) VALUES (?, ?)";
cmd.Parameters.Add(new OdbcParameter("@UserID", OdbcType.Int, theUserID));
cmd.Parameters.Add(new OdbcParameter("@picturepath", OdbcType.VarChar, fileuploadpath));
cmd.ExecuteNonQuery();
关于c# - ExecuteNonQuery:连接属性尚未初始化?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5427945/