dot net help - Sajha Mobile
SAJHA MOBILE
dot net help
Posts 8 · Viewed 6159 · Go to Last Post
bobbhai
· Snapshot 0
Like · Likedby · 0
I have three column in excel. one third one i have some formula that will give the value. Now i have to write a dot net code that excel formula will save it in SQL. i need to save that formula in SQL and also if anyone change the formula in excel, i have to see that on SQL.
bobbhai
· Snapshot 5
Like · Liked by · 0
can anyone plz share the idea , how should i proceed,
Fuel Tank
· Snapshot 99
Like · Liked by · 0
Bro, explain it clearly (with example if applicable). I have some experience, I might be able to help if I understand the question .
bobbhai
· Snapshot 251
Like · Liked by · 0
I have three column in excel. No third one i have some formula that will give calculate the value. Lets say I use to formula to add first 2 column and get the result in third. Now i have to connect this excel sheet to sql server. i need to get the excel data to sql server..
Now i concern is, i need to record the change in excel to sql , i also need to record the change in formula , that user used to calculate the value.
I did connect the excel to sql , but with some reason its not working , also i couldn;t find the connection string of excel to sql. Here is my code so far i have done.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Drawing;


namespace ExcelUpload
{
public partial class Excel_Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnUpload_Click(object sender, EventArgs e)
{
if
(FileUpload1.HasFile)// this will check and ask to upload the file.HASFile is boolean value and only return true or false.
{
// to allow user to enter only excel file, use the extension method
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);// system.Io has extension. filename will gave the path for extension.
if
(fileExtension.ToLower() != ".xlsm" && fileExtension.ToLower() != ".xlsx")// convert the extesion to lower casee ..its to avoid the user mix behaviour fo using the extension {
{
lbUpload.Text = "Only .xlsm and xlsx file are Allowed.";
lbUpload.ForeColor = System.Drawing.Color.Green;
}

else
{
//if we want to restrict the file size ..contacnt length is in byte. if we want to 1024*1024 bytes = 1048576 bytes=1MB
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize > 5242880) // 5MB max
{
lbUpload.Text = " Maximun files size . %MB exceeded";
lbUpload.ForeColor = System.Drawing.Color.Red;
}
else
{
FileUpload1.SaveAs(Server.MapPath("~/Upload/" + FileUpload1.FileName));
// connection to sql
SqlConnection con = new SqlConnection("provideer=Microsoft.Ace.SqlDB.12.0; Data Source=‪"+C:\Users\baburam\Desktop\GPS Excel.xlsx+";Extended propertities=Excel_Gridview 12.0; HDR=Yes");
SqlCommand cmd = new SqlCommand("select * from[Sheet1$]",con);
SqlDataAdapter objAdapter = new SqlDataAdapter(cmd);

con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
string con_str = @"datasource=.;database=GPS_Demo;integrated security =True";

SqlBulkCopy bulkinsert = new SqlBulkCopy(con_str);
bulkinsert.DestinationTableName = "Customer";
bulkinsert.WriteToServer(rdr);
con.Close()
;
Array.ForEach(Directory.GetFiles((Server.MapPath("~/Upload/"))), File.Delete);

lbUpload.Text = "File Uploaded";
lbUpload.ForeColor = System.Drawing.Color.Green;
}
}
}
else
{
lbUpload.Text = "Please Select the File";
lbUpload.ForeColor = System.Drawing.Color.Red;
}
}
}
}

bobbhai
· Snapshot 256
Like · Liked by · 0
how do i use connection string to excel to sql ..


// connection to sql
SqlConnection con = new SqlConnection("provideer=Microsoft.Ace.SqlDB.12.0; Data Source=‪"+C:\Users\baburam\Desktop\GPS Excel.xlsx+";Extended propertities=Excel_Gridview 12.0; HDR=Yes");
SqlCommand cmd = new SqlCommand("select * from[Sheet1$]",con);
alece
· Snapshot 325
Like · Liked by · 0
Check provider string... Why is it provideer? And why do u have a +sign in your source ..? Use @.. Also is your first row column names since u have HDR=yes..?.
bobbhai
· Snapshot 380
Like · Liked by · 0
i did different approach. with this i got an error "Could not find installable ISAM"..


string path = string.Concat((Server.MapPath("~/Upload/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=‪" + path + ";Extended Propertities=Excel 12.0");
OleDbCommand cmd = new OleDbCommand("select * from[Sheet1$]", con);
OleDbDataAdapter objAdapter = new OleDbDataAdapter(cmd);
alece
· Snapshot 480
Like · Liked by · 0
why don't u try something like this?

string path = @"C:\Desktop\test.xlsx";
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
SqlCommand cmd = new SqlCommand("select * from[Sheet1$]",conStr);



Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 1 · Viewed 1
· Posts 5 · Viewed 2383
· Posts 72 · Viewed 16007 · Likes 15
· Posts 1 · Viewed 137
· Posts 3 · Viewed 452
· Posts 1 · Viewed 124
· Posts 16 · Viewed 2476 · Likes 3
· Posts 1 · Viewed 156
· Posts 1 · Viewed 137
· Posts 4 · Viewed 604



Your Banner Here
Travel Partners
Travel House Nepal