Zaveed Abbasi
Zaveed Abbasi

Reputation: 446

How to convert Ods file to Data Set C#

I am unable to convert ods file to data set

I have downloaded the file from follwoing link UK List ods file link

and using following piece of code to convert this ods file to data set

int count = 0;
            int list_clear = 1;
            string connectionString;
            if (ConfigurationManager.AppSettings["OperatingSystem"] == "64Bits")
                connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=" + location  + ";Extended Properties=\"Excel 12.0;\"";
            else
                connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                    location + @";Extended Properties=""Excel 8.0;HDR=YES;""";
    
    
               int recCount = 0;
                string xmlData = "";
                // Create a factory
                DbProviderFactory factory =
                       DbProviderFactories.GetFactory("System.Data.OleDb");
    
                // Create an adapter
                DbDataAdapter adapter = factory.CreateDataAdapter();
    
                // Create a select command to get the dataset
                DbCommand selectCommand = factory.CreateCommand();
                //selectCommand.CommandText = "SELECT * FROM [sanctionsconlist$]";
                selectCommand.CommandText = "SELECT * FROM [sheet1$]";// "SELECT * FROM [Data$]";
    
                // Set up the connection and the connection string
                DbConnection connection = factory.CreateConnection();
    
                connection.ConnectionString = connectionString;
                selectCommand.Connection = connection;
    
                // Set up the command
                adapter.SelectCommand = selectCommand;
    
                // Create the dataset
                DataSet ds = new DataSet();
                DataSet dsData = new DataSet();
                // Load the results
                adapter.Fill(ds);
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString);
                cmd = new SqlCommand();
                con.Open();
                tran = con.BeginTransaction();
                cmd.CommandText = "USP_ImportUKListSanctionListData";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                cmd.Transaction = tran;
                cmd.Parameters.Add("@xmlData", SqlDbType.NVarChar, -1);
                cmd.Parameters.Add("@list_clear_flag", SqlDbType.Int);
                cmd.Parameters.Add("@update_list", SqlDbType.Int);
                cmd.Parameters.Add("@Error", SqlDbType.Int);
                DataTable dt = ds.Tables[0].Clone();

but i get the error, external table is not in the expected format

Can any one help me in this reagard, thanks

Upvotes: 0

Views: 375

Answers (0)

Related Questions