简体   繁体   English

使用Oledbconnection时出现编译错误

[英]compliation error while using Oledbconnection

i am using OleDbConnection for connection string here but I am getting error at line 我在这里使用OleDbConnection作为连接字符串,但在行上却出现错误

if (conn.State == ConnectionState.Closed)

Error as CS0019: Operator '==' cannot be applied to operands of type 'System.Data.ConnectionState' and 'ConnectionState' 错误为CS0019:运算符'=='无法应用于类型'System.Data.ConnectionState'和'ConnectionState'的操作数

here is my code 这是我的代码

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

I dont know why? 我不知道为什么?

I tried the solutions from other link but it didn't helped 我尝试了其他链接的解决方案,但没有帮助

It seems to me you have an ambiguous class or property name. 在我看来,您有一个模糊的类或属性名称。 ConnectionState appears to have two meanings. ConnectionState似乎有两个含义。

Try to prefix ConnectionState as follows with its full namespace: 尝试按以下方式给ConnectionState加上完整的名称空间:

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

After reading your comments on Patrick's answer, I see else is missing here 阅读您对Patrick的回答的评论后,我发现这里还缺少其他内容

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\"";
    }

May be connString is left blank as strFileType is not .xls.or .xlsx. 可能是connString留为空白,因为strFileType不是.xls。或.xlsx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM