本帖最后由 nmcfab 于 2015-1-20 03:29 编辑
报错截图
新人寻求指点,非常感谢.
想要实现的功能,命令按钮打开指定的xls文件,显示前十条记录填充到dataGridView中,同时取得da.Tables[0]的表结构,如字段,类型等.
命令按钮中的代码如下:
[C#] 纯文本查看 复制代码 private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
this.txtFileName.Text = file.FileName.ToString();
string conString = file.FileName;
string strSource = @"Provider =Microsoft.ACE.OLEDB.12.0;" + "data source=" + conString + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strSource);
string sqlString = @"Select top 10 * from[Sheet1$]";
OleDbDataAdapter adapter = new OleDbDataAdapter(sqlString, conn);
DataSet da = new DataSet();
adapter.Fill(da);
string tabName = da.Tables[0].TableName;
DataTable schDa = conn.GetSchema(tabName);
this.dataGridView1.DataSource = da.Tables[0];
}
调试时程序报错
|