1 / 8

CS 341 Programming Language Design and Implementation

CS 341 Programming Language Design and Implementation. Administrative HW7 due tonight @ 9pm Quiz Friday, 3/21 Topics: declarative paradigm, SQL, ADO.NET Then spring break! Today: LINQ. Dynamic vs. Stored Procedures. more flexible, easier to change ("agile"). more efficient, safer.

prema
Download Presentation

CS 341 Programming Language Design and Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 341 Programming Language Design and Implementation • Administrative • HW7 due tonight @ 9pm • Quiz Friday, 3/21 • Topics: declarative paradigm, SQL, ADO.NET • Then spring break! • Today: • LINQ CS 341 -- 19 March 2014

  2. Dynamic vs. Stored Procedures more flexible,easier to change ("agile") more efficient,safer DynamicSQL query strings Stored procedures in DB OracleCommandcmd = new OracleCommand(db); cmd.CommandText = "SELECT MovieName WHERE MovieID=" + ID; object result = cmd.ExecuteScalar(); OracleCommandcmd = new OracleCommand(db); cmd.CommandText = "GetMovieName"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@MovieID", ID)); object result = cmd.ExecuteScalar(); CS 341 -- 19 March 2014

  3. Is there a middle ground? • If SQL is so important, why not embed into language? • Someone did! • PhD thesis by Erik Meijer • Hired by Microsoft to integrate his work into C# CS 341 -- 19 March 2014

  4. LINQ: • LINQ == Language Integrated Query • Great example of language design to solve common problem • SQL-like syntax embedded into C# • compiler uses DB schema to enable static type checking & type inference • extensible… CS 341 -- 19 March 2014

  5. LINQ Example: SELECT MovieName FROM Movies WHEREMovieID= 124; The Seventh Seal string connectionInfo = "Data Source=Netflix-65K.sdf"; SqlCeConnectiondb = new SqlCeConnection(connectionInfo); db.Open(); SqlCeCommandcmd = new SqlCeCommand(); cmd.Connection = db; cmd.CommandText = "SELECT MovieName FROM Movies WHERE …;" ; object result = cmd.ExecuteScalar(); db.Close(); // LINQ: varquery = fromm innetflix.Movieswherem.MovieID == 124 selectm.MovieName; foreach(var name in query) MessageBox.Show(name); CS 341 -- 19 March 2014

  6. LINQ supports most features of SQL: • Complex joins, table creation, insert, update, delete, etc. • Google “101 LINQ samples in C#” + SELECT AVG(Rating) FROM Reviews INNER JOIN Movies ON Reviews.MovieID = Movies.MovieID WHERE Movies.MovieName = 'Finding Nemo'; 2.92 varquery = fromm in netflix.Movies joinr in netflix.Reviews onm.MovieIDequalsr.MovieID wherem.MovieName == "Finding Nemo" selectr.Rating; varavg = query.Average( (rating) => rating ); Console.WriteLine(avg); CS 341 -- 19 March 2014

  7. Working with "LINQ to SQL"… • You need to make database schemaknown to C# • Steps in Visual Studio: • Open your project… • Project >> Add New Data Source… • Select Database, then Dataset, add New Connection… • save under the name "NetflixDB" • Visual Studio adds necessary database info to project LINQ can query most data sources — LINQ to XML, LINQ to Data Structures, LINQ to Text Files, etc. CS 341 -- 19 March 2014

  8. Connecting "LINQ to SQL" to underlying database… // At startup, build local in-memory copy of database: NetflixDBnetflix= new NetflixDB(); NetflixDBTableAdapters.MoviesTableAdaptermovieAdapter= new NetflixDBTableAdapters.MoviesTableAdapter(); NetflixDBTableAdapters.ReviewsTableAdapterreviewAdapter= new NetflixDBTableAdapters.ReviewsTableAdapter(); movieAdapter.Fill( netflix.Movies); reviewAdapter.Fill( netflix.Reviews); CS 341 -- 19 March 2014

More Related