这很奇怪,我正在尝试使用数据读取器访问excel表并收到我无法搞清楚的错误。想知道是否有人遇到过同样的问题。
Microsoft Office Access数据库引擎找不到对象“SHEETNAME”。确保对象存在,并且您正确拼写其名称和路径名称
var str = new StringBuilder();
using (var myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=foreignkeys.xlsx;Extended Properties='Excel 8.0;HDR=Yes'"))
{
using (var myCommand = new OleDbCommand())
{
myConnection.Open();
myCommand.Connection = myConnection;
myCommand.CommandText = "select TableName, ColumnName from [SHEETNAME]";
using (var dr = myCommand.ExecuteReader())
{
while (dr.Read())
{
var tableName = dr["TableName"].ToString();
var columnName = dr["ColumnName"].ToString();
var tmp = columnName;
tmp = tmp.Replace("ID", "").Replace("Id", "");
str.AppendLine(string.Format("IF NOT EXISTS ( SELECT 1 FROM sys.foreign_keys AS FK WHERE name = 'FK_{0}_{1}' AND parent_object_id = OBJECT_ID('{2}'))", tableName, tmp, tableName));
str.AppendLine("BEGIN");
str.AppendLine(string.Format("ALTER TABLE dbo.{0} WITH CHECK", tableName));
str.AppendLine(string.Format("ADD CONSTRAINT FK_{0}_{1} FOREIGN KEY ({2}) REFERENCES dbo.{3}({4})", tableName, tmp, columnName, tmp, columnName));
str.AppendLine("END");
str.AppendLine("");
str.AppendLine("");
}
}
}
}
return str.ToString();
答案 0 :(得分:1)
假设您的Excel文件包含名为SHEETNAME
的工作表,那么您需要编写
myCommand.CommandText = "select TableName, ColumnName from [SHEETNAME$]";
^