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.
Related
I have a working SQL Server Express running in my computer as MYPC\SQLEXPRESS.
In my ASP.NET MVC 5 project I need to use this connection string in order to connect with it:
<add name="maymoneyDB"
connectionString="Server=MYPC\SQLEXPRESS;Initial Catalog=myDB;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
The problem is that my coworkers have their SQL Server instances at . so I need to change the connection string each time I pull changes from TFS.
How can I change my server path? I try with link but seems to change ##SERVERNAME but not the path.
Any explanation about handle this situation is also helpful (maybe change the path is not the best approach).
Thanks
did anybody host asp.net website with sql database?
I couldn't integrate my database connection with .mdf sql file (in my App_Data folder).
Is there anything need to change my data connection string (connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\example.mdf;Integrated Security=True;").
My host website: www.hostgator.com
It's really helpful if u give me proper suggestions step by step.
Database name is missing your Connection String :
Change as below:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\example.mdf;Initial Catalog=mydatabase;Integrated Security=True;"
your connection string (connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\example.mdf;Integrated Security=True;") is the one you use when running locally. change it to the connection string that your hosting site gave you. it is usually on the database information.
After initial setup has been done, test your connecting weather it is successful or not then
write your connecting string as below:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\example.mdf;Initial Catalog=yourDBname;Integrated Security=True;"
Where yourDBname is the name of your database that you have already given.
Hope this helps
Regards,
Rubel
I am new to create connection string and application configuration file. I use an example which showed connection to SQL Server CE using file which is what I do not want instead I want to connect to SQL Server 2008 Standard edition.
While exploring about connection string on several links like http://www.connectionstrings.com/sql-server/ i found that the Connection string uses the Property "Data Source = " and in some places it uses "Server=" which is quiet confusing.
Here is what I have in my application configuration file.
<connectionStrings>
<add name="ShareManagement"
connectionString="Data Source=localhost"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I want someone to tell me which properties need be used and what should be their respective values. (I am using default sa user as UserID and a password and using SQL Server authentication mode. My SQL Server database is installed on same machine/server on which my Visual Studio solution / application reside).
Ragards.
You can use either server= or Data Source= (those two are equivalent), and you can use either database= or Initial Catalog= (again: those are equivalent) - take your pick, use whatever you prefer.
But you just need to define at least
server,
database,
either Integrated Security=SSPI (for integrated, Windows authentication) or User id=abc;Password=xxxx for SQL Server authentication
You need at least these three pieces of information.
So if you want to use integrated security (Windows authentication), use this connection string:
<connectionStrings>
<add name="ShareManagement"
connectionString="server=(local);database=AdventureWorks;Integrated Security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
but if you want to use SQL Server authorization for a user John with a password secret, use this connection string:
<connectionStrings>
<add name="ShareManagement"
connectionString="server=(local);database=AdventureWorks;User ID=John;Password=secret;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Since I'm almost exclusively using these connection strings to connect to a standard relational database server, I personally prefer to use server=.... and database=...... - those just seem more natural, clearer and more intuitive to me. But again: you can also use those other key strings - they're 100% equivalent!
In my opinion, you should use the following connection string:
<add name="MyConnectionString" connectionString="Data Source=Server;Initial Catalog=DatabaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
Data Source should be as of the server name, and the initial catalogue will represent the database name you have with your sql server 2008 instance. To get logged in via the integrated security can be a better choice if you have not defined a user/password separately for that particular database except 'sa' account.
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'd like to know how to link my WCF application with a remote SQL Server database. By remote, I mean that is on the same network than me but not on the same computer/project.
I've the controll of the computer where the database is stored on.
What I've done so far : create my WCF application and try to add an ADO.NET connection. My issue : where to find the name of the server ? (and also : is it the good way to proceed ?).
Thanks !
where to find the name of the server?
Three options:
whoever "owns" the database server tells you the details, and you put them in a configuration file (or some other configuration system)
whoever "owns" the database server tells some key user the details, and the user puts them into a screen / api in the application
something like the above, but you try to discover sql servers at runtime via SqlDataSourceEnumerator (not a fan of this option, to be honest)
Conntion string should look like
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;
In Place of myServerName you can use IPAdress of machine
I suggest you add connectionString in the Web.config file of the application
<add name="connectionString"
connectionString="Data Source=ServerName/PC-Name;Initial Catalog=DatabaseName;User ID=userid;Password=pass"
providerName="System.Data.SqlClient" />
Use the connection string in your code/Logic
string conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;