Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Importing data from excel sheet to sql data table!

$
0
0

Hi

I am trying to import data from excel sheet to a data table in sql express which is used to build a candle stick graph. Just need your opinion if this code fine or any modification is needed.

SqlDataReader dr;
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Database=D:\data.xlsx;Initial Catalog=ExcelDB;" + @"; Extended Properties =""Excel 8.0;HDR=Yes;IMEX=1;ImportMiexd Type=text;TypeGuessRows=0;Integrated Security=true""";
        SqlCommand cmd = new SqlCommand("Select StockName,Date,Open,High,Low,Close from [Sheet1$]",con);
        cmd.Connection = con;

try
        {
            con.Open();
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Response.Write("Able to read");
            }
            else
            {
                Response.Write("Unable to read");
            }
        }
        catch (Exception ex)
        {   

            BOOM BOOM EXCEPTION
        }
        finally
        {
            if (con.State == ConnectionState.Open)
                con.Close();
        }

Now i get an exception like this:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Guess there should be some problem with the connection string. Need your opinion if the connection string to import data from excel is in right format or need modification.

Thanks in advance.

Regards,

N1ZAM


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>