Sorry, I am a beginner and it may be very basic question.
I have tested the below query and it does create a table and add all the contents from an excel file
SELECT *
INTO TEST_TABLE
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=D:\Vpath.xlsx; HDR=YES; IMEX=1',
'SELECT * FROM [Sheet1$]')
GO
I tried to do the same in visual studio with the below
string str = @"D:\path.xlsx";
string Str1 = "SELECT * INTO TEST_TABLE FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0; Database=" + str + "; HDR = YES; IMEX = 1',' SELECT * FROM [Sheet1$]'";
SqlConnection con = new SqlConnection("Connection...details");
con.Open();
SqlCommand cmd = new SqlCommand(Str1, con);
SqlDataReader reader = cmd.ExecuteReader();
cmd.ExecuteNonQuery();
reader.Close();
con.Close();
I get the below error
System.Data.SqlClient.SqlException: 'Incorrect syntax near ' SELECT * FROM [Sheet1$]'.'