首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

将excel文件保存在应用程序文件夹中,并使用windows窗体和C#将内容上载到SQL Server

将excel文件保存在应用程序文件夹中,并使用Windows窗体和C#将内容上传到SQL Server的步骤如下:

  1. 首先,确保已经安装了Microsoft Office Excel和SQL Server,并且在C#项目中引用了相应的库文件。
  2. 在Windows窗体应用程序中,创建一个用于选择excel文件的文件对话框。可以使用OpenFileDialog类来实现,设置Filter属性为Excel文件类型,然后调用ShowDialog方法显示对话框并获取用户选择的文件路径。
  3. 使用C#的Excel操作库,如NPOI或EPPlus,打开选中的excel文件。这些库可以帮助读取和操作excel文件中的数据。
  4. 读取excel文件中的数据,并将其存储在一个数据结构中,如DataTable。
  5. 创建一个与SQL Server数据库连接的连接字符串,包括服务器名称、数据库名称、身份验证方式和登录凭据等信息。
  6. 使用ADO.NET库中的SqlConnection类,根据连接字符串建立与SQL Server的连接。
  7. 创建一个SQLCommand对象,编写插入数据的SQL语句,将excel文件中的数据逐行插入到SQL Server数据库中。
  8. 执行SQLCommand对象的ExecuteNonQuery方法,将数据插入到SQL Server数据库中。
  9. 关闭与SQL Server的连接。

下面是一个示例代码,演示了如何将excel文件内容上传到SQL Server:

代码语言:csharp
复制
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace ExcelToSQL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel Files|*.xlsx;*.xls";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;

                // 读取excel文件
                IWorkbook workbook;
                using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    workbook = new XSSFWorkbook(fileStream);
                }

                // 获取第一个工作表
                ISheet sheet = workbook.GetSheetAt(0);

                // 创建连接字符串
                string connectionString = "Data Source=YourServerName;Initial Catalog=YourDatabaseName;Integrated Security=True";

                // 建立与SQL Server的连接
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    // 遍历excel文件中的每一行
                    for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);

                        // 读取每一列的数据
                        string column1 = row.GetCell(0).ToString();
                        string column2 = row.GetCell(1).ToString();
                        // ...

                        // 构建插入数据的SQL语句
                        string sql = $"INSERT INTO YourTableName (Column1, Column2) VALUES ('{column1}', '{column2}')";

                        // 执行插入操作
                        using (SqlCommand command = new SqlCommand(sql, connection))
                        {
                            command.ExecuteNonQuery();
                        }
                    }
                }

                MessageBox.Show("数据上传成功!");
            }
        }
    }
}

请注意,上述示例代码仅供参考,实际应用中可能需要根据具体情况进行修改和优化。另外,为了保证安全性和性能,建议在实际应用中添加适当的错误处理、数据验证和批量插入等功能。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券