Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 748607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:20:23+00:00 2026-05-14T14:20:23+00:00

Is it possible to execute a SQL (TSQL or Linq To SQL) from within

  • 0

Is it possible to execute a SQL (TSQL or Linq To SQL) from within a T4 Template so that the schema information from a table can be used to customized the code generation?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:20:23+00:00Added an answer on May 14, 2026 at 2:20 pm

    Yes of course – you can execute any arbitrary valid .NET code in a T4 template – that template gets converted into a .NET class, after all, which then gets compiled and executed.

    What’s your issue? What have you tried to do and how far have you come? Where are the roadblocks??

    UPDATE:
    I tried the following:

    • I put a Linq-to-SQL model into a class library called AdventureWorksData
    • I then created a new project (console app)
    • I referenced that AdventureWorksData assembly

    With this setup in place, I can write a T4 template something like this:

    <#@ Template Language="C#v3.5" Debug="true" #>
    <#@ Output Extension=".cs" #>
    <#@ Assembly Name="System.Core.dll" #>
    <#@ Assembly Name="System.Data.dll" #>
    <#@ Assembly Name="System.Data.Linq.dll" #>
    <#@ Assembly Name="AdventureWorksData.dll" #>
    <#@ Import Namespace="System" #>
    <#@ Import Namespace="System.Data" #>
    <#@ Import Namespace="System.Data.Linq" #>
    <#@ Import Namespace="System.Linq" #>
    <#@ Import Namespace="AdventureWorksData" #>
    using System;
    using AdventureWorksData;
    
    namespace AdventureWorks.Products
    {
        public class ProductList
        {
    <#
          AdventureWorksDataContext ctx = new AdventureWorksDataContext();
    
          var result = from p in ctx.Products 
                       where p.ProductCategoryID == 5 
                       select p;
    
          foreach (Product product in result)
          { 
    #>
                public string <#= SanitizeName(product.Name) #> = "<#= product.ProductNumber #> / ID= <#= product.ProductID #>";
    <#
          }
    #>
        }
    }
    <#+
           internal string SanitizeName(string input)
           {
               return input.Replace(" ", "_").Replace("-", "_").Replace("/", "_").Replace(".", "_").Replace(",", "_");
           }
    #>
    

    What I do here is enumerate over the Products in the AdventureWorksLT sample database, and I grab all products with ProductCategoryID == 5, using Linq.

    I then create a class which holds strings where the name of the string variable corresponds to the (sanitized) name of the product itself, and the string value contains some of the bits of information on that product in the database.

    As you can see, you can fairly easily incorporate a Linq-to-SQL model and a LINQ query into your T4 template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to execute sql statements from a C program? I'm creating a
I have dynamic SQL stored in a SQL table that I have to execute
I am looking at whether it is possible to execute arbitrary SQL commands (dynamic
How is it possible to execute a direct SQL command to an ADO.NET connected
Using DBI::DatabaseHandle#execute or DBI::DatabaseHandle#prepare it's not possible to run an sql script (with mutiple
Is it possible to execute a specific sub in a vbs file from a
Is it possible to execute store procedure like a table for SELECT operator (MS
Is it possible to execute a statement like this in NHibernate? SELECT * FROM
Using T-SQL, I would like to execute an UPDATE statement that will SET columns
In T-SQL you can do following SELECT SUBSTRING(COLUMN1, 1, 3), * FROM TABLE1 But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.