Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Phagu Mahato
More...


Forums » .NET » Windows »

To make the database connection globally


Posted Date: 21 Jun 2012      Posted By:: Maya     Member Level: Bronze    Member Rank: 3175     Points: 2   Responses: 7



hi,
In my program im using insert, update, and delete. for that im opening the connection and closing the connection everytime. its that any easy way to make the connection to open globally instead of opening for an single process.




Responses

#676773    Author: Somasekar N      Member Level: Gold      Member Rank: 305     Date: 21/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

add below code to your web.Config file

<connectionStrings>
<add name="sampleDBConnectionString" connectionString="Data Source=SQLEXPRESS;Initial Catalog=sampleDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>


you can make use of this connection string in your programs as


String connString = ConfigurationManager.ConnectionStrings["sampleDBConnectionString"].ConnectionString;

You can create object once in your program for your DB connection and Open or Close where ever you use connections.

Click here to get coupon codes...
Coupon Codes



 
#676781    Author: Ultimaterengan        Member Level: Gold      Member Rank: 9     Date: 21/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

Write your connection string in web configuration file.



Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!


http://renganathan1984.blogspot.com/



 
#676784    Author: Bijit      Member Level: Gold      Member Rank: 157     Date: 21/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Create separate class under App_Code



public static class DataBaseConnection
{

private string connectionString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;


public static SqlConnection GetConnection(string connectionString )
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(connectionString);
connection.Open();
}
catch (Exception ex)
{

if (connection != null)
{
connection.Dispose();
}
}
return connection;
}


public static SqlCommand GetCommand(this SqlConnection connection, string commandText, CommandType commandType)
{
SqlCommand command = connection.CreateCommand();
command.CommandTimeout = connection.ConnectionTimeout;
command.CommandType = commandType;
command.CommandText = commandText;
return command;
}


public static void AddParameter(this SqlCommand command, string parameterName, object parameterValue, SqlDbType parameterSqlType)
{
if (!parameterName.StartsWith("@"))
{
parameterName = "@" + parameterName;
}
command.Parameters.Add(parameterName, parameterSqlType);
command.Parameters[parameterName].Value = parameterValue;
}
}




Web Config File



<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrin







 
#676787    Author: Prasad kulkarni        Member Level: Diamond      Member Rank: 8     Date: 21/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

you can use Application variable for single connection architecture where we can keep connection object to application variable and can use in our website.
check of connection is already open then use the same object else you can create new connection object and store it to variable

Thanks
Koolprasd2003
[DotNetSpider MVM]



 
#676793    Author: Maya      Member Level: Bronze      Member Rank: 3175     Date: 22/Jun/2012   Rating: 2 out of 52 out of 5     Points: 1

THANKS A LOT


 
#676933    Author: Siva Prasad      Member Level: Gold      Member Rank: 61     Date: 22/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

Method 1 : Using Application Variable

public class Global : System.Web.HttpApplication
{

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["DBCon"] = @"Data Source=SIVA-PC\sqlexpress;Initial Catalog=ShivaDB;Integrated Security=True";
}


protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection(Application["DBCon"].ToString());
sqlcon.Open();
}

Method 2: Using Global Variable

public partial class WebForm1 : System.Web.UI.Page
{
public static string GlobalDBConString = @"Data Source=SIVA-PC\sqlexpress;Initial Catalog=ShivaDB;Integrated Security=True";

protected void Page_Load(object sender, EventArgs e)
{

}


public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection(WebForm1.GlobalDBConString);
sqlcon.Open();


Method 3: Using global method

public static SqlConnection DbConnect(SqlConnection sqlConn)
{
sqlConn.ConnectionString = @"Data Source=SIVA-PC\sqlexpress;Initial Catalog=ShivaDB;Integrated Security=True";
return sqlConn;
}


public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
WebForm1.DbConnect(con);
con.Open();



 
#677436    Author: Rajan Patekar      Member Level: Gold      Member Rank: 455     Date: 26/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi,
*Make the connecting global i.e. declare the string as you declare the global variable.

*Another way you can declare it in web.config file.

Regards,
Rajan Patekar



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Insert datetime into ms access database
Previous : Filling a datagrid problem with code in vb.net
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.