Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
(Form1.cs)
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
<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 1-Oct-12 18:16pm
Edited 1-Oct-12 18:18pm
JF201553.2K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
use this code:
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:
<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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\cargo.mdf;Integrated Security=True;User Instance=True");
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi
try this

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());
 
        }
    }
}
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 403
1 OriginalGriff 354
2 George Jonsson 202
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100