Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a problem in my code for copy data from excell to sql using desktop appliction

i find error

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

and i don't know what is the problem

the excell file include one sheet include only 4 rows and the remain is empty:

george 1 1 1
willum 1 1 1
mickel 1 1 1
jinnefr 1 1 1

where all column types in sheet ara general

and sql database include table called childern include 9 columns

and i use 4 columns and datatypes ara in order (varchar(50)---smallint----tinyint----tinyint) not allow null

but the other coluns allow null


my code behind

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;

namespace ExcellToSql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
       OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Mode='Write';Integrated Security=SSPI;Data Source=D:\\MyData.xlsx;Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);
                con.Open();
                OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", con);
                OleDbDataReader odr = cmd.ExecuteReader();
                string name = " ";
                short country = 0;
                byte status = 0;
                byte type = 0;
                while (odr.Read())
                {
                    name = valid(odr, 0);
                    country = short.Parse(valid(odr, 1));
                    status = byte.Parse(valid(odr, 2));
                    type = byte.Parse(valid(odr, 3));
                    insertdataintosql(name, country, status, type);
                }
                con.Close();
            
        }
        public void insertdataintosql(string name, short country, byte status, byte type)
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "insert into Client(name,country,status,type) values(@name,@country,@status,@type)";
        cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
        cmd.Parameters.Add("@country", SqlDbType.SmallInt).Value = country;
        cmd.Parameters.Add("@status", SqlDbType.TinyInt).Value = status;
        cmd.Parameters.Add("@type", SqlDbType.TinyInt).Value = type;
        
        cmd.CommandType = CommandType.Text;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
 
        
        protected string valid(OleDbDataReader myreader, int stval)
				
    {
        object val = myreader[stval];
        if (val != DBNull.Value)
            return val.ToString();
        else
            return Convert.ToString(0);
    }
    
        }
    }


so i need any one tell me how i can solve this error
Posted
Updated 14-Aug-12 2:09am
v4

1 solution

For the first look... Wrong OLEDB connection to MS Excel file!
See here: http://www.connectionstrings.com/[^], section Data Files - MS Excel 2007.

Quote:

ACE OLEDB 12.0
Type OLE DB Provider
Usage Provider=Microsoft.ACE.OLEDB.12.0
Manufacturer Microsoft

This one is for connecting to Excel 2007 files with the Xlsx file extension. That is the Office Open XML format with macros disabled.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.
 
Share this answer
 
Comments
hallo2012 14-Aug-12 8:24am    
i write again connection string OleDbConnection
hallo2012 14-Aug-12 8:25am    
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Integrated Security=SSPI;Extended Properties=Excel 12.0 Xml;HDR=NO;Data Source=D:\\MyData.xlsx;Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);
Maciej Los 14-Aug-12 8:30am    
So what? You had made an error, using "Extended properties" twice:
... Extended Properties=Excel 12.0 Xml;HDR=NO; ...
Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);

Do not "produce" comments!
hallo2012 15-Aug-12 4:31am    
the error is not in this i find that i don't need to all in the connection string
hallo2012 14-Aug-12 8:25am    
and the error is the same "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900