c# - 执行非查询 : Connection property has not been initialized?

标签 c# asp.net mysql sql html

我的代码出现错误:

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?

还有上传图片时可以覆盖的方法吗? Atm 它只是将图像保存到与我已有的文件夹相同的文件夹中吗?第一张图片或任何图片显然不会具有相同的文件名,那么我将如何用我上传的图片覆盖文件夹中的任何图片?

编辑:

新错误(fileupload 工作因为它存储在正确的区域但是将 fileupload 传递给插入语句有点不稳定)

我得到了错误

无法上传文件。发生以下错误:错误 [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);

便宜的镜头哈哈,但我想值得一试,它像往常一样哭了!

最佳答案

您需要将连接与 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(); 
} 

关于c# - 执行非查询 : Connection property has not been initialized?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5427945/

相关文章:

c# - 更新面板内的异步文件上传和触发回发的下拉列表不起作用

c# - 根据调度程序将 async-await C# 代码转换为 F#

asp.net - 是否可以在 Visual Studio 2010 的 ASP.NET MVC 项目中使用 HTML5?

asp.net - 授予 Asp.Net 用户帐户修改网站根目录的权限是否安全?

MySQL,自增,导出和导入

MySQL - 用第二行的第二列减去第一行的第一列

c# - 删除 UWP 中的导航缓存

c# - 如何使用objectGUID得到一个DirectoryEntry?

asp.net - 如何在没有 VS2010 的情况下运行 ASP.NET Web 应用程序?

mysql - 使用 NodeJs 和 Handlebars 对表格的 <th> 标签进行水平排序