<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nathan Brixius : SQL</title><link>http://blogs.msdn.com/natbr/archive/tags/SQL/default.aspx</link><description>Tags: SQL</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Solver Foundation LINQ to SQL example</title><link>http://blogs.msdn.com/natbr/archive/2009/06/02/solver-foundation-linq-to-sql-example.aspx</link><pubDate>Tue, 02 Jun 2009 17:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9685593</guid><dc:creator>Nathan Brixius</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/natbr/comments/9685593.aspx</comments><wfw:commentRss>http://blogs.msdn.com/natbr/commentrss.aspx?PostID=9685593</wfw:commentRss><wfw:comment>http://blogs.msdn.com/natbr/rsscomments.aspx?PostID=9685593</wfw:comment><description>&lt;P&gt;Erwin, a modeling consultant and top Solver Foundation user, &lt;A href="http://yetanothermathprogrammingconsultant.blogspot.com/2009/05/ms-solver-foundation-data-binding.html" target=_blank mce_href="http://yetanothermathprogrammingconsultant.blogspot.com/2009/05/ms-solver-foundation-data-binding.html"&gt;encountered some problems trying to do two-way data binding&lt;/A&gt; using DataTable objects.&amp;nbsp; There are more details on &lt;A href="http://code.msdn.microsoft.com/solverfoundation/Thread/View.aspx?ThreadId=1812" target=_blank mce_href="http://code.msdn.microsoft.com/solverfoundation/Thread/View.aspx?ThreadId=1812"&gt;this discussion thread&lt;/A&gt;.&amp;nbsp; Ross, a member of the Solver Foundation team, was kind enough to code up a workaround for Erwin's example.&amp;nbsp; In addition to this CS file, you will need to create a new DBML file called SampleDataContext, &lt;A href="http://blogs.msdn.com/natbr/archive/2009/05/05/creating-parameterized-solver-foundation-models-using-linq-to-sql.aspx" mce_href="http://blogs.msdn.com/natbr/archive/2009/05/05/creating-parameterized-solver-foundation-models-using-linq-to-sql.aspx"&gt;as I described in a previous post&lt;/A&gt;.&lt;/P&gt;&lt;PRE&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.OleDb;
using System.Data.Linq;
using System.Text;
using Microsoft.SolverFoundation.Services;
using System.IO;
 
namespace OML1
{
    class Test
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Solve();
        }
 
        // Holds the OML model
        string strModel = @"Model[
              Parameters[Sets,I],
              Parameters[Reals,p[I]],
 
              Decisions[Reals[0,Infinity],x[I]],
 
              Constraints[
                 Foreach[{i,I}, x[i]==p[i]]
              ]
           ]";
 
 
        //  SFS
        SolverContext context;
        SampleDataContext data;
 
 
        //  Constructor
        public Test()
        {
            context = SolverContext.GetContext();
            data = new SampleDataContext("Data Source=Sql_server_name;Initial Catalog=DataPartitionAllocation20_5;Integrated Security=True");
            context.DataSource = data;
        }
 
 
        // Solve the problem
        public void Solve()
        {
            context.LoadModel(FileFormat.OML, new StringReader(strModel));
 
            Parameter p = context.CurrentModel.Parameters.First(q =&amp;gt; q.Name == "p");
            p.SetBinding(data.P, "value", new string[] { "index" });
 
            Decision x = context.CurrentModel.Decisions.First(d =&amp;gt; d.Name == "x");
            x.SetBinding(data.X, "value", new string[] { "index" }); 
 
            Solution solution = context.Solve();
            Console.Write("{0}", solution.GetReport());
 
            context.PropagateDecisions();
 
        }
 
    }
 
}
&lt;/PRE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9685593" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/natbr/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/natbr/archive/tags/Solver+Foundation/default.aspx">Solver Foundation</category><category domain="http://blogs.msdn.com/natbr/archive/tags/optimization/default.aspx">optimization</category><category domain="http://blogs.msdn.com/natbr/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/natbr/archive/tags/OML/default.aspx">OML</category><category domain="http://blogs.msdn.com/natbr/archive/tags/SQL/default.aspx">SQL</category></item><item><title>Creating parameterized Solver Foundation models using LINQ to SQL</title><link>http://blogs.msdn.com/natbr/archive/2009/05/05/creating-parameterized-solver-foundation-models-using-linq-to-sql.aspx</link><pubDate>Tue, 05 May 2009 23:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9589626</guid><dc:creator>Nathan Brixius</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/natbr/comments/9589626.aspx</comments><wfw:commentRss>http://blogs.msdn.com/natbr/commentrss.aspx?PostID=9589626</wfw:commentRss><wfw:comment>http://blogs.msdn.com/natbr/rsscomments.aspx?PostID=9589626</wfw:comment><description>&lt;P&gt;&lt;A href="http://code.msdn.microsoft.com/solverfoundation/Thread/View.aspx?ThreadId=1665" mce_href="http://code.msdn.microsoft.com/solverfoundation/Thread/View.aspx?ThreadId=1665"&gt;On the Solver Foundation MSDN forum&lt;/A&gt; there was a question about how to read model data from a DB and use it within a Solver Foundation model.&amp;nbsp; In this post I will extend my production planning sample to use LINQ to SQL.&amp;nbsp; To follow along at home you will need to have a recent version of SQL Server installed locally, and some basic knowledge of how to create SQL tables.&amp;nbsp; You should also have compiled and run the code from my &lt;A href="http://blogs.msdn.com/natbr/archive/2009/04/24/modeling-a-production-planning-problem-using-solver-foundation.aspx" mce_href="http://blogs.msdn.com/natbr/archive/2009/04/24/modeling-a-production-planning-problem-using-solver-foundation.aspx"&gt;previous post&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 1: Create and populate the DB&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first step is to create tables corresponding to the entities in my model.&amp;nbsp; I created a very simple DB with three tables: Countries, Products, and Yields.&amp;nbsp; The Yields table has foreign key constraints to the Countries and Products tables.&amp;nbsp; Here is a diagram:&lt;/P&gt;
&lt;P&gt;&lt;IMG title="Petrochem Entities" alt="Petrochem Entities" src="http://blogs.msdn.com/photos/nathan_brixius/images/9589627/original.aspx" mce_src="http://blogs.msdn.com/photos/nathan_brixius/images/9589627/original.aspx"&gt;&lt;/P&gt;
&lt;P&gt;To populate the DB I just wrote a script that inserts my problem data, and ran itin SQL Management Studio.&amp;nbsp; Here's the script (and forgive my SQL):&lt;/P&gt;&lt;PRE&gt;GO 

DELETE FROM Yields
DELETE FROM Products
DELETE FROM Countries
GO

INSERT INTO Countries (Id, Name, Limit, Cost)
VALUES (0, 'SA', 9000, 20)

INSERT INTO Countries (Id, Name, Limit, Cost)
VALUES (1, 'VZ', 6000, 15)

GO

INSERT INTO Products (Id, Name, Demand)
VALUES (0, 'Gas', 1900)

INSERT INTO Products (Id, Name, Demand)
VALUES (1, 'Jet Fuel', 1500)

INSERT INTO Products (Id, Name, Demand)
VALUES (2, 'Lubricant', 500)

GO

INSERT INTO Yields (CountryId, ProductId, Value)
VALUES (0, 0, 0.3)
INSERT INTO Yields (CountryId, ProductId, Value)
VALUES (1, 0, 0.4)

INSERT INTO Yields (CountryId, ProductId, Value)
VALUES (0, 1, 0.4)
INSERT INTO Yields (CountryId, ProductId, Value)
VALUES (1, 1, 0.2)

INSERT INTO Yields (CountryId, ProductId, Value)
VALUES (0, 2, 0.2)
INSERT INTO Yields (CountryId, ProductId, Value)
VALUES (1, 2, 0.3)
&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 2: Create Entity and DataContext classes in Visual Studio&lt;/STRONG&gt; &lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" mce_href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx"&gt;Scott Guthrie's blog&lt;/A&gt; (and the MSDN docs) show you exactly how to do this:&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Add a new "Linq to SQL file" to your project called Petrochem.dbml.&lt;/LI&gt;
&lt;LI&gt;Bring up the Server Explorer window, connect to your database and drag the tables into the dbml window.&amp;nbsp; Visual Studio will automatically create a Datacontext class (mine is called PetrochemDataContext) and an entity class for each table that you include.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;IMG style="WIDTH: 257px; HEIGHT: 180px" title="Petrochem DB" alt="Petrochem DB" src="http://blogs.msdn.com/photos/nathan_brixius/images/9589628/original.aspx" width=257 height=180 mce_src="http://blogs.msdn.com/photos/nathan_brixius/images/9589628/original.aspx"&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Step 3: Modify Solver Foundation Services data binding code&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;This is in fact very easy because Solver Foundation Services was designed to work well with LINQ.&amp;nbsp; Take the PetrochemDataBinding sample from last time, and change the SetBinding statements to work with the PetrochemDataContext class instead of a hardcoded DataSet.&amp;nbsp; The code is almost identical:&lt;/P&gt;&lt;PRE&gt;  private static void PetrochemLinqDataBinding() {
      SolverContext context = SolverContext.GetContext();
      context.ClearModel();
      Model model = context.CreateModel();

      PetrochemDataContext db = new PetrochemDataContext();

      Set products = new Set(Domain.Any, "products");
      Set countries = new Set(Domain.Any, "countries");

      Parameter demand = new Parameter(Domain.Real, "demand", products);
      demand.SetBinding(db.Products, "Demand", "Id");

      Parameter yield = new Parameter(Domain.Real, "yield", products, countries);
      yield.SetBinding(db.Yields, "Value", "ProductId", "CountryId");

      Parameter limit = new Parameter(Domain.Real, "limit", countries);
      limit.SetBinding(db.Countries, "Limit", "Id");

      Parameter cost = new Parameter(Domain.Real, "cost", countries);
      cost.SetBinding(db.Countries, "Cost", "Id");

      model.AddParameters(demand, yield, limit, cost);

      Decision produce = new Decision(Domain.RealNonnegative, "produce", countries);
      model.AddDecision(produce);

      model.AddGoal("goal", GoalKind.Minimize, Model.Sum(Model.ForEach(countries, c =&amp;gt; cost[c] * produce[c])));

      model.AddConstraint("Demand",
        Model.ForEach(products, p =&amp;gt; Model.Sum(Model.ForEach(countries, c =&amp;gt; yield[p, c] * produce[c])) &amp;gt;= demand[p])
        );

      model.AddConstraint("Production limit",
        Model.ForEach(countries, c =&amp;gt; produce[c] &amp;lt;= limit[c])
        );

      Solution solution = context.Solve(new SimplexDirective());
      Report report = solution.GetReport();
      Console.WriteLine(report);
    }
&lt;/PRE&gt;
&lt;P&gt;That's all there is to it!&amp;nbsp; Note that instead of passing the entire collection (e.g. db.Countries) you could easily use LINQ statements or stored procedures, or whatever you like.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9589626" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/natbr/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/natbr/archive/tags/Solver+Foundation/default.aspx">Solver Foundation</category><category domain="http://blogs.msdn.com/natbr/archive/tags/optimization/default.aspx">optimization</category><category domain="http://blogs.msdn.com/natbr/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/natbr/archive/tags/OML/default.aspx">OML</category><category domain="http://blogs.msdn.com/natbr/archive/tags/SQL/default.aspx">SQL</category></item></channel></rss>