【问题标题】:ExecuteNonQuery: Connection property has not been initialized?ExecuteNonQuery:连接属性尚未初始化?
【发布时间】: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:如果你指的是我,那么我已经做到了...... =]

标签: c# asp.net mysql sql html


【解决方案1】:

以下在语法上是fubar'd:

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

【讨论】:

    【解决方案2】:

    你需要把连接和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(); 
    } 
    

    【讨论】:

      【解决方案3】:

      您想在OdbcConnection 上使用CreateCommand 创建OdbcCommand。发布的代码不会将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();
      

      【讨论】:

        【解决方案4】:

        这就是问题所在:

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

        问候

        【讨论】:

        • 是的,我发现当我在 try 语句中更改我的连接字符串和会话字符串时,谢谢(我把它拿出来是因为我把连接字符串放在了 try 之外)
        • 检查我的编辑...将断点添加到文件up..并检查您的文件路径+文件名是否传递到fileup。所以插入不会有问题。
        猜你喜欢
        • 2012-05-03
        • 2012-08-20
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        相关资源
        最近更新 更多