I am using VS2005 C# and SQL Server 2005. I am currently doing an import for a .CSV excel file data into my SQL Server database.
I am having some error, which I assume is related to my sql statement. Below is my code:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Get the name of the Excel spreadsheet to upload.
string strFileName = Server.HtmlEncode(FileUpload1.FileName);
// Get the extension of the Excel spreadsheet.
string strExtension = Path.GetExtension(strFileName);
// Validate the file extension.
if (strExtension != ".xls" && strExtension != ".xlsx" && strExtension != ".csv" && strExtension != ".csv")
{
Response.Write("<script>alert('Failed to import DEM Conflicting Role Datasheet. Cause: Invalid Excel file.');</script>");
return;
}
// Generate the file name to save.
string dir = #"C:\Documents and Settings\rhlim\My Documents\Visual Studio 2005\WebSites\SoD\UploadFiles\";
string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
// Save the Excel spreadsheet on server.
FileUpload1.SaveAs(dir+mycsv);
// Create Connection to Excel Workbook
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";
using (OleDbConnection ExcelConnection = new OleDbConnection(connStr))
{
OleDbCommand ExcelCommand = new OleDbCommand("SELECT [TABLES] FROM" + mycsv, ExcelConnection);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
ExcelConnection.Open();
using (DbDataReader dr = ExcelCommand.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=<IP>;Initial Catalog=<DB>;User ID=<UID>;Password=<PW>";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "DEMUserRoles";
bulkCopy.WriteToServer(dr);
Response.Write("<script>alert('DEM User Data imported');</script>");
}
}
}
}
else Response.Write("<script>alert('Failed to import DEM User Roles Data. Cause: No file found.');</script>");
}
I am getting the error
"Syntax error (missing operator) in query expression '[Description] FROM20111109164041.csv'."
while executing using (DbDataReader dr = ExcelCommand.ExecuteReader()). Description is the last column in my database.
Anyone know what is wrong with my code? Thank You
You need a space between FROM and the csv file! :)
OleDbCommand ExcelCommand = new OleDbCommand("SELECT [TABLES] FROM " + mycsv, ExcelConnection);
That's why I always use the string.Format method, you see much better how the final string will look:
OleDbCommand ExcelCommand = new OleDbCommand(string.Format("SELECT [TABLES] FROM {0}",mycsv), ExcelConnection);
Seems like you need to place a blank between FROM and your CSV-File:
'[Description] FROM 20111109164041.csv'
If you are adding a string you should place it between single quotes
OleDbDataAdapter da = new OleDbDataAdapter("select * , '" + tempUid + "' as [UID] from [" + sheet1 + "]", conn);
Related
I am working on a c# project where I need the user to open an Excel file and insert the data in an SQL server database. The problem is that using this connection string
("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;")
during the opening of the connection I get
the provider 'microsoft.ace.oledb.12.0' is not registered
exception, which is odd because I worked with an Access Database and used the same connection string. The Access Database Engine is installed on my PC and I even compiled at x86, am I missing something?
The reasons are as follows:
When accessing the .xlsx file with SQL SERVER, you must use
provider 'Microsoft.ACE.OLEDB.12.0' to implement.
First install AccessDatabaseEngine.exe.
Download path: http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en
This provider can be seen in the database on 32-bit systems.
It cannot be seen in a 64-bit system, so you need to call
C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\DTExec.exe
to execute the package.
solution:
Open IIS Manager.
Right-click the connection pool where the application is located.
Modify "Enable 32 as application" to true.
The code shown below may help you.
Sqlconn sqlcon = new Sqlconn();
public Form1()
{
InitializeComponent();
}
string str_Excel_Path;
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Only one file can be opened------------------------------------------
openFileDialog1.Filter = "Excel file|*.xlsx";//Set the open file filter
openFileDialog1.Title = "Open Excel file";//Set the title of the open file
openFileDialog1.Multiselect = true;//Allow multiple files to be selected
if (openFileDialog1.ShowDialog() == DialogResult.OK)//Determine whether a file is selected
{
str_Excel_Path = openFileDialog1.FileName.ToString();//Get the selected file address
textBox1.Text = str_Excel_Path;//Display the selected file address in textBox1
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
string[] P_str_Names = textBox1.Text.Split(',');//Store all selected Excel file names
string P_str_Name = "";//Store the traversed Excel file name
List<string> P_list_SheetNames = new List<string>();//Create a generic collection object to store sheet names
for (int i = 0; i < P_str_Names.Length; i++)//Iterate over all selected Excel file names
{
P_str_Name = P_str_Names[i];//The Excel file name traversed by the record
P_list_SheetNames = GetSheetName(P_str_Name);//Get all sheet names in the Excel file
for (int j = 0; j < P_list_SheetNames.Count; j++)//traverse all worksheets
{
/* if (ckbox_Windows.Checked)//Log in to SQL Server with Windows authentication
//Export worksheet contents to SQL Server
{
ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data Source='" + txt_Server.Text + "';Initial Catalog='" + cbox_Server.Text + "';Integrated Security=True;");
}
else if (ckbox_SQL.Checked)//Log in to SQL Server with SQL Server authentication
{
ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data Source='" + txt_Server.Text + "'Database='" + cbox_Server.Text + "';Uid='" + txt_Name.Text + "';Pwd='" + txt_Pwd.Text + "';");
}*/
ImportDataToSql(P_str_Name, P_list_SheetNames[j], "Data source=localhost;Initial Catalog=student;User ID=sa;Password=123456");
}
}
MessageBox.Show("All selected Excel sheets have been imported into SQL Server database!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Please select the file to be imported into the database!");
}
}
//Get all worksheet names in the Excel file
private List<string> GetSheetName(string P_str_Name)
{
List<string> P_list_SheetName = new List<string>();//Create a generic collection object
//Connect to the Excel database
//OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + P_str_Name + ";Extended Properties=Excel 8.0;");
OleDbConnection olecon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + P_str_Name + ";Extended Properties=\"Excel 12.0;HDR=yes;IMEX=1;\"");
olecon.Open();//Open database connection
System.Data.DataTable DTable = olecon.GetSchema("Tables");//Create a table object
DataTableReader DTReader = new DataTableReader(DTable);//Create table read object
while (DTReader.Read())
{
string p_str_sName = DTReader["Table_Name"].ToString().Replace('$', ' ').Trim();//Record the worksheet name
if (!P_list_SheetName.Contains(p_str_sName))//Determine whether the sheet name already exists in the generic collection
P_list_SheetName.Add(p_str_sName);//Add the worksheet to the pan-collection
}
DTable = null;//Clear the table object
DTReader = null;//Clear the table read object
olecon.Close();//Close the database connection
return P_list_Sheet
}
/* Import the content of the specified worksheet in Excel into the SQL Server database */
public void ImportDataToSql(string p_str_Excel, string p_str_SheetName, string p_str_SqlCon)
{
DataSet myds = new DataSet();//Create a dataset object
try
{
// get all data
//string P_str_OledbCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + p_str_Excel + ";Extended Properties=Excel 8.0;";
string P_str_OledbCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + p_str_Excel + ";Extended Properties=\"Excel 12.0;HDR=yes;IMEX=1;\"";
OleDbConnection oledbcon = new OleDbConnection(P_str_OledbCon);//Create an Oledb database connection object
string p_str_ExcelSql = "";//Record the Excel query statement to be executed
OleDbDataAdapter oledbda = null;//Create an Oledb data bridge object
p_str_ExcelSql = string.Format("select * from [{0}$]", p_str_SheetName);//Record the Excel query statement to be executed
oledbda = new OleDbDataAdapter(p_str_ExcelSql, P_str_OledbCon);//Execute Excel query using data bridge
oledbda.Fill(myds, p_str_SheetName);//Fill data
//Define variables to record the SQL statement that creates the table
string P_str_CreateSql = string.Format("create table {0}(", p_str_SheetName);
foreach (DataColumn c in myds.Tables[0].Columns)//traverse all rows in the dataset
{
P_str_CreateSql += string.Format("[{0}]text,", c.ColumnName);//Create a field in the table
}
P_str_CreateSql = P_str_CreateSql + ")";//Improve the SQL statement for creating a table
//Create SQL database connection object
using (SqlConnection sqlcon = new SqlConnection(p_str_SqlCon))
{
sqlcon.Open();//Open database connection
SqlCommand sqlcmd = sqlcon.CreateCommand();//Create an execution command object
sqlcmd.CommandText = P_str_CreateSql;//Specify the SQL data to be executed
sqlcmd.ExecuteNonQuery();//Execute operation
sqlcon.Close();//Close the database connection
}
using (SqlBulkCopy bcp = new SqlBulkCopy(p_str_SqlCon))//Import data with bcp
{
bcp.BatchSize = 100;//Number of rows per transfer
bcp.DestinationTableName = p_str_SheetName;//Define the destination table
bcp.WriteToServer(myds.Tables[0]);//Write data to SQL server data table
}
}
catch
{
MessageBox.Show("SQL Server database already exists" + p_str_SheetName + "Table!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Sqlconn code:
// private static string constr = "server=(local);Initial Catalog=D_total;Integrated Security=True";
private static string constr = "Data source=localhost;Initial Catalog=student;User ID=sa;Password=123456";
// private static string constr = "Data Source =192.168.1.201;Initial Catalog=D_total23 ;User Id=sa;Password=123";
public DataTable f1()
{
string A = "select name from master..sysdatabases";//Query this database information
return Only_Table1(A);
}
public DataTable Only_Table1(string exec)
{
System.Data.DataTable dt_jdl = new DataTable();
try
{
using (SqlConnection con = new SqlConnection(constr))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (con.State == ConnectionState.Open || con.State == ConnectionState.Connecting)
{
SqlDataAdapter sda2 = new SqlDataAdapter(exec, con);//All by writing stored procedures
DataSet ds2 = new DataSet();
sda2.Fill(ds2, "cxq");
dt_jdl = ds2.Tables["cxq"];
sda2.Dispose();
ds2.Dispose();
}
con.Close();
}
return dt_jdl;
}
catch (Exception EX)
{
return null;
}
}
}
Run the project:
Click the OK button to select the xml file from the folder. Click the import button to import the file into the database.
I'm building a program that needs the following features:
Import an Excel file into database -- Check
Avoid duplicates --- working on it
Ignore some rows that are in the header of the Excel file and the bottom --- that's what I want to ask you guys
Here's my code
protected void Upload_Click(object sender, EventArgs e)
{
string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
string filename = Path.GetFileName(filepath);
FileUpload1.SaveAs(excelPath);
string strConnection = #"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HDR=NO,IMEX=1;\"";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable schema = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = schema.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand cmd = new OleDbCommand("Select * from [" + sheetName + "]", excelConnection);
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
{
sqlBulk.ColumnMappings.Add(0,0);
sqlBulk.ColumnMappings.Add(1,1);
sqlBulk.ColumnMappings.Add(2,2);
sqlBulk.ColumnMappings.Add(3,3);
sqlBulk.DestinationTableName = "Dados";
sqlBulk.WriteToServer(dReader);
}
excelConnection.Close();
}
And what I'm struggling is that I need to my code to find the columns in the excel and ignore the rows that I don't need...
I thought that this lines were enough for the job :
sqlBulk.ColumnMappings.Add(0,0);
sqlBulk.ColumnMappings.Add(1,1);
sqlBulk.ColumnMappings.Add(2,2);
sqlBulk.ColumnMappings.Add(3,3);
sqlBulk.DestinationTableName = "Dados";
Here's the table that I want to import :
Dealing with fluctuating source files is tricky. Maybe try loading the file as a whole into a raw table and then after loading the table call a proc to only move the columns that match complete row to your prod or final table. For example, only select from raw where Data Mov, Data Valor, Descricao do Movimento, and Valor em EUR are not null. If needed you could add more validation if needed, like checking the first two columns for date format and the last column for a numeric value. I just think it might be easier to do it in SQL than in .NET code.
I am reading data from an Excel file. when I read the normal Excel file,It works fine but when I read an excel file which has columns like shown below it does not find the work sheet and gives an exception-
The Microsoft Jet database engine could not find the object 'Sheet1$_'. Make sure the object exists and that you spell its name and the path name correctly.
My Code to read the excel is-
private static DataTable getExcelData(string ExcelPath)
{
OleDbConnection con;
string connectionString;
string[] pathArray = ExcelPath.Split('.');
var Extention = pathArray[pathArray.Length - 1];
if (Extention == "xlsx")
//read a 2007 file
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
ExcelPath + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
else
//read a 97-2003 file
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
ExcelPath + ";Extended Properties=Excel 8.0;";
con = new OleDbConnection(connectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataTable dbSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);
var firstSheetName = dbSchema.Rows[0]["TABLE_NAME"];
OleDbDataAdapter cmd = new OleDbDataAdapter("select * from [" + firstSheetName + "] Where NOT [Event Code]=''", con);
DataSet ds = new DataSet();
cmd.Fill(ds);
con.Close();
return ds.Tables[0];
}
}
I have to get all the columns inside Mon,Tues etc.
GetOleDbSchemaTable also returns hidden tables in your Excel file: usually a name like Sheet1$_ indicates an hidden table created when you apply a filter on Sheet1$.
You need to change your code: search for table that ends with $ to set firstSheetName.
Please note that OLEDB does not preserve the sheet order as they were in Excel.
Also note that you need to do this to read an excel file with multirow titles:
set HDR=No in EXTENDED PROPERTIES of your connection string
specify column name and select range in your OleDbCommand in order to skip the first two rows
For example:
SELECT [F1] AS Location,
[F2] AS EmpId,
[F3] AS EmpName,
[F4] AS MondayShift,
[F5] AS Monday15Min,
[F6] AS Monday30Min,
[F7] AS Monday15Min2
FROM [Sheet1$A3:G]
I am uploading an excel file by using c# and i am selecting a column named LOT from that excel file.In column LOT one row is having number starting with asterisk symbol(*). I need a query to select all the rows. please Help me out in this.
I tried the below sample code but it is not working fine. Its returning the lots which doesn't have asterisk symbol(*).
protected void btnUpload_Click(object sender, EventArgs e)
{
//Get path from web.config file to upload
string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
string filename = string.Empty;
//To check whether file is selected or not to uplaod
if (BrowseFile.HasFile)
{
try
{
string[] allowdFile = { ".xls", ".xlsx" };
//Here we are allowing only excel file so verifying selected file pdf or not
string FileExt = System.IO.Path.GetExtension(BrowseFile.PostedFile.FileName);
//Check whether selected file is valid extension or not
bool isValidFile = allowdFile.Contains(FileExt);
if (!isValidFile)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = "Please upload only Excel";
}
else
{
// Get size of uploaded file, here restricting size of file
int FileSize = BrowseFile.PostedFile.ContentLength;
if (FileSize <= 1048576)//1048576 byte = 1MB
{
//Get file name of selected file
filename = Path.GetFileName(Server.MapPath(BrowseFile.FileName));
//Save selected file into server location
BrowseFile.SaveAs(Server.MapPath(FilePath) + filename);
//Get file path
string filePath = Server.MapPath(FilePath) + filename;
//Open the connection with excel file based on excel version
OleDbConnection con = null;
if (FileExt == ".xls")
{
con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;");
}
else if (FileExt == ".xlsx")
{
con = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;");
}
con.Open();
//Get the list of sheet available in excel sheet
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//Get first sheet name
string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();
//Select rows from first sheet in excel sheet and fill into dataset
//OleDbCommand ExcelCommand = new OleDbCommand(#"SELECT LOT FROM [" + getExcelSheetName + #"] WHERE LOT IS NOT NULL", con);
OleDbCommand ExcelCommand = new OleDbCommand(#"SELECT LOT FROM [" + getExcelSheetName + #"] WHERE LOT LIKE '**'", con);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataTable ExcelDataSet = new DataTable();
ExcelAdapter.Fill(ExcelDataSet);
//Holding the data into the list
List<DataRow> strLot = ExcelDataSet.AsEnumerable().ToList();
con.Close();
//string lots = "";
//seperating the each lot with a comma separator
for (int i = 0; i < strLot.Count; i++)
{
totLots += ExcelDataSet.Rows[i].ItemArray.GetValue(0).ToString() + ",";
}
//Removing the last comma separator
totLots = totLots.Remove(totLots.Length - 1);
You can use the LIKE operator
WHERE <Column> LIKE '\**'
Also, depending on your data source, the wildcard '*' could be replaced with '%' and 'LIKE' keyword could be replaced with 'ALIKE'
with '%' the search text will become '*%' instead of '\**'
Edit: Code below checked with .xls and .xlsx files
OleDbCommand ExcelCommand = new OleDbCommand(#"SELECT LOT FROM [" + getExcelSheetName + #"] WHERE LOT LIKE '*%'", con);
Change your command text to the following...
#"SELECT [LOT] FROM [" + getExcelSheetName + #"] WHERE [LOT] LIKE '\*%')"
This should preform a simple pattern match like a SQL script and look for any member of the column starting with an asterisk and containing anything at all after that.
I am new to ASP.NET and C# and I am using VS2005 C# and SQL Server 2005.
I have a web application which contain a function which uploads data from a spreadsheet imported into the database.
However, my current function copies the spreadsheet uploaded into a directory, and used the uploaded file in the directory for reading of contents instead.
I would like to change it such that it will not create a backup copy of the uploaded excel file, and read the file contents directly from the uploaded file instead of the backup created.
Below is my code snippet for the import function for .xls and .xlsx spreadsheet:
if (FileImport.HasFile)
{
// Get the name of the Excel spreadsheet to upload.
string strFileName = Server.HtmlEncode(FileImport.FileName);
// Get the extension of the Excel spreadsheet.
string strExtension = Path.GetExtension(strFileName);
// Validate the file extension.
if (strExtension == ".xls" || strExtension == ".xlsx")
{
// Generate the file name to save.
string strUploadFileName = "C:/Documents and Settings/user01/My Documents/Visual Studio 2005/WebSites/MajorProject/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
// Save the Excel spreadsheet on server.
FileImport.SaveAs(strUploadFileName);
// Create Connection to Excel Workbook
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Excel 8.0;";
using (OleDbConnection connection =
new OleDbConnection(connStr))
{
string selectStmt = string.Format("Select [COLUMNS] FROM [userlist$]");
OleDbCommand command = new OleDbCommand(selectStmt, connection);
connection.Open();
Console.WriteLine("Connection Opened");
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=<datasource>";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "UserDB";
bulkCopy.WriteToServer(dr);
return;
}
}
}
Below is my code snippet for the import function for .csv spreadsheet:
if (strExtension == ".csv")
{
// Generate the file name to save.
string dir = #"C:\Documents and Settings\user01\My Documents\Visual Studio 2005\WebSites\MajorProject\UploadFiles\";
string mycsv = DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
// Save the Excel spreadsheet on server.
BaanImport.SaveAs(dir + mycsv);
// Create Connection to Excel Workbook
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dir + ";Extended Properties=Text;";
using (OleDbConnection ExcelConnection = new OleDbConnection(connStr))
{
string selectStmt = string.Format("SELECT [COLUMNS] FROM " + mycsv);
OleDbCommand ExcelCommand = new OleDbCommand(selectStmt, ExcelConnection);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
ExcelConnection.Open();
using (DbDataReader dr = ExcelCommand.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=<datasource>";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "UserDB";
bulkCopy.WriteToServer(dr);
return;
}
}
}
}
May I know how could I change it such that it will not create a copy in the dir:#"C:\Documents and Settings\user01\My Documents\Visual Studio 2005\WebSites\MajorProject\UploadFiles\, but read the data directly from the imported file instead?
Thank you for any help in advance.
Assuming that the object you are working with for the imported files is an HttpPostedFile, then you can use its InputStream to read the file directly from its uploaded location.
See this MSDN documentation for more information and sample code.