如何在c#windows窗体中将数据从Excel文件导入到sql server [英] How Import data from Excel file to sql server in c# windows form

查看:238
本文介绍了如何在c#windows窗体中将数据从Excel文件导入到sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hiiiiiiiiiii ,,



如何在c#windows中将数据从Excel文件导入sql server ???

hiiiiiiiiiii,,

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

推荐答案

试试这个:



http://www.c-sharpcorner.com/uploadfile/vivek4u_swamy/import-data-from-excel-to-a-sql-server-database/ [ ^ ]


您可以使用 OleDB-SQL Utility [ ^ ]。



另请参阅Excel的连接字符串和SQL查询示例。例如:

You may use the code from the OleDB-SQL Utility[^].

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






这是使用SqlBulkCopy导入的主要功能:



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();
    }
}


这篇关于如何在c#windows窗体中将数据从Excel文件导入到sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆