索引超出数组边界 - 正在插入数据库

本文关键字:插入 数据库 边界 数组 索引 | 更新日期: 2023-09-27 18:32:37

据我所知,如果数组中的字段数与预期不匹配,就会发生此错误?这让我感到困惑,因为我的数据库中有 8 个字段,并且插入了 8 个参数。

样品.txt

"105"|2015-01-01 00:00:00|"500"|"John Walsh"|"Facebook"|"Joe"|"Schmoe"|"(555) 555-5555"
"555"|2016-05-20 12:40:00|"780"|"Justin Smith"|"Twitter"|"Daniel"|"Smith"|"(000) 000-0000"

法典

static void Main(string[] args)
{
    String line;
    try
    {
        string sConnectionString = "Data Source=localhost''SQLEXPRESS;Initial Catalog=MyDB;User ID=MyUsername;Password=MyPassword";
        SqlConnection objConn = new SqlConnection(sConnectionString);
        objConn.Open();
        StreamReader sr = new StreamReader("C:''Users''ME''Documents''sample.txt");
        line = sr.ReadLine();
        char delimiterChar = '|';
        while (line != null)
        {
            string[] words = line.Split(delimiterChar);
            foreach (string s in words)
            {
                Console.WriteLine(s);
                string sSQL = "INSERT INTO Details " + "(Id, Date, customerId, customerName, profile, shopOwnerFirstName, shopOwnerLastName, shopOwnerPhone) " + "VALUES (@Id, @Date, @customerId, @customerName, @profile, @shopOwnerFirstName, @shopOwnerLastName, @shopOwnerPhone)";
                SqlCommand objCmd = new SqlCommand(sSQL, objConn);
                objCmd.Parameters.AddWithValue("@Id", s[0]);
                objCmd.Parameters.AddWithValue("@Date", s[1]);
                objCmd.Parameters.AddWithValue("@customerId", s[2]);
                objCmd.Parameters.AddWithValue("@customerName", s[3]);
                objCmd.Parameters.AddWithValue("@profile", s[4]);
                objCmd.Parameters.AddWithValue("@shopOwnerFirstName", s[5]);
                objCmd.Parameters.AddWithValue("@shopOwnerLastName", s[6]);
                objCmd.Parameters.AddWithValue("@shopOwnerPhone", s[7]);
                objCmd.ExecuteNonQuery();
            }
            line = sr.ReadLine();
        }
        sr.Close();
        Console.ReadLine();
}
catch (Exception e)
{
    Console.WriteLine("Exception: " + e.Message);
    Console.ReadKey();
}
finally
{
    Console.WriteLine("Executing Finally Block");
    Console.ReadKey();
}

索引超出数组边界 - 正在插入数据库

当你不需要的时候,你会循环播放单词。 s是数组中的单个条目,当您对其使用索引器时,您将获得单个字符。 例如,由于"500"没有 8 个字符,因此您就出界了。 你的代码应该更像:

while (line != null)
{
    string[] words = line.Split(delimiterChar);
    string sSQL = "INSERT INTO Details " + "(Id, Date, customerId, customerName, profile, shopOwnerFirstName, shopOwnerLastName, shopOwnerPhone) " + "VALUES (@Id, @Date, @customerId, @customerName, @profile, @shopOwnerFirstName, @shopOwnerLastName, @shopOwnerPhone)";
    SqlCommand objCmd = new SqlCommand(sSQL, objConn);
    objCmd.Parameters.AddWithValue("@Id", words[0]);
    objCmd.Parameters.AddWithValue("@Date", words[1]);
    objCmd.Parameters.AddWithValue("@customerId", words[2]);
    objCmd.Parameters.AddWithValue("@customerName", words[3]);
    objCmd.Parameters.AddWithValue("@profile", words[4]);
    objCmd.Parameters.AddWithValue("@shopOwnerFirstName", words[5]);
    objCmd.Parameters.AddWithValue("@shopOwnerLastName", words[6]);
    objCmd.Parameters.AddWithValue("@shopOwnerPhone", words[7]);
    objCmd.ExecuteNonQuery();
    line = sr.ReadLine();
}

您可能还需要了解使用 AddWithValue 的含义。