索引超出了数组的范围 - 插入数据库

时间:2016-01-29 16:51:04

标签: c# arrays

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

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

1 个答案:

答案 0 :(得分:4)

当你不需要时,你会循环翻译。 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();
}

您可能还想了解the implications of using AddWithValue