2 u011289515 u011289515 于 2013.07.07 11:31 提问

C#谁能帮我看看那里出问题了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SuperMarket
{
class manager
{
public Array[] show(string sql)
{
DBHelper db = new DBHelper();
int i = 0;
Array[] arr = new Array[100];
try
{

            SqlCommand comm = new SqlCommand(sql,db.Conn);
            db.Open();
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                Page pa = new Page();
                pa.Goodsld = Convert.ToInt32(read["Goodsld"]);
                pa.GoodName = read["GoodName"].ToString();
                pa.Price = read["Price"].ToString();
                pa.TypeName = read["TypeName"].ToString();
                pa.ProdureCounts = Convert.ToInt32(read["ProdureCounts"]);
                arr[i] = pa(此处报错);
                i++;
            }
        }
        catch (Exception)
        {
            Console.WriteLine("异常");
        }
        finally
        { 
            db.Close();
        }
        return arr;
    }

    public int show1(string sql)
    {
        DBHelper db = new DBHelper();
        int k = 0 ;
        try
        {

            SqlCommand comm = new SqlCommand(sql, db.Conn);
            db.Open();
            k = (int)comm.ExecuteNonQuery();
        }
        catch (Exception)
        {
            Console.WriteLine("异常");
        }
        finally
        {
            db.Close();
        }
        return k;
    }
}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperMarket
{
class sqlmanager
{
Page pa = new Page();
manager man = new manager();
public void showmanager()
{
Console.WriteLine("1.查询全部物品信息");
Console.WriteLine("2.查询指定商品价格");
Console.WriteLine("3.增加物品信息");
Console.WriteLine("输入其他字符串退出系统");
int num = Convert.ToInt32(Console.ReadLine());
if (num == 1)
{
sqla();
}
else if (num==2)
{
sqlb();
}
else if (num == 3)
{
sqlc();
}
}
public void sqla()
{

        string sql = "select * from Goods";
        Array[] arr = man.show(sql);
        foreach (Array item in arr)
        {
            if (item != null)
            {
                Console.WriteLine("{0},{1},{2},{3},{4}",pa.Goodsld,pa.GoodName,pa.Price,pa.TypeName,pa.ProdureCounts);
            }
        }
    }

1个回答

aspx0571
aspx0571   2013.07.11 15:21
已采纳

Array[] arr = new Array[100];
很可疑~~~
如果你能把调试的错误信息贴出来,能更好的解决问题.

建议还是不要用Array了,因为你的写法可能存在数组索引溢出的风险(数据记录数大于100的情况下).
给你一段以下代码参考(你的代码中完全可以用List取代Array)

    public List<GroupService> CreateGroup(Group group)
    {
        GroupService service = new GroupService();
        List<GroupService> list = new List<GroupService>();
        list.Add(service);
        return list;
    }
Csdn user default icon
上传中...
上传图片
插入图片