C#编程数据导入

这篇博客探讨了如何在C#编程中使用SqlHelper.cs辅助类来实现数据导入功能,结合App.config配置文件进行数据库连接,详细阐述了执行数据库操作的过程。
摘要由CSDN通过智能技术生成

SQL helper 文件    

SqlHelper.cs


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 数据导入
{
    class SqlHelper
    {
        private static string connStr = ConfigurationManager.ConnectionStrings["database"].ConnectionString;//连接数据库放在配置文件中
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    //foreach (SqlParameter param in parameters)
                    //{
                    //    cmd.Parameters
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用C#编程语言将CSV数据导入Excel。这里是一个示例代码,演示了如何实现这个功能: ```csharp using System; using System.Data; using System.IO; using Excel = Microsoft.Office.Interop.Excel; namespace CSVtoExcel { class Program { static void Main(string[] args) { string csvFilePath = "path_to_csv_file.csv"; string excelFilePath = "path_to_excel_file.xlsx"; // 创建一个新的Excel应用程序实例 Excel.Application excelApp = new Excel.Application(); excelApp.Visible = false; // 打开CSV文件 DataTable dt = new DataTable(); using (StreamReader sr = new StreamReader(csvFilePath)) { string[] headers = sr.ReadLine().Split(','); foreach (string header in headers) { dt.Columns.Add(header); } while (!sr.EndOfStream) { string[] rows = sr.ReadLine().Split(','); DataRow dr = dt.NewRow(); for (int i = 0; i < headers.Length; i++) { dr[i] = rows[i]; } dt.Rows.Add(dr); } } // 在Excel中创建一个新的工作簿 Excel.Workbook workbook = excelApp.Workbooks.Add(Type.Missing); Excel.Worksheet worksheet = workbook.ActiveSheet; // 将DataTable中的数据写入Excel工作表 for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { worksheet.Cells[i + 1, j + 1] = dt.Rows[i][j].ToString(); } } // 保存Excel文件并关闭应用程序 workbook.SaveAs(excelFilePath); workbook.Close(); excelApp.Quit(); Console.WriteLine("CSV数据已成功导入Excel文件!"); } } } ``` 在上面的示例中,你需要将`csvFilePath`替换为你的CSV文件的路径,将`excelFilePath`替换为你希望保存Excel文件的路径。这段代码将读取CSV文件的数据,并将其写入一个新的Excel工作簿中。最后,保存Excel文件并关闭应用程序。 请注意,使用此示例代码需要安装Microsoft.Office.Interop.Excel库。你可以在Visual Studio中通过NuGet包管理器安装该库。 希望这能帮助到你!如果你有任何其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值