locked
app config connection string

    Question

  • i try to substitute the connection string in app.config into my code, how do i shorten the connection string? can i replace the add name with NorthwindEFCS ?

     

     

    <connectionStrings>

    <add name="LinqToSQL1.Properties.Settings.NorthwindEFConnectionString"

    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NorthwindEF.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

    providerName="System.Data.SqlClient" />

    </connectionStrings>

     

    string connectionString1 = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NorthwindEF.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;";

    using (SqlConnection connection = new SqlConnection(connectionString))

    {

    connection.Open();

    NorthWindDataContext db1 = new NorthWindDataContext(connection);

     

    var queryResult = from s in db1.Customers

    where s.CustomerID.StartsWith("A")

    select s;

    dataGridView1.DataSource = queryResult;

    connection.Close();

    }

     

     

    Thursday, August 07, 2008 6:12 AM

Answers

  •  learner08 wrote:

    i got the dataGridView working, but when i put it in listBox, it shows only 'LinqtoSQL1.Customer' , how do i display it in listBox1 ?  Besides, i would like to try out using sqladapter, dataset and sqlconnection, to display the record in datagridview instead of the linq.

     

    public void SimpleQuery()

    {

    NorthWindDataContext dc = new NorthWindDataContext();

    var q =

    from a in dc.GetTable<Customer>()

    select a;

    listBox1.DataSource = q;

    //dataGridView1.DataSource = q;

    }



    Hi learner08,

    You can use a ArrayList collection as the Listbox control’s data source.

    ArrayList a1 = new ArrayList();

                       

     

                        foreach (var t1 in q)

                        {

                            a1.Add(t1.FieldName);

                        }

                        listBox1.DataSource = a1;

    Regards,

    Xun


    Monday, August 11, 2008 5:32 AM

All replies

  • Hi learner08,


    Based on my understanding, you are trying to figure how to get the connect string from the “app.config” file.

    If that is case, well, perhaps you can take a look at the following code snippet, which demonstrates how to do that.


    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);       


    ConnectionStringSettings connstr = config.ConnectionStrings.ConnectionStrings["LinqToSQL1.Properties.Settings.NorthwindEFConnectionString"];


    String strConn = connstr.ConnectionString;

    MessageBox.Show(strConn);

     

    And here is a thread about this issue, maybe you can try to refer to it to get more information.

    http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/f93cd04c-d270-4b86-84fe-37a8cbcb4e55/

    Regards,

    Xun

    Monday, August 11, 2008 3:34 AM
  • I try to display the customer records in the datagridview but there's nothing there:

     

     

    Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

    ConnectionStringSettings connstr = config.ConnectionStrings.ConnectionStrings["sqlconnection.Properties.Settings.NorthwindEFConnectionString"];

    string strConn = connstr.ConnectionString;

    SqlConnection cn = new SqlConnection(strConn);

    cn.Open();

    DataSet ds = new DataSet();

    string sqlstring = "Select * from Customers";

    SqlDataAdapter sda = new SqlDataAdapter(sqlstring, cn);

    sda.Fill(ds);

    dataGridView1.DataSource = ds;

    cn.Close();

    Monday, August 11, 2008 4:06 AM
  • Monday, August 11, 2008 4:09 AM
  • i got the dataGridView working, but when i put it in listBox, it shows only 'LinqtoSQL1.Customer' , how do i display it in listBox1 ?  Besides, i would like to try out using sqladapter, dataset and sqlconnection, to display the record in datagridview instead of the linq.

     

    public void SimpleQuery()

    {

    NorthWindDataContext dc = new NorthWindDataContext();

    var q =

    from a in dc.GetTable<Customer>()

    select a;

    listBox1.DataSource = q;

    //dataGridView1.DataSource = q;

    }

    Monday, August 11, 2008 4:35 AM
  •  learner08 wrote:

    i got the dataGridView working, but when i put it in listBox, it shows only 'LinqtoSQL1.Customer' , how do i display it in listBox1 ?  Besides, i would like to try out using sqladapter, dataset and sqlconnection, to display the record in datagridview instead of the linq.

     

    public void SimpleQuery()

    {

    NorthWindDataContext dc = new NorthWindDataContext();

    var q =

    from a in dc.GetTable<Customer>()

    select a;

    listBox1.DataSource = q;

    //dataGridView1.DataSource = q;

    }



    Hi learner08,

    You can use a ArrayList collection as the Listbox control’s data source.

    ArrayList a1 = new ArrayList();

                       

     

                        foreach (var t1 in q)

                        {

                            a1.Add(t1.FieldName);

                        }

                        listBox1.DataSource = a1;

    Regards,

    Xun


    Monday, August 11, 2008 5:32 AM