English 中文(简体)
SQL 例外未处理。 在 GripView 中查看 Excel 文件
原标题:SQLException was Unhandled. Viewing Excel File in GridView

我一直在做一个 C# 工程, 我浏览并浏览一个 Excel 文件到网格视图中。 但是, 有一个错误信息, 我无法理解 。 有人能帮我一下吗 。

这是我用的代码:

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)."
最佳回答

您正在试图通过 SqlConnect 连接到Excel。

要连接到Excel, 请使用 OleDBConnect :

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(); 
}
问题回答

您不能为 ODBC 或 JET 连接使用 SqlConnection SqlDataAdapter 。 请使用 OleDbConnection OleDbDataAdapter

还请确保及时处置连接和资源。在此建议使用 < code> 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); 
       }
   } 
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签