Need to read excel data from the server folder path and update it in the SQL server .once it is updated need to move to another folder using the C# console application
code:
string value = "read";
string ConStr;
string HDR;
HDR = "YES";
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection conn = new OleDbConnection(ConStr);
conn.Open();
OleDbCommand newvalues = new OleDbCommand("select * from [" + value + "$]", conn);
OleDbDataAdapter newvalue = new OleDbDataAdapter(newvalues);
DataTable dt = new DataTable();
dt.Fill(dt1);
conn.Close();
DataTable dt = dt1;
SqlCommand cmd = new SqlCommand("spupdate", connect);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter valparam = new SqlParameter("@update", SqlDbType.Structured);
valparam.TypeName = "tablevalue";
valparam.Value = values;
cmd.Parameters.Add(valparam);
cmd.ExecuteNonQuery();
connect.Close();
Naimish MakwanaPosted Mar 22, 2023, 12:08 PM
Use below code:
Jignesh KumarPosted Mar 22, 2023, 12:11 PM
Please try this one,
Meghana MPosted Mar 22, 2023, 11:47 AM
string datetime = DateTime.Now.ToString("MM-dd-yyyy");
string fileName = Path.GetFileName(sourceFolderpath);
string destination = Path.Combine(destinationFolderPath, fileName);
File.Move(sourceFolderpath, destination);
-DestinationFolder file Output Should be in the below format once it is moved from source to destination
File1_12/2/2022.xlsx
Jignesh KumarPosted Mar 22, 2023, 8:11 AM
Get filename and append datetime string to filename.
Naimish MakwanaPosted Mar 22, 2023, 7:55 AM
Add time stemp in filename. try below
Meghana MPosted Mar 22, 2023, 7:47 AM
how to add a timestamp for the filename once it is moved to the destination folder
destination folder should be in this format
Eg:filename_datetime.xlsx
Jignesh KumarPosted Mar 22, 2023, 5:05 AM
Hello Meghana,
Its permission issue, You can try to put in another drive of your machine.
Naimish MakwanaPosted Mar 22, 2023, 4:13 AM
try the following solutions:
Check if the file is open by another user: If the file is being used by another user, you may need to wait for them to close it or ask them to close the file. Alternatively, you can try accessing the file from another computer or workstation.
Check file permissions: Ensure that you have the necessary permissions to access the file. You can do this by right-clicking on the file, selecting Properties, and then clicking on the Security tab. If you do not have the necessary permissions, you will need to contact the file owner or administrator to grant you access.
Close other applications: Close any other applications that may be using the file, as they may be preventing Microsoft Access from accessing the file.
Restart the computer: If none of the above solutions work, try restarting your computer to release any locks on the file.
Thanks
Naimish
Meghana MPosted Mar 22, 2023, 4:11 AM
I'm getting the below error, I have given the source path, and destination path as the local machine path
The Microsoft Access database engine cannot open or write to the file 'C:\servername\filename'. It is already opened exclusively by another user, or you need permission to view and write its data.'
Naimish MakwanaPosted Mar 22, 2023, 4:04 AM
Hello Meghna,
Try below code:
Thanks
Naimish Makwana
Jignesh KumarPosted Mar 22, 2023, 3:58 AM
Helli,
Please refer this one for insert excel data to SQL,
https://www.c-sharpcorner.com/blogs/import-excel-spreadsheet-data-into-sql-server-table-using-c-sharp
Move file once insert operation done,
Tuhin PaulPosted Mar 22, 2023, 1:31 AM
you need to replace "your connection string", "your source folder path", and "your destination folder path" with your actual values. Remember that the File.Move method will throw an exception if the destination file already exists, so you may want to add some error handling code to handle this case.
Tuhin PaulPosted Mar 22, 2023, 1:30 AM
To read data from an Excel file you are using OleDb. To move the file to another folder after the update, you can use the File.Move method in C#.