hello all,

as i know that if we don't want to specify the static path of databse

then ,

we can use "Server.mapPath" like,( in any .aspx page )

str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("student1.mdf") + ";" + "Integrated Security=True;User Instance=True";

but my question is that ,

why this "Server.mapPath" can't work with in the any .class file

in c# ( and also vb.net )

like,

/// <summary>
/// Summary description for sql_data
/// </summary>
/// 
public class sql_data
{
    public SqlConnection conection = new SqlConnection();
    public SqlCommand sql1 = new SqlCommand();
       public string str;
	public sql_data()
	{       
        str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("student1.mdf") + ";" + "Integrated Security=True;User Instance=True";
                    this.conection = new SqlConnection(str); 
                     
	}
    }

because i want to make a class wich contain sql conection,sql command & adapter etc which can define in the class & can be used
in my entire web site

pls help me to solve this problem
i have search so many web site but i can't get proper web site

thanx in advance

Recommended Answers

All 5 Replies

Make a static variable with your connection string and set it from the windows form or ASP.NET web app when the code initializes and use that. You don't need to construct it every time.

Hi John,

Why do you want to construct the connection string dynamically in your class file?

Store the connection information in web.config file for web applications and app.config file for WinForms application. You can retrieve the conneciton information using System.Configuration.ConnectionStringSettings class in your class file.

For your question, You can use Server.MapPath in your class file using HttpContext object. Try System.Web.HttpContext.Current.Server.MapPath method.

It's really works !!

thnak u so much

n whold u pls give me any example (or any web site address ) how can use web.config file for connection

one agin thank u ...!!

Place your connection information in web.config. Assume that your database is in App_data folder of your of web site.

<connectionStrings>

<add name="TestConnectionString" connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|student1.mdf;Integrated Security=True;User Instance=True"  providerName="System.Data.SqlClient"/>
    </connectionStrings>

To use the connection string from web.config

Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
.
.
.

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);

    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(
            "SELECT * FROM Customers", connection);
    DataSet dsCustomers = new DataSet();
    adapter.Fill(dsCustomers);
   connection.Close();

It's working ......

thank u so much sir for ur reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.