ExecuteNonQuery:Connection属性尚未初始化?

时间:2011-03-25 02:33:25

标签: c# asp.net mysql sql html

我的代码出错:

ExecuteNonQuery:尚未初始化Connection属性。

这可能是由于此代码中的行:

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语句是abit wonky)

我收到错误

无法上传文件。发生以下错误:错误[42000] [MySQL] [ODBC 3.51驱动程序] [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);

便宜的拍摄哈哈,但值得一试,我猜它一如既往地哭了!

4 个答案:

答案 0 :(得分:8)

您需要将连接与cmd:

相关联
OdbcCommand cmd = 
  new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");       

cmd.Connection = cn;  // <--------

cmd.ExecuteNonQuery();

另外,请删除括号:

{         
    OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; 
    Server=localhost; Database=gymwebsite2; User=root; Password=commando;");          cn.Open(); 
} 

答案 1 :(得分:5)

您想在OdbcCommand上使用CreateCommand创建OdbcConnection。发布的代码不会将cmdcn联系起来。此外,您应该使用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();

答案 2 :(得分:2)

这是问题所在:

OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");  <--- there is no connection intialize here

将其更改为:

    //eg: odbconnection cn = new odbcconnection();

string fileup = fileupload + "," filename;


    OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileup.ToString() + "')",cn);  

此致

答案 3 :(得分:1)

以下是语法上的fubar:

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();
}