我如何解决索引超出数组的范围 [英] How do I the solve index was outside the bounds of the array

查看:265
本文介绍了我如何解决索引超出数组的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string vertex = "select vertex_path from Tbl_AllGraphInfo where Graph_Id='" + Graph_Id + "'";
            SqlCommand cmd = new SqlCommand(vertex,con11);
            con11.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                fullpath = dr.GetString(0);

            }
            dr.Close();
//            string actresult = ((string)cmd.ExecuteScalar());
            con11.Close();
            int counter = 0;
            string[] array = fullpath.Split(';');
            string[] array2 = new string[2];
            List<string> list = new List<string>();
            for (int i = 0; i < array.Length; i++)
            {
               array2= array[i].Split(',');
               list.Add(array2[1]);


            }
            counter = 0;

            string[] pattern = (string[])list.ToArray();



我尝试过的事情:

索引不在数组的范围内



What I have tried:

Index was outside the bounds of the array

推荐答案

使用调试器并逐步浏览应用程序:由于无法访问,我们无法做到这一点您的数据.
在该代码的第一行上放置一个断点,然后在调试器中运行它.当执行到达断点时,它将停止并让您控制.使用调试器检查变量的内容,然后逐步执行程序,在执行每个程序之前先弄清您的期望.
您发生了什么预期吗?如果是这样,请继续下一行.如果没有,为什么不呢?发生了什么?导致这种情况发生的变量是什么?

我们无法为您做任何事情-我们无权访问您的数据,并且我认为该代码中没有错误-它发生在您未向我们显示的行上.因此,请尝试一下,看看您能找到什么!

顺便说一句:不要串联字符串来构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.改为使用参数化查询.
Use the debugger and step through your application: we can''t do that as we don''t have access to your data.
Put a breakpoint on the first line of that code, and run it in the debugger. When execution reaches the breakpoint it will stop and let you have control. Use the debugger to examine variable contents, and step through the program line by line, working out what you expect to happen before you execute each one.
Did you what expected happen? If so, continue with the next line. If not, why not? What did happen? What was in the variables that causes this to happen?

We can''t do any of this for you - we don''t have access to your data, and I don''t think the error is in that code - it occurs on a line you haven''t shown us. So give it a try, and see what you can find out!

BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于我如何解决索引超出数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆