80 likes | 228 Views
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.
E N D
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
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
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
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
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
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
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
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