Error ConfigurationManager.ConnectionStrings.ConnectionString

I am trying to extract data from a Microsoft Access database file to populate several text fields. (The text fields are all made in XAML.) I am pretty sure that something is missing because the database file is not accessed.

Here is my code:

DataTable tblVFWPostManagers = new DataTable(); string connString2 = ConfigurationManager.ConnectionStrings**/*["\\Documents\DatabaseFile.accdb"]*/**.ConnectionString; string query2 = @"SELECT Manager ID, Manager FName, Manager LName, Manager Address, Manager City, Manager State, Manager Zip Code, Manager Home Phone Number, Manager Cell Phone Number, Manager Email FROM tblVFWPostManagers"; //Fill the VFWPostManagers Set with the data using (SqlConnection conn2 = new SqlConnection(connString2)) { SqlDataAdapter da2 = new SqlDataAdapter(query2, conn2); da2.Fill(tblVFWPostManagers); } 

Note. I am sure the bold font is incorrect. However, I'm not quite sure what is going on in these brackets. At first I suggested that this is where the filepath path went. When I commented on this section, the error disappeared though.

How can I retrieve data from my database using the above method? What am I missing?

+4
source share
2 answers

A few errors in your code:

ConfigurationManager.ConnectionStrings refers to a specific configuration section of your application that stores data for accessing your databases (one or more). The section contains lines like these

  <connectionStrings> <add name="MyDataBase" connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\myFolder\myAccess2007file.accdb; Persist Security Info=False"/> </connectionStrings> 

(To create a valid connection string for your application, see www.connectionstrings.com )
Thus, your code refers to these section voices using the "name" key with

 string connString2 = ConfigurationManager.ConnectionStrings["MyDataBase"].ConnectionString; 

It is said that the text of your query will fail because you are using extensive column names with spaces. In this case, each column name must be enclosed in square brackets.

 string query2 = @"SELECT [Manager ID], [Manager FName], [Manager LName], ..... 
+6
source

Your app.config or web.config file has a ConnectionStrings section:

 <configuration> <connectionStrings> <add name="MyConnection" connectionString="..."/> </connectionStrings> ... </configuration> 

You can access it in your code:

 string connString2 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; 
+2
source

Source: https://habr.com/ru/post/1496901/


All Articles