Showing posts with label Connection string. Show all posts
Showing posts with label Connection string. Show all posts

Monday, April 23, 2012

How to connecting to SQLserver from asp.net application

Title: How to create connection string in asp.net using C#.net

Description:
If you want to connect with SQLServer from asp.net application ,you can either use the OLEDB class as well as SQL Client classes.While connecting with OLEDB Connection to mention the target data source we specify the name of the provider which has be to be used.But when you use SQL connection or Oracle connection classes the connection string does not require the provider name to be specified.Here i will show an example to get the data from database using connection object

Example
Using system.Data.SqlClient
SqlConnection cn;
SqlCOmmand cmd;
SqlDataReader dr;
under page load
cn=New SqlConnection("user id=sa;password=123;Database=asp.net");
cmd=New SqlCommand("select*from students",cn);
cn.open();
dr=cmd.ExcuteReader();
if(dr.Read())
{
Response.Write(dr[0].Tostring());
}

Tuesday, January 31, 2012

application related settings in config file

Title: How to set connection string settings in app.config in c#.net

Description:
Here i will describe what are the settings are available in config files and what is the use of those settings .
In asp.net by default no values of current page or accessible to other pages .if we want to define application wide static settings then we can use app settings tag of web.config.This should be written under configuration tag.With above app settings when we run the application automatically id and month are loaded to retrieve the in code they have to use a .net assembly or name space called System.Configuration.

Configuration Manager(old configuration settings) or classes are that are retrieve value from configuration file.All applications that are related to data sources need connection string.It is very difficult to maintain connection string in programs and also to maintain consistent connection strings(connection pooling,asynchronous precessing etc)In .net version this is tag is added to write connection string in configuration level and refuse it for entire project .As per location are data source of project is relocated to connection strings.This connection string written under configuration directly

Codebehind:
string str=ConfigurationManager.ConnectionStrings["Con"].ToString();
SqlConnectioncn=new SqlConnection(str);
cn.open();
label.test="database connected";
Data source controls also recommended to save connection string in configuration file.We can use connection string and some other web.config settinggs in markup also.
< % $config settings % >
ex:-connectionString="< % $connectionString:pubs % >"

Bel