Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(Form1.cs)
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

app.config file details
HTML
<configuration>
    <configsections>
    </configsections>
    <connectionstrings>
        <add name="WindowsApplication3.Properties.Settings.Database1ConnectionString">
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </add></connectionstrings>
</configuration>

this is my commection string how to access this connection in windows form..
Posted
Updated 1-Oct-12 18:18pm
v2

Hi,

use this code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           var connectionString = ConfigurationManager.ConnectionStrings["WindowsApplication3.Properties.Settings.Database1ConnectionString"].ConnectionString;
           MessageBox.Show(connectionString);
        }
    }
}

And change your app.config file to the following:
HTML
<configuration>
  <connectionstrings>
    <add name="WindowsApplication3.Properties.Settings.Database1ConnectionString">
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True" 
    providerName="System.Data.SqlClient"/>
  </add></connectionstrings>
</configuration>

I tested it and it works for me.
 
Share this answer
 
v2
hi try this

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;//add this two namespace
using System.Web.Configuration;

using System.Windows.Forms;

namespace WindowsApplication3
{
    public partial class Form2 : Form
    {
 

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
         SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["WindowsApplication3.Properties.Settings.Database1ConnectionString"].ToString());

        }
    }
}
 
Share this answer
 
v3
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\cargo.mdf;Integrated Security=True;User Instance=True");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900