Learn IT Courses from the Best Tutors
Search in
Hi,
I have asp.net project with sql connection.I want connect database with asp.net and run the project and web config file there
In web.config: <connectionStrings> <add name="ConnectionString" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com" providerName="System.Data.SqlClient" /> </connectionStrings>In Class.cs public static string ConnectionString{ get{ return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;} set{}
read lessPut this in Web configuration file as: <connectionStrings> <add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System. Data. SqlClient" /></connectionStrings>
read less<connectionStrings> <add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient" /> </connectionStrings>
read less'Connectionstring' is the variable to use the connection string in any class of the .net.
ip_address_of_server : represents the ip of the server
databasename : database name
username : name of the database user
password : password of the database associated with the user
Use belo code in web.config file
<appSettings>
<add key="connectionstring" value="server=ip_address_of_server;database=databasename;uid=username;pwd=password"/>
</appSettings>
namespace DemoApplication { public partial class Demo System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string connetionString; SqlConnection cnn; connetionString = @"Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb ;User ID=sa;Password=demol23"; cnn = new SqlConnection(connetionString); cnn.Open(); Response.Write("Connection MAde"); conn.Close(); } } }read less
After opening the web.config file in application, add sample db connection in connectionStrings section like this:
Declaring connectionStrings in web.config file:
Now, write the code to get the connection string from web.config file in our codebehind file. Add the following namespace in codebehind file.
using System.Configuration;
This namespace is used to get configuration section details from web.config file.
C# code
View 48 more Answers
Related Questions
Use of INDEX in MS Excel?
I am working as Tech support executive with windows and Linux total of 4 years of experience , Not getting enough calls / opportunities ... Which domain is good for change Sap basis consultant / any other suggestions are welcome
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Make a Career in Mobile Application Programming
Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...
Make a Career as a BPO Professional
Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...
Top 5 Skills Every Software Developer Must have
Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today. In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...
Why Should you Become an IT Consultant
Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...
Looking for IT Courses ?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for IT Courses Classes are on UrbanPro
The best Tutors for IT Courses Classes are on UrbanPro