I've recently found some very old server control code and need to up and running. The problem is I don't know how to connect it to the database. Below is the SQL connection:
SqlConnection sqlConnection = new SqlConnection(this.Page.Application["AuctionDbase"].ToString());
SqlCommand sqlCommand = new SqlCommand();
StringBuilder stringBuilder = new StringBuilder();
My question is do I need a web.config file where it can grab the connection string? I've placed the database on a local SQL server but I don't know if that's the right approach. I've never worked with Server Controls before and trying to get this deployed has been difficult!
Thank you in advance!!
Are you looking at the code snippet and trying to locate where the existing connection string is?
Judging by the code snippet, it doesn't look like it's in the web.config. If it was then i would expect a call to the ConfigurationManager, not Page.Application.
Search the code to see if "AuctionDbase" is being set anywhere.
It's possible that it's being set in the global.asax file if you have one.
Another possibility is in the Project options under Settings.
There are various ways of storing/retrieving connection details.
The connectionStrings element would be the most straightforward way to handle connection strings. The connectionStrings section is dedicated to connection strings and was introduced in .NET 2.0.
If the privacy of your connection credentials is of concern: you should use the connectionStrings section, as it can also be encrypted separately from any other settings.
In the Web.config file you can add:
<configuration>
...
<configSections>
<connectionStrings>
<add name="connectionStringName" connectionString="Data Source=(local);Initial Catalog=DBNAME;User Id=XXX;Password=XXX;" providerName="System.Data.SqlClient" />
</connectionStrings>
<configSections>
...
<configuration>
for more information on connection strings you can learn more here: https://www.connectionstrings.com/sql-server/
and in your code you can get the connection string like this:
string connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
Related
This s the connection String I am using in my local asp.net mvc site, I want to publish the website over a web hosting. They have SQL Instance of .\SQLEXPRESS12 and I have created a database name myapp_db as well. I have created a user with that db also. How can I modify the EF generated Connection String, and will it effect my auto generated models too ?
P.S. I know this question might not be StackOverflow's guidelines, but I would appreciate any help.
<connectionStrings>
<add name="DCHSurgical_DBContext" connectionString="metadata=res://*/Areas.ADMIN.Models.Model.csdl|res://*/Areas.ADMIN.Models.Model.ssdl|res://*/Areas.ADMIN.Models.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=myapp_db;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Just change this part of your connectionString like this:
< ... data source=Server's IP Address,Port;initial catalog=myapp_db;
user id=username that you have created;password=yourpassword;
MultipleActiveResultSets=True;App=EntityFramework""
... />
I am using entity framework model first approach in WCF.
The EF creates a connection stringwhich looks something like this:
automatically for the first time.
Now I want to change the connection string as database is located in other server.
How can I do it?
Connection string looks like this for now:
<connectionStrings><add name="Entities" connectionString="metadata=res://*/Database.Model1.csdl|res://*/Database.Model1.ssdl|res://*/Database.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=AB;password=admin;persist security info=True;user id=TEST"" providerName="System.Data.EntityClient" /></connectionStrings>)
Regards
Anudeep
One simple way is to modify the app.config or web.config file with the new connection details so calling the var context = new DataContext("NewServerEntities") constructor will use the named connection in your config file.
<add name="NewServerEntities" connectionString="metadata=res://*/Database.Model1.csdl|res://*/Database.Model1.ssdl|res://*/Database.Model1.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=NewServer;password=admin;persist security info=True;user id=TEST"" providerName="System.Data.EntityClient" />
Remove the connection string from the app.config file, re-run the entity Data Model wizard by right-clicking the designer and chose Update Model From Database, it will guide you to build a new connection. Or else directly edit the Datasource and credentials in the existing connection string in App.config.
I have hosted a website in my local machine IIS. The login page is loading fine, but after I enter the credentials, it is not not redirecting me to the homepage, it just stays on the same login page.
This is the connection string that I am using:
<add name="MyConn" connectionString="server=ajaymeda-pc;database=RoomExpenses;Trusted_Connection=true;"/>
</connectionStrings>
P.S: I have lost the source code and only have the compiled version.
First, ensure that the connection information is correct (server and database name). Then make sure that 'Remote Connections' are enabled in SQL.
Also, since it is .Net, you could grab a .Net decompiler (perform a Google search) that will allow you to decompile the DLLs and view the most of the source code. This is probably your last option, but would allow you to at least view what the page is doing and gives you a starting point if you had to redevelop the site.
use this as connection string if this is the issue as you mention:
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=RoomExpenses;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
UPDATE #1
you call this connection string inside your code like below:
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ToString()))
{
try
{}
catch
{}
}
I know I can create a connection string in the c# class itself, but I am trying to avoid doing that. I want to create the connection in the web.config, which I read is more secure. Nevertheless I couldn't find any example that has the following attributes specified:
Host name
Port
SID
Username
Password
Connection Name
Could anyone help please with creating this in webconfig? I am connecting to oracle DB.
Here is the template:
<connectionStrings>
<add name="{ConnectionName}"
connectionString="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;"
providerName="Oracle.DataAccess.Client"/>
</connectionStrings>
Here is one of mine - minus a real TNS name and username and password:
<add name="MSOL" connectionString="Data Source={TNS_NAME};User ID={username};Password={password};pooling=true;min pool size=5;Max Pool Size=60" providerName="Oracle.DataAccess.Client"/>
After adding the connection string to the web.config you can use the following:
System.Configuration.ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
to retrieve the connection string.
It may can help u....
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;
http://www.connectionstrings.com/oracle
You can investigate what the connection string should be like:
1) Create an empty text file in windows explorer and rename it to X.UDL
2) Double click on it and the datalink provider dialog will appear.
3) Select the provider tab. Find the provider for your data access method and click next.
4) Select your source
5) Test the connection and save it.
6) Compare the contents of X.UDL with your connections string.
I changed my project connection string through Code, as a result, Connection strings in all Config files changed. but problem is here that in datasets, some tables have different connection strings:
app.config connection string: "Datasource=USERPC; initial Catalog=MYDataBase; integrated security=False; User ID=sa; Password=user"
tablAdapter Connection string: "Datasource=USERPC; initial Catalog=MYDataBase; integrated security=False; User ID=sa"
what is my problem and How can i do solve it?
Go to Project Properties -> Settings Tab set there Connection String Value to application level instead of app.config. If you are using typed dataset the connectionstring defined by them itself so better do not alter them. For using conenctionstring in you code. Go directed as above. Then create a property or function that will access the value using Settings CLass.
In vb we use My.Settings.Default.ConnStringName
Are you sure, that your connection strings won't work. Cause all what is missing is the password and if i remember correctly if you fill in a password and read the connection string afterwards back you won't see the password anymore. But that's not a bug, it's a security feature.
To override this behavior you should take a look into the Remarks section of the SqlConnection