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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:39:18+00:00 2026-05-20T06:39:18+00:00

I have a C# .net winforms application with two forms which connects to sql

  • 0

I have a C# .net winforms application with two forms which connects to sql express using connection string.

form1 connects to sql with trusted connection. connection string is given in the app.config file.

now, a button on form1 is clicked which changes the connection to application role credentials.

now, form2 opens & it has a button which when clicked needs to create a database using the application role settings.

so how do i use the application role settings created in form1 into form2. because to execute the database creation query the Form2 needs a new connection object.

So, do i have to add another app.config file or what else?

————————EDITED—————————————————-

public partial class Form1 : Form
{
    SqlConnection conn = new SqlConnection();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
             //new code for using conenction string from app.config file added 
                //  Read appSettings
                    string title = ConfigurationSettings.AppSettings["ApplicationTitle"];
                    string connectString =
                    ConfigurationSettings.AppSettings["ConnectionString"];
                    conn.ConnectionString = connectString;
           //new code ends

        conn.Open();
        string setapprole = "sp_setapprole 'my_app' , 'app_pass' ";
        SqlCommand cmd_app = new SqlCommand(setapprole, conn);
        SqlDataReader approle_reader = cmd_app.ExecuteReader();
        approle_reader.Close();

        Form2 f2 = new Form2();
        f2.Show();
    }

}

——————————–FORM2.CS——————————

public partial class Form2 : Form
{
   //how to connect to the database, 
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {  
        string query = "create database new_db";
         //I WANT TO USE the conn object here, 
          //& want the connection to use the application role
         // which was set in Form1.cs 
        SqlCommand cmd = new SqlCommand(query, conn); 
        SqlDataReader createdb = cmd.ExecuteReader();

    }
}

—————EDITED-1———————————-

my app.config file :

`xml version=”1.0″ encoding=”utf-8″

configuration

appSettings
add key=”ApplicationTitle” value=”Setup Database , Tables and Permissions”
add key=”ConnectionString”
value=”Server=localhost; Trusted_Connection=true”
appSettings

configuration

  • 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-20T06:39:19+00:00Added an answer on May 20, 2026 at 6:39 am

    Use a SqlConnectionStringBuilder to modify the connection parameters and then open a new connection to the DB.

    EDIT

    public partial class Form1 : Form
        {
            SqlConnection conn = new SqlConnection("Data Source=TODO;Initial Catalog=TODO;Integrated Security=True");
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                using (SqlCommand command = new SqlCommand("sp_setapprole 'my_app' , 'app_pass' ", conn))
                {
                    command.CommandType = CommandType.Text;
                    conn.Open();
                    command.ExecuteNonQuery();
                }
                // The application role is set and remains active until the user disconnects
    
                Form2 f2 = new Form2(conn);
                f2.Show();
            }
        }
    
        public partial class Form2 : Form
        {
            SqlConnection conn = null;
            //how to connect to the database, 
            public Form2(SqlConnection conn)
            {
                InitializeComponent();
                this.conn = conn;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    using (SqlCommand command = new SqlCommand("create database new_db", conn))
                    {
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }
                }
                finally
                {
                    // Important to close the DB connection (at which point the approle becomes inactive)
                    conn.Close();
                }
    
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .net (3.5) WinForms application and want to display some html on
I have a .net 3.5 WinForms application that runs fine on my machine, but
I have a .NET 2.0 WinForms application with a ToolStrip on my main form.
It seems like when you have a WinForms .NET application, and a ComboBox (set
I have a TreeView control in my WinForms .NET application that has multiple levels
I have small problem with my .net 2.0 winforms application. I want to embed
I have a .net winforms app which has a few animation effects, fade ins
I have a WinForms application (VS 2008, .NET 3.5) that has a form with
In a VB.NET Winforms application, I have a form that contains both a datagridview
I have a Winform application built with C# and .Net 2.0. I have a

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.