我在这里使用 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/