santhosha
santhosha

Reputation: 377

Import Excel data to sql database using asp.net

string filepath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filepath);
string ext = Path.GetExtension(filename);
String strConnection = "Data Source=192.168.51.02;Initial Catalog=****;User   ID=*****;Password=*****;Integrated Security=True";
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";

OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

OleDbCommand cmd = new OleDbCommand("Select [Emp ID],[Emp Name],[Log Date],[Logtime],[Type] from [One Month Report$]", excelConnection);
excelConnection.Open();

//cmd.ExecuteNonQuery();


//DataSet ds = new DataSet();
//SqlDataAdapter da = new SqlDataAdapter("Select [sno],[Name],[Designation] from [Sheet1$]", strConnection);

OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

sqlBulk.DestinationTableName = "K_Master_EmpAttendanceDet";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();

I want import data from excel to database using asp.net .I have tried like this but I am getting this error:

Login failed for user. The user is not associated with a trusted SQL Server connection.

Is it require any Dll file or any thing wrong in my code. Please Help me.

Upvotes: 2

Views: 189

Answers (1)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18883

Just Remove the "integrated security" part from your strConnection . Here your connection will use the windows authentication instead of the sql authentication. 

Upvotes: 2

Related Questions