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);
}
}
}
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;
}