当连接到数据库时,Visual basic throw“初始化格式”错误
问题描述:
我需要上传一个excel文件,读取它的内容并将其插入到我的数据库中。当连接到数据库时,Visual basic throw“初始化格式”错误
我总是得到这样的错误:
格式的初始化字符串的不符合规格 从索引186
我找到了一些解决方案(尽管大部分都使用C#
) ,我试图申请但没有成功:
connString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & ";Extended Properties=""Excel 12.0;HDR=NO;"")")
我也试过:
connString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & ";Extended Properties=Excel 12.0;)")
但他们都没有工作。这是路径:
"C:\Users\tama\Documents\Visual Studio 2008\Projects\uploader\uploader\File\Plan TEST.xlsx"
,我必须确保该path
是正确的,文件格式为xlsx
,所以我使用的版本12.0
。
答
var pathExtension = Path.GetExtension(fileName);
var connectionString = string.Empty;
if (pathExtension == ".xls")
{
connString = string.Format(@"PProvider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 8.0;HDR=YES\";", yourPath)
}
if (pathExtension == ".xlsx")
{
connString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";", yourPath)
}
非常感谢。当我回到代码时,我会尝试它。可能明天。 –