Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQLException未处理。在GridView中查看Excel文件_C#_.net_Sql Server_Gridview_Datagridview - Fatal编程技术网

C# SQLException未处理。在GridView中查看Excel文件

C# SQLException未处理。在GridView中查看Excel文件,c#,.net,sql-server,gridview,datagridview,C#,.net,Sql Server,Gridview,Datagridview,我一直在做一个C#项目,在这个项目中,我浏览并查看一个Excel文件到gridview中。然而,有一个错误消息,我不明白。有人能帮我一下吗 这是我使用的代码: private void buttonUpload_Click(object sender, EventArgs e) { string connectionString = String.Format(@"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1

我一直在做一个C#项目,在这个项目中,我浏览并查看一个Excel文件到gridview中。然而,有一个错误消息,我不明白。有人能帮我一下吗

这是我使用的代码:

private void buttonUpload_Click(object sender, EventArgs e)
{
    string connectionString = String.Format(@"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text);

    string query = String.Format("select * from [{0}$]", "Sheet1");

    SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connectionString);

    DataSet dataSet = new DataSet();

    dataAdapter.Fill(dataSet);

    dataGridView1.DataSource = dataSet.Tables[0];
}
这是错误消息:

"A network-related or instance-specific error occurred while establishing 
a connection to SQL Server. The server was not found or was not accessible. 
Verify that the instance name is correct and that SQL Server is configured 
to allow remote connections. 
(provider: SQL Network Interfaces, 
error: 26 - Error Locating Server/Instance Specified)."

您正试图通过
SqlConnection
连接到Excel

要连接到Excel,请使用OLEDB连接:

private void buttonUpload_Click(object sender, EventArgs e)
{
   string connectionString = String.Format(@"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text)
   var objConn = new OleDbConnection(connectionString);

   objConn.Open(); 

   OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

   OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); 

   objAdapter1.SelectCommand = objCmdSelect; 

   DataSet objDataset1 = new DataSet(); 

   objAdapter1.Fill(objDataset1); 

   objConn.Close(); 
}

不能将
SqlConnection
SqlDataAdapter
用于ODBC或JET连接。使用
OleDbConnection
OleDbDataAdapter

还请确保及时处理连接和资源。此处建议使用
Using
语句

private void buttonUpload_Click(object sender, EventArgs e)
{
   string connectionString = String.Format(@"Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", textBoxFileName.Text);
   DataSet objDataset1 = new DataSet();

   using (OleDbConnection objConn = new OleDbConnection(connectionString))
   {
       objConn.Open(); 

       using (OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn))
       using (OleDbDataAdapter objAdapter1 = new OleDbDataAdapter())
       {
           objAdapter1.SelectCommand = objCmdSelect;    
           objAdapter1.Fill(objDataset1); 
       }
   } 
}

通常是这样,但在他的情况下,他希望使用JET连接到Excel文件。这在使用
SqlConnection
SqlDataAdapter
时不起作用。太好了。既然你已经完全改变了答案,我的评论似乎有点不对劲@ThorstenDittmar-:-P我根据错误信息发布了我的原始答案,然后当我重新阅读问题时,我注意到他正在使用Excel并进行了相应的修改。@DarrenDavies:我按照你的建议做了(使用OleDBConnection连接到Excel),上一个错误消失了。但是,有一条新的错误消息不断显示。错误消息为“外部表不是预期格式。”。问题的原因可能是什么?请允许我问一下。谢谢