【问题标题】:Wp7 dataconnectivity errorWp7 数据连接错误
【发布时间】:2012-05-11 08:54:21
【问题描述】:

我有一个表 emp2,其中包含 Empid EmpName Salary

我在 emp 中从 c 传递了一个 linq,其中 empname=="ss" select c 现在我想显示那个 empname 的 empid 但是代码在linq处显示错误 错误 ::: Emp2.EmpName' 没有支持的 SQL 转换。在 countrylist=query.tolist();

private const string ConnectionString = "isostore:/Emp1.sdf";

    [Table(Name = "Emp")]
    public class Emp2
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int EmpId
        {
            get;
            set;
        }

        public string EmpName
        {
            get;
            set;
        }
        public int Salary
        {
            get;
            set;
        }
    }


    public MainPage()
    {
        InitializeComponent();

        using (CountryDataContext context = new CountryDataContext(ConnectionString))
        {

            if (!context.DatabaseExists())
            {

                context.CreateDatabase();

            }
            else
            {
                MessageBox.Show("Employee table exist already");
            }
        }




    }


    private IList<Emp2> getcountry()
    {
        IList<Emp2> countryList = null;
        using (CountryDataContext context = new CountryDataContext(ConnectionString))
        {
            IQueryable<Emp2> query = from c in context.Emp where c.EmpName=="ss"   select c;
            countryList = query.ToList();
        }

        return countryList;
    }



    public class CountryDataContext : DataContext
    {
        public CountryDataContext(string connectionString)
            : base(connectionString)
        {

        }
        public Table<Emp2> Emp
        {
            get
            {
                return this.GetTable<Emp2>();
            }
        }


    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IList<Emp2> emp= this.getcountry();

        foreach (Emp2 a in emp)
        {
            MessageBox.Show(a.EmpId.ToString());

        }

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    您尚未向EmpName 属性添加属性以表明它是一列。

    【讨论】:

    • 谢谢先生,这个特定的问题已经解决了,但是现在它一旦到达按钮代码中的 foreach 循环就会抛出一个异常 Invalid session id .. 我需要做什么来处理这个?
    • 您的异常是由于您从 getcountry 返回了一个查询,但是当您在按钮处理程序的 foreach 循环中使用查询 (IList&lt;Emp2&gt;) 时,@987654324你创建的@已经被 using 语句处理掉了。
    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2018-05-23
    • 2012-09-29
    • 2014-10-04
    相关资源
    最近更新 更多