当前上下文中不存在名称函数 c# 错误

the name function does not exists in the current context c# error

我编写了一个 SqlBulkCopy 程序,用于将 CSV 数据导入 MYSQL 数据库。在 InsertDataIntoSQLServerUsingSQLBulkCopy 函数中触发一些错误,如下图所示。

我在下面附上了我的源代码。错误在第 54 行触发。

 using System;
using System.Data;
using Microsoft.VisualBasic.FileIO;
using System.Data.SqlClient;

namespace ReadDataFromCSVFile
{
    static class Program
    {
        static void Main()
        {
            string csv_file_path = @"C:\Users\source\repos\WindowsService1\WindowsService1\bin\Debug\data.csv";
            DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
            Console.WriteLine("Rows count:" + csvData.Rows.Count);
            Console.ReadLine();
        }
        private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable csvData = new DataTable();
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                        }
                        csvData.Rows.Add(fieldData);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return csvData;
        }

       **line 54  function static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)             {
            using (SqlConnection dbConnection = new SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=MorganDB; Integrated Security=SSPI;"))
            {
                dbConnection.Open();
                using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
                {
                    s.DestinationTableName = "table1";

                    foreach (var column in csvFileData.Columns)
                        s.ColumnMappings.Add(column.ToString(), column.ToString());

                    s.WriteToServer(csvFileData);
                }
            }

        }
    }
}

如果有人能发现错误,我将不胜感激。谢谢!

您是否对 Visual BASIC 感到困惑? function 不是 C# 关键字。将 function 替换为 privatepublic,具体取决于用例。

此外,您应该能够在包含中使用 System.IO 而不是 Microsoft.VisualBasic.FileIo

我想你已经添加了“**第 54 行”来指示错误所在?

从你的屏幕抓取来看,文本 function 似乎出现在下面。

   **line 54  function static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)             {
        using (SqlConnection dbConnection = new SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=MorganDB; Integrated Security=SSPI;"))
        { 

在 C# 中,您不需要(事实上不能)像在 VB 中那样声明 function。该行应该是:

static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)             
{
   // Rest of code
}