ExecuteNonQuery:连接属性尚未初始化? [英] ExecuteNonQuery: Connection property has not been initialized?
问题描述
在我的代码中出现错误:
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 =上传状态:文件已上传!;
$ b OdbcCommand cmd = new OdbcCommand(INSERT INTO Pictures(UserID,picturepath)VALUES(''+ theUserId +'','+ fileuploadpath +') );
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
StatusLabel.Text =上传状态:文件无法上传,出现以下错误:+ ex 。信息;
$ / code>
还有一个问题,我不认为它是我想要的插入,因为这将在我的数据库中创建一个重复条目,是否将它从INSERT INTO更改为更新?
在上传图片时是否有覆盖的方法? Atm它只是将图像保存到我已有的文件夹中?第一个图像或任何图像显然不会有相同的文件名,所以我怎么会用一个im上传覆盖文件夹中的任何图像?
编辑:
新错误(fileupload作为它存储在正确的区域,但传递fileupload到插入语句是abit wonky)
我得到错误
无法上传文件。出现以下错误:错误[42000] [MySQL] [ODBC 3.51驱动程序] [mysqld-5.5.9]您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以找到在C:\ Users \Garrith\Documents\Visual Studio 2010\WebSites\WebSite1\userdata\1\uplo附近使用的正确语法'在第1行
这有点奇怪吗?
所有我试图做的是保存文件路径+文件名在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 =上传状态:文件已上传!;
//某种功能可以将路径输入到我的插入语法中?
OdbcCommand cmd = new OdbcCommand(INSERT INTO Pictures(UserID,picturepath)VALUES(''+ + UserUd +'','+ fileuploadpath +'),cn);
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
StatusLabel.Text =上传状态:文件无法上传,出现以下错误:+ ex.Message;
code
你可以在这行看到:
VALUES(''+ theUserId +'','+ fileuploadpath +') ,cn);
我错过了文件名我试过了:
VALUES('+ theUserId +','+ fileuploadpath,filename +''),cn);
便宜的拍摄大声笑但值得一试我猜,它总是这样哭泣!
解决方案使用 CreateCommand 创建 OdbcCommand
code>在 OdbcConnection
上。发布的代码不会将 cmd
与 cn
绑定。此外,您应该使用CommandParameters而不是内联值(以防止SQL注入攻击)。
OdbcCommand cmd = cn.CreateCommand ();
cmd.CommandText =INSERT INTO图片(UserID,图片路径)VALUES(?,?);
cmd.Parameters.Add(新的OdbcParameter(@ UserID,OdbcType.Int,theUserID));
cmd.Parameters.Add(new OdbcParameter(@ picturepath,OdbcType.VarChar,fileuploadpath));
cmd.ExecuteNonQuery();
I get an error in my code:
ExecuteNonQuery: Connection property has not been initialized.
This could be due to the line in this code:
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");
Full code:
{
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;
}
}
}
}
There is another problem, I dont think its an insert I want as this is going to make a duplicate entry within my database, would it just be a case of changing it from INSERT INTO to UPDATE?
Also is there a way to overwrite upon uploading the image? Atm it just saving the image into the same folder as the one I already have? The first image or any image obviously isnt going to have the same file name so how would I go about overwriting any image in the folder with the one im uploading?
EDIT:
New error (fileupload works as its stored in the correct area but passing the fileupload to the insert statement is abit wonky)
I get the error
The file could not be uploaded. The following error occured: ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.5.9]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''C:\Users\Garrith\Documents\Visual Studio 2010\WebSites\WebSite1\userdata\1\uplo' at line 1
Which is kind of strange?
All im trying to do is save the filepath+filename in mydb my attempt at passing to the insert has obviously failed.
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;
}
}
}
}
As you can see on this line:
VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn);
im missing the "filename" i tryed this:
VALUES ('" + theUserId + "' , '" + fileuploadpath, filename + "')", cn);
Cheap shot lol but worth a go I guess and it cryed as it always does!
解决方案 You want to create the OdbcCommand
using CreateCommand
on OdbcConnection
. The code as posted does not tie cmd
to cn
. Also, you should use CommandParameters instead of inlining the values (to guard against SQL injection attacks).
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();
这篇关于ExecuteNonQuery:连接属性尚未初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文