none
SqlMetal connection string to Application.Settings RRS feed

  • Question

  • I'm not sure if this is the correct forum.

    Is there a way to specify which app.config connection string to be used when generating dbml with SqlMetal?

    After the dbml is generated I have to open it in VS2010 and modify the Connection property to match the desired application setting.

    Thank you.

    Thursday, July 22, 2010 7:23 PM

Answers

  • Hi StefanDP,

     

    When you use SqlMetal to generate the .dbml file and add it to the project,  visual studio will use the code generator component  to

    generate the dbml.layout and designer.cs files, but the Default constructor for the DataContext will not be generated, as you said,

    when you set the connection property and save, VS would run the MsLinqToSQLGenerator to regenerate the .cs file, at this time

    the Default constructor for the DataContext is created with the connection string you just selected. This is the similar to dragging a table from the Server Explorer and drop it onto the .dbml to create the corresponding code in visual studio.  Typically, we use the auto generated code by SqlMetal.exe like this:

     

    Suppose you have a connection string in your app.config like :

    <connectionStrings>

        <add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"

        providerName="System.Data.SqlClient" />

    </connectionStrings>

     

    We can declare a string variable and use the ConfigurationManager to get the connection string from the configuration file.
     

      string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

     

    Instantiate an object of the DataContext class and write the LINQ query

     

         NorthWindDataContext db = new NorthWindDataContext(connStr);

       

         var results = from c in db.Customers  select c;

     

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by StefanDP Monday, July 26, 2010 3:16 PM
    Monday, July 26, 2010 5:50 AM
    Moderator

All replies

  • Hi StefanDP,

    You can specify the Connection Options (/conn:<connection string>) to the command line when execute SqlMetal.exe to generate the .dbml file. The /conn: option specifies database connection string.  For example, you can generate dbml by using a connection string in the command line like this:

      SqlMetal /conn:"server='myserver'; database='Northwind'" /dbml:C:\MyProject\Northwind.dbml

    For more information about the “/conn:<connection string>option , please refer to http://msdn.microsoft.com/en-us/library/bb386987.aspx

    If you have any other questions, please feel free to let me know.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 23, 2010 4:59 AM
    Moderator
  • I looked further into that. I was mislead by VS. When I add exisitng dbml file to a project it runs the MsLinqToSQLGenerator as a custom tool and generates the dbml.layout and designer.cs files. After that when I open the dbml in VS there is Connection property for it. I canselect one of the connection strings I defined in the app.config file and when I save it VS will add constructor with no parameters to the DataContext class. This constructor loads the connection string from the specified application settings. then I don't need to pass connection information when I use the DataContext.

    I was trying to pass the name of the connection string to SqlMetal so I don't have to do the extra step in VS.

    I haven't realised that when changing the Connection property in the dbml designer in VS on saving it VS would run the MsLinqToSQLGenerator to regenerate the .cs file. In any case if I regenerate the dbml file with SqlMetal I have to run the MsLinqToSqlGenerator either by right clicking on the dbml and 'Run Custom Tool' or changing something in the dbml designer and saving it.

    Friday, July 23, 2010 2:11 PM
  • Hi StefanDP,

     

    When you use SqlMetal to generate the .dbml file and add it to the project,  visual studio will use the code generator component  to

    generate the dbml.layout and designer.cs files, but the Default constructor for the DataContext will not be generated, as you said,

    when you set the connection property and save, VS would run the MsLinqToSQLGenerator to regenerate the .cs file, at this time

    the Default constructor for the DataContext is created with the connection string you just selected. This is the similar to dragging a table from the Server Explorer and drop it onto the .dbml to create the corresponding code in visual studio.  Typically, we use the auto generated code by SqlMetal.exe like this:

     

    Suppose you have a connection string in your app.config like :

    <connectionStrings>

        <add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"

        providerName="System.Data.SqlClient" />

    </connectionStrings>

     

    We can declare a string variable and use the ConfigurationManager to get the connection string from the configuration file.
     

      string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

     

    Instantiate an object of the DataContext class and write the LINQ query

     

         NorthWindDataContext db = new NorthWindDataContext(connStr);

       

         var results = from c in db.Customers  select c;

     

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by StefanDP Monday, July 26, 2010 3:16 PM
    Monday, July 26, 2010 5:50 AM
    Moderator