c# - 使用 Oledbconnection 时出现编译错误

标签 c# asp.net

我在这里使用 OleDbConnection 作为连接字符串,但我在行中收到错误

if (conn.State == ConnectionState.Closed)

Error as CS0019: Operator '==' cannot be applied to operands of type 'System.Data.ConnectionState' and 'ConnectionState'

这是我的代码

protected void btnSave_Click(object sender, EventArgs e)
{
    DataTable dtExcel = new DataTable();
    dtExcel.Clear();
    string StrCount = String.Empty;
    string connString = "";
    HttpPostedFile File = FileUpload1.PostedFile;
    string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
    string path = FileUpload1.PostedFile.FileName;
    string Filename = path.Substring(path.LastIndexOf("\\") + 1, path.Length - path.LastIndexOf("\\") - 1);
    path = Server.MapPath(@"~/Excels/" + "/" + Filename.ToString());

    File.SaveAs(path);
    if (strFileType.Trim() == ".xls")
    {
        connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
    }
    else if (strFileType.Trim() == ".xlsx")
    {
        connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
    }
    string query = "SELECT * FROM [Sheet 1$]";
    OleDbConnection conn = new OleDbConnection(connString);
    conn.Close();
    if (conn.State == ConnectionState.Closed)
        conn.Open();
    OleDbCommand cmd = new OleDbCommand(query, conn);
    OleDbDataAdapter daExcel = new OleDbDataAdapter(cmd);

    daExcel.Fill(dtExcel);
    conn.Close();}

不知道为什么?

我尝试了其他链接的解决方案,但没有帮助

最佳答案

在我看来,您的类名或属性名不明确。 ConnectionState 似乎有两个含义。

尝试在 ConnectionState 前加上其完整的命名空间:

if (conn.State == System.Data.ConnectionState.Closed)

关于c# - 使用 Oledbconnection 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510926/

相关文章:

c# - 字节变量如何存储在内存中?

c# - 如何在运行时 C# 中搜索嵌入式文件

c# - 从列表中获取优先项目并将它们插入到给定位置的同一列表中 c#

asp.net - 实时服务器上的 PageRequestManagerParserErrorException - 帮助!

javascript - 过滤谷歌地图范围内的标记

c# - ASP.NET 中有这样的控件可以在运行时呈现 HTML 吗?

c# - C# 程序集中是否定义了 WM_KEYDOWN 常量?

c# - 在 WPF 之外使用 Freezable 类的任何 "Gotchas"?

c# - 尽管没有发生请求,但 TempData[] 被删除

c# - 如何记录系统用户所做的更改?