0

I have spent hours trying to figure out why my database cannot find the table I have found numerous examples and none have seemed to help. I have created a separate class to handle the database operations so I can use it on multiple pages. Here is the code

  [Table]
public class MatchItem
{
    [Column(IsPrimaryKey = true, CanBeNull=false,IsDbGenerated=true)]
    public int MatchID { get; set; }
    [Column(CanBeNull = false)]
    public string MatchNumber { get; set; }
    [Column(CanBeNull = false)]
    public string EventName { get; set; }
    [Column(CanBeNull = false)]
    public DateTime Time { get; set; }
    [Column(CanBeNull = false)]
    public string[] RedTeams { get; set; }
    [Column(CanBeNull = false)]
    public string[] BlueTeams { get; set; }
    [Column(CanBeNull = false)]
    public int RedFinal { get; set; }
    [Column(CanBeNull = false)]
    public int BlueFinal{ get; set; }
}

Here is the Data context

public class MatchDataContext:DataContext
{
    public MatchDataContext(string connectionString) :
        base(connectionString)
    {
    }
    public Table<MatchItem> Matches
    {
        get
        {
            return this.GetTable<MatchItem>();
        }
    }
}

I made a class so I could use it on multiple pages

public class MatchDBManager
{
    private static string connectionString = @"Data Source=isostore:/DB.sdf";

    public MatchDBManager()
    {

        initialize();
    }

    public void initialize()
    {
        using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
        {

            if (Mchdb.DatabaseExists())
            {
                Console.WriteLine("DB already exists");
            }
            else
            {
                Mchdb.CreateDatabase();
                Console.WriteLine("DB created");
            }
        }
    }
    public void addMatchData(IList<MatchItem> data)
    {
        //this.clearData();
        //initialize();
        using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
        {
            Mchdb.Matches.InsertAllOnSubmit(data);
            Mchdb.SubmitChanges();
        }

    }
    public IList<MatchItem> getTeamData(string teamNum)
    {
        IList<MatchItem> MatchList = null;
        using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
        {

            IQueryable<MatchItem> mchQuery = from mch in Mchdb.Matches where (mch.RedTeams[0] == teamNum || mch.RedTeams[1] == teamNum || mch.RedTeams[2] == teamNum || mch.BlueTeams[0] == teamNum || mch.BlueTeams[1] == teamNum || mch.BlueTeams[2] == teamNum) select mch;
            MatchList = mchQuery.ToList();

        }
        return MatchList;
    }
    public IList<MatchItem> getEventData(string eventID)
    {
        IList<MatchItem> MatchList = null;
        using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
        {
            IQueryable<MatchItem> mchQuery = from mch in Mchdb.Matches where mch.Event == eventID select mch;
            MatchList = mchQuery.ToList();
        }
        return MatchList;
    }
    private void clearData()
    {
        using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
        {

            if (Mchdb.DatabaseExists())
            {
                Mchdb.DeleteDatabase();

            }
        }
    }
}

I have the error The specified table does not exist[Match].

Added here is where I download

   public IList<MatchItem> ParseXML(XmlReader reader)
    {
        //List<string> save = new List<string>();
        List<MatchItem> MatchList= new List<MatchItem>();
        XElement matchData;
        matchData = XElement.Load(reader);
        StringBuilder output = new StringBuilder();
        int count = 0;
        var matches = from item

                         in matchData.Elements("match")

                      select item;

        foreach (XElement eachmatch in matches)
        {
            MatchItem mch = new MatchItem();
           string Time = ((eachmatch.Element("pubdate").Value).ToString());
            mch.EventName = ((eachmatch.Element("event").Value).ToString());
            mch.MatchNumber = ((eachmatch.Element("mch").Value).ToString() + (eachmatch.Element("typ").Value).ToString());
            string[] RT = { eachmatch.Element("red1").Value.ToString(), eachmatch.Element("red2").Value.ToString(), eachmatch.Element("red3").Value.ToString() };
            string[] BT = { eachmatch.Element("blue1").Value.ToString(), eachmatch.Element("blue2").Value.ToString(), eachmatch.Element("blue3").Value.ToString() };
            string RF = ((eachmatch.Element("rfin").Value).ToString());
            string BF = ((eachmatch.Element("bfin").Value).ToString());
           // Time = Time.Substring(0, (Time.IndexOf("+") - 1));
            mch.Time = DateTime.Parse(Time);
            mch.RedTeams = RT;
            mch.BlueTeams = BT;
            mch.RedFinal = int.Parse(RF);
            mch.BlueFinal= int.Parse(BF);
            mch.MatchID = count;
            count += 1;


            MatchList.Add(mch);
        }
        return MatchList;

    }

This is where I call this method

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

 initializeDB();
        if (e.Error == null)
        {
            XmlReader reader = XmlReader.Create(new StringReader(e.Result));
            DownloadInfo di = new DownloadInfo();
            IList <MatchItem>data= di.ParseXML(reader);
            outputer(data);
            addData(data.ToList<MatchItem>());
        }
        else
        {
            IList<MatchItem> data = getTeamData(strMyTeam);
            outputer(data);
        }
}

I ended up removing the DatabaseManager class and putting the functions in the main code Then I output them to the screen here

 public void outputer(IList<MatchItem> mch)
    {
        for (int i = 0; i < mch.Count; i++)
        {
            Score sc = new Score();

            sc.Width = lsbData.Width;
            sc.Height = sc.Height;
            sc.Time = mch[i].Time.ToString();
            sc.Event = mch[i].EventName;
            sc.RT = mch[i].RedTeams[0] + "  " + mch[i].RedTeams[1] + "  " + mch[i].RedTeams[2];
            sc.BT = mch[i].BlueTeams[0] + "  " + mch[i].BlueTeams[1] + "  " + mch[i].BlueTeams[2];
            sc.RF = mch[i].RedFinal.ToString();
            sc.BF = mch[i].BlueFinal.ToString();
            lsbData.Items.Add(sc);
        }

    }

*note:score is a custom control that works(and worked) before the database code *

4

1 回答 1

1

我没有看到您实际创建Match对象的位置。

如果您有,您还需要在问题中包含该代码。如果你没有,那可以解释为什么它不存在。

添加

为了将Match对象添加到列表中,您必须先创建对象然后将它们添加到列表中,我认为您不能在创建每个单独的对象之前创建整个对象列表。

更多附加信息

在您向其添加项目之前,仍需要创建该对象。这就是错误告诉你的。您没有要插入数据的对象。

Match Table1 = new Match();

这将创建一个新Match对象,它允许您访问对象的各个部分并将数据插入到对象中,如下所示

Table1.MatchNumber = 42

在您为特定的人留出该内存位置并为其命名之前,您无法将某些内容添加到内存位置。

Match当您创建该类时,您可以向其中添加函数和各种有趣的东西,但在创建对象之前您不能使用任何它。

您不能将某些内容添加到不存在的列表中,您必须先创建Match对象,然后将其添加到列表中

于 2013-07-29T21:27:40.377 回答