首页 新闻 会员 周边 捐助

C#连接数据库代码,求大神帮忙看看是哪出了问题

0
悬赏园豆:80 [已解决问题] 解决于 2015-10-24 23:20
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);
            }
        }
    }
KeepStatus的主页 KeepStatus | 初学一级 | 园豆:130
提问于:2015-10-24 21:39
< >
分享
最佳答案
0
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;
    }
收获园豆:80
请叫我头头哥 | 大侠五级 |园豆:9382 | 2015-10-24 23:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册