Skip to content

Improve samples by building them #3725

@ErikEJ

Description

@ErikEJ

Is your feature request related to a problem? Please describe.

None of the samples in the docs/samples folder are built or compiled, leading to potentially unusable samples

Describe the solution you'd like

Add a project and solution to build these at least locally

One of the challenges is that most of the samples have this shape:

using System;
using System.Data;
// <Snippet1>
using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        OpenSqlConnection();
        Console.ReadLine();
    }

    private static void OpenSqlConnection()
    {
        string connectionString = GetConnectionString();

        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = connectionString;

            connection.Open();

            Console.WriteLine("State: {0}", connection.State);
            Console.WriteLine("ConnectionString: {0}",
                connection.ConnectionString);
        }
    }

    static private string GetConnectionString()
    {
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file.
        return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
            + "Integrated Security=true;";
    }
}
// </Snippet1>

Which of course causes clashes as Program is in the Global namespace

A possible solution would be to add namespaces to sample:

using System;
using System.Data;
// <Snippet1>
using Microsoft.Data.SqlClient;

namespace SqlConnection_ConnectionString;

class Program
{
    static void Main()
    {
        OpenSqlConnection();
        Console.ReadLine();
    }

    private static void OpenSqlConnection()
    {
        string connectionString = GetConnectionString();

        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = connectionString;

            connection.Open();

            Console.WriteLine("State: {0}", connection.State);
            Console.WriteLine("ConnectionString: {0}",
                connection.ConnectionString);
        }
    }

    static private string GetConnectionString()
    {
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file.
        return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
            + "Integrated Security=true;";
    }
}
// </Snippet1>

Additional context

let me know if you think this adds value and I can do it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Up-for-Grabs 🙌Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions