static void EntityClientDemo() { string connectionString = ConfigurationManager.ConnectionStrings["BooksEntities"].ConnectionString; var connection = new EntityConnection(connectionString); connection.Open(); EntityCommand command = connection.CreateCommand(); command.CommandText = "[BooksEntities].[Books]"; // command.CommandText = "SELECT Books.Title, Books.Publisher FROM [BooksEntities].[Books]"; // command.CommandText = "SELECT VALUE it FROM BooksEntities.Books AS it WHERE it.Publisher = @Publisher"; command.Parameters.AddWithValue("Publisher", "Wrox Press"); EntityDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SequentialAccess); while (reader.Read()) { Console.WriteLine("{0}, {1}", reader["Title"], reader["Publisher"]); } reader.Close(); }
private void ActDataTable_Click(System.Object sender, System.EventArgs e) { // ----- Query that returns an ADO.NET DataTable. DataRow oneRow; DataTable resultsAsTable; EntityCommand query; EntityDataReader results = null; string sqlText; // ----- Clear any previous results. DisplayResults.DataSource = null; // ----- Build a storage area for the results. resultsAsTable = new DataTable(); resultsAsTable.Columns.Add("CustomerID", typeof(long)); resultsAsTable.Columns.Add("CustomerName", typeof(string)); resultsAsTable.Columns.Add("AnnualFee", typeof(decimal)); // ----- Connect to the entity provider. using (EntityConnection linkToDB = new EntityConnection(GetConnectionString())) { try { linkToDB.Open(); } catch (Exception ex) { MessageBox.Show("Error opening the data connection: " + ex.Message); return; } // ----- Retrieve the data via a parameterized query. try { } catch (Exception ex) { MessageBox.Show("Error processing parameterized query: " + ex.Message); return; } try { // ----- Move each row into the DataTable. } catch (Exception ex) { MessageBox.Show("Error saving query results: " + ex.Message); return; } finally { results.Close(); } // ----- Display the results to the user. try { DisplayResults.DataSource = resultsAsTable; MessageBox.Show(resultsAsTable.Rows.Count + " result(s)"); } catch (Exception ex) { MessageBox.Show("Error displaying query results: " + ex.Message); } } }
private void ActDataTable_Click(System.Object sender, System.EventArgs e) { // ----- Query that returns an ADO.NET DataTable. DataRow oneRow; DataTable resultsAsTable; EntityCommand query; EntityDataReader results = null; string sqlText; // ----- Clear any previous results. DisplayResults.DataSource = null; // ----- Build a storage area for the results. resultsAsTable = new DataTable(); resultsAsTable.Columns.Add("CustomerID", typeof(long)); resultsAsTable.Columns.Add("CustomerName", typeof(string)); resultsAsTable.Columns.Add("AnnualFee", typeof(decimal)); // ----- Connect to the entity provider. using (EntityConnection linkToDB = new EntityConnection(GetConnectionString())) { try { linkToDB.Open(); } catch (Exception ex) { MessageBox.Show("Error opening the data connection: " + ex.Message); return; } // ----- Retrieve the data via a parameterized query. sqlText = @"SELECT CU.ID, CU.FullName, CU.AnnualFee FROM SalesOrderEntities.Customers AS CU WHERE CU.AnnualFee >= @MinFee ORDER BY CU.FullName"; try { query = new EntityCommand(sqlText, linkToDB); query.Parameters.AddWithValue("MinFee", 200); results = query.ExecuteReader(CommandBehavior.SequentialAccess); } catch (Exception ex) { MessageBox.Show("Error processing parameterized query: " + ex.Message); return; } try { // ----- Move each row into the DataTable. while (results.Read()) { oneRow = resultsAsTable.NewRow(); oneRow["CustomerID"] = (long)results["ID"]; oneRow["CustomerName"] = (string)results["FullName"]; oneRow["AnnualFee"] = (decimal)results["AnnualFee"]; resultsAsTable.Rows.Add(oneRow); } } catch (Exception ex) { MessageBox.Show("Error saving query results: " + ex.Message); return; } finally { results.Close(); } // ----- Display the results to the user. try { DisplayResults.DataSource = resultsAsTable; MessageBox.Show(resultsAsTable.Rows.Count + " result(s)"); } catch (Exception ex) { MessageBox.Show("Error displaying query results: " + ex.Message); } } }