Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
hiiiiiiiiiii,,

How Import data from Excel file to sql server in c# windows form ???
Posted

 
Share this answer
 
Comments
a7med ssss 27-Jun-14 11:22am    
look for this error that appear when connection is open
Could not find installable ISAM.
You may use the code from the OleDB-SQL Utility[^].

See also there examples of connection strings and SQL queries to Excel. For example:
SQL
SELECT NULL AS ImportID, * FROM [Sheet1$]


Here is the main function to import using SqlBulkCopy:
static void OleDbToSqlServer(string oleDbConnectionString, string oleDbSQL,
    string sqlConnectionString, string sqlTableName)
{

    oleDbConnectionString = ExpandConnectionStringFileName(oleDbConnectionString);

    OleDbConnection oleDbConnection = new OleDbConnection(oleDbConnectionString);
    try
    {
        oleDbConnection.Open();
        OleDbCommand command = new OleDbCommand(oleDbSQL, oleDbConnection);
        try
        {
            OleDbDataReader reader = command.ExecuteReader();
            try
            {
                SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
                try
                {
                    sqlConnection.Open();
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnection))
                    {
                        bulkCopy.DestinationTableName = sqlTableName;
                        bulkCopy.WriteToServer(reader);
                    }
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
            finally
            {
                reader.Close();
            }
        }
        finally
        {
            command.Dispose();
        }
    }
    finally
    {
        oleDbConnection.Close();
    }
}
 
Share this answer
 
look for this error that appear when connection is open
Could not find installable ISAM.
 
Share this answer
 

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