0

I am using VS2010 C# Express and MS SQL Server 2012.

Using C# I'm trying to query a test database I have created.

My C# code is as follows:

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

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataContext db = new DataContext("C:\\Program Files\\Microsoft SQL         Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\test.mdf");
                Table<Portfolios> PORTFOLIO = db.GetTable<Portfolios>();
                var q =
                    from c in PORTFOLIO
                    where c.PORTF_CODE == "PTF1"
                    select c;
                Console.WriteLine(q);
                foreach (var portf in q)
                {
                    Console.WriteLine("id = {0}, City = {1}", portf.PORTF_CODE,         portf.PORTF_NAME);
                    Console.ReadLine();
                }
            }
        }

        [Table(Name = "PORTFOLIO")]
        public class Portfolios
        {
            [Column(IsPrimaryKey = true)]
            public int PORTF_ID;
            [Column]
            public string PORTF_CODE;
            [Column]
            public string PORTF_NAME;
            [Column]
            public int BENCH_ID;
            [Column]
            public int CCY_ID;
        }
    }

The PORTFOLIO table that I am trying to query was created using the below code:

CREATE TABLE PORTFOLIO
( PORTF_ID      INT             IDENTITY(10000, 1)          PRIMARY KEY CLUSTERED
, PORTF_CODE    VARCHAR(25)     NOT NULL
, PORTF_NAME    VARCHAR(200)    NOT NULL
, BENCH_ID      INT             
, CCY_ID        INT)

INSERT INTO PORTFOLIO(PORTF_CODE, PORTF_NAME)
VALUES('PTF1', 'PTF1 - Portfolio 1');

Once I have created the above, I detach the database and run the c# code. The error I get is when I step over the code line:

foreach (var portf in q)

The error says: Invalid object name 'PORTFOLIO'. I assumed this meant that it couldn't find the PORTFOLIO object in my database, but I have checked several times that it is there and that there is data in it. I have even recreated the database several times to be sure.

Can anyone spot why it can't find the PORTFOLIO Object please? Thanks

4

1 回答 1

1

目前尚不清楚为什么您会遇到这种问题,但是您的代码没有任何问题,并且经过很少的修改,它也可以在我的计算机上运行。您应该注意的是,您正在处理计算机中的 MSSQL 服务器也在处理的 DB 文件(因为您的 MDF 文件路径现在看起来像这样)。

为了避免可访问性问题,对分离的数据库工作会更好。

因此,我建议您像我一样采取以下步骤来解决它:

  1. 将您的“测试”数据库与 MSSQL 服务器分离,以便独立处理它:在 MSSMS -> 右键单击​​您的数据库 -> 任务 -> 分离 -> 选择要分离的“测试”数据库。

  2. 打开此文件夹:“C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA”并将 test.mdf 和 test_log.ldf 文件复制到“C:\Databases”(首先创建此文件夹)。

  3. 我对您的代码进行了一些更改,因此请复制并尝试运行它:

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace ConsoleApplication1
{
    static class Program
    {
        static void Main()
        {
            var db = new DataContext("C:\\Databases\\test.mdf");

            var portfolio = db.GetTable<Portfolios>();

            var result =
                from c in portfolio
                where c.PORTF_CODE == "PTF1"
                select c;

            foreach (var item in result)
            {
                Console.WriteLine("id = {0}, City = {1}", item.PORTF_CODE, item.PORTF_NAME);
            }

            Console.ReadLine();
        }
    }

    [Table(Name = "PORTFOLIO")]
    public class Portfolios
    {
        [Column(IsPrimaryKey = true)]
        public int? PORTF_ID;

        [Column]
        public string PORTF_CODE;

        [Column]
        public string PORTF_NAME;

        [Column]
        public int? BENCH_ID;

        [Column]
        public int? CCY_ID;
    }
}
于 2013-08-11T22:37:37.037 回答