Retrieving information from a database

I am going to get information from a database using LINQ, but I do not know why I am getting this error:

Invalid 'Retriveinfos' object name.

My class is here:

[Table (Name="Retriveinfos")] public class Retriveinfo { [Column] public string Name { get; set; } [Column] public string LastName { get; set; } [Column(IsPrimaryKey = true)] public int Id { get; set; } } 

and then using this line of code to connect and retrieve information:

 DataContext dcon = new DataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Second_School_project\Review_Site\Review_Site\App_Data\ReviewDatabase.mdf;Integrated Security=True;User Instance=True"); Table<Retriveinfo> Retriveinfos = dcon.GetTable<Retriveinfo>(); var c = from d in Retriveinfos where d.Id == 1 select new { d.Name, d.LastName }; foreach (var a in c) Response.Write(a.Name.ToString() + " " + a.LastName.ToString()); 
+6
source share
2 answers

Well Retriveinfos is a table in your database that is not a class, you may need to use something like an entity structure to create classes to represent tables before you can do something like the above

+2
source

What happens if you type dcon.xxx At this point you should get intellisense and you will get the correct names.

Also, there is no need to do GetTable, you can just do it like this

 var c = from d in dcon.Retriveinfos where d.Id == 1 select new { d.Name, d.LastName }; foreach (var a in c) { ... } 

By the way, if your identifier is really unique, you can do this:

 var c = dcon.Retriveinfos.SingleOrDefault(s=>s.id == 1) 

And you can also skip your foreach in this case

0
source

Source: https://habr.com/ru/post/906281/


All Articles