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 9017243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:13:07+00:00 2026-06-16T04:13:07+00:00

Is it possible for me to create a snippet and have it analyze a

  • 0

Is it possible for me to create a snippet and have it analyze a current class, get the properties of said class, and then create a sql function that writes out line by line each property in a command parameter.

What I am looking for is doing something like this:

public static int Add(MyObject Message) {
        MySqlConnection connection = new MySqlConnection(MySqlConnection);
        MySqlCommand command = new MySqlCommand("Add_Message", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@IMFromUserID", Message.IMFromUserID);
        command.Parameters.AddWithValue("@IMToUserID", Message.IMToUserID);
        command.Parameters.AddWithValue("@IMMessage", Message.IMMessage);
        command.Parameters.AddWithValue("@IMTimestamp", Message.IMTimestamp);
        connection.Open();
        MySqlDataReader reader = command.ExecuteReader();
        while (reader.Read()) {
            Message.IMID = (int)reader["IMID"];
        }
        command.Dispose();
        connection.Close();
        connection.Dispose();
        return Message.IMID;
    }

Basically I want the snippet to populate the entire Add function and fill in the @PropertyName and the Message.PropertyName in the command.Parameters.AddWithValue

  • 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-06-16T04:13:10+00:00Added an answer on June 16, 2026 at 4:13 am

    I don’t think code snippets are powerful enough. Maybe the ReSharper’s code templates are powerful enough but I don’t think so, too. You could look into using T4 templates if you really need or want code generation.

    Personally I would suggest to avoid compiletime code generation altogether. You could use reflection – easy but slow – or runtime code generation – complex but fast. If performance is not a primary concern I suggest to use reflection.

     public static Int32 Add<TMessage>(TMessage message)
         where TMessage: IMessageWithIMID
     {
        using (var connection = new MySqlConnection(connectionString))
        using (var command = new MySqlCommand("Add_Message", connection))
        {
            command.CommandType = CommandType.StoredProcedure;
    
            // We look only at public instance properties but you can easily
            // change this and even use a custom attribute to control which
            // properties to include.
            var properties = typeof(TObject).GetProperties(BindingFlags.Public |
                                                           BindingFlags.Instance);
    
            foreach (var property in properties)
            {
                var parameterName = "@" + property.Name;
    
                var value = property.GetValue(message, null);
    
                command.Parameters.AddWithValue(parameterName, value);
            }
    
            connection.Open();
    
            message.IMID = (Int32)command.ExecuteScalar();
    
            return message.IMID;
        }
    }
    

    Note that you have to introduce and implement the interface IMessageWithIMID in order to access the property IMID.

    internal interface IMessageWithIMID
    {
        Int32 IMID { get; set; }
    }
    

    Note that you also don’t need a data read – you can just use ExecuteScalar(). This turns

    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            message.IMID = (Int32)reader["IMID"];
        }
    }
    

    into

    message.IMID = (Int32)command.ExecuteScalar();
    

    and you are done.

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

Sidebar

Related Questions

Hi I would like to create a Visual Studio snippet that I can have
Is possible create an extension for SQL Management Studio in Visual Studio 2010? Visual
Possible Duplicate: Create an alert on any view controller after Facebook request:didFailWithError: I have
I have written this code (this is a snippet) that doesn't seem to be
I have an XSL that is meant to create a copy of an XML
Hi it'd like to know if it's at all possible create a parametric equalizer
Possible Duplicate: Create provisioning profile in iphone application i developed my iphone app and
Possible Duplicate: Create event handler for OnScroll for web browser control I would like
Possible Duplicate: Create an Array of the Last 30 Days Using PHP I am
Possible Duplicate: Create Excel file in Java How to save output in excel format

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.