Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
2.20/5 (4 votes)
See more:
How can I create a connection string in ASP.net?
How can I create a connection string in a class library? Which class and methods can I use and how must I call them?

Thanks in advance!
Posted
Updated 10-Feb-11 10:52am
v2
Comments
Dholakiya Ankit 13-Sep-13 2:48am    
u don't get a ans from this solutions?
[no name] 24-Apr-14 6:09am    
Search it of Google you will get thousands of solution

First, find out what your connection string will be

http://www.connectionstrings.com/[^]

Second, add it to your application configuration

XML
<connectionStrings>
  <add name="SomeConnectionString" connectionString="Whatever" providerName="System.Data.SqlClient " />
</connectionStrings>


Third, add a reference to System.Configuration in your project and you can read the data from the config file

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx[^]


string myConnectionString = ConfigurationManager.ConnectionStrings["WhateverName"].ConnectionString;
 
Share this answer
 
v2
Are these related?

I will assume not...

Connection string:
1) In VS, open the Server Explorer (Menu: "View...Server Explorer"
2) Right click the database your are interested in, and select "Properties"
3) The connection string is listed in the properties window.
4) Open your Web.Config and below the "appSettings" section add:
XML
<connectionStrings>
  <add name="MyDatabase" connectionString="...connection string as per the properties window"/>
</connectionStrings>


Objects and methods:
This is a bigger subject. I assume you have created your class library, and want to use items from them.
1) If you haven't done so, add a reference to the class library in you app.
2) Add a using if you want it.
3) Declare an object of the appropriate class type from your library, as if it was a .NET inbuilt type. for example if your class library defines a class "MyClass" then:
MyClass myInstanceOfMyClass = new MyClass();
myInstanceOfMyClass.MYMethod();
 
Share this answer
 
how do i create connection string in asp.net?

Whether it be ASP.NET or Winforms, forming connection string is same in all.

Look here for it: Connection String[^]


In class library how to create object and methode,how do i call them?

Create class. Objects are instances of the class. Methods are behaviours exposed in a class.

Lastly, now, based on your last few questions, I would suggest to get a beginner book and start of from there. Doing things randomly or asking anything would not help. Study, move stepwise and learn.
 
Share this answer
 
ConnectionStrings section in web.config.

Sql Authentication
XML
<connectionStrings>
  <add name="ConnectDBString" connectionString="server=SqlServerName;database=DatabaseName;uid=User;password=Password;" />
</connectionStrings>


Windows Authentication

XML
<connectionStrings>
   <add name="ConnectDBString" connectionString="server=SqlServerName;database=DatabaseName;Integrated Security=True" providerName="System.Data.SqlClient" />
 </connectionStrings>



To read the connection string into your code, use the ConfigurationSettings class.

string connStr = ConfigurationManager.ConnectionStrings["ConnectDBString"].ConnectionString;



For further information please go through the following links
1) ConnectionStrings
2) ConnectionStrings

If this would be really helpful to you then don't forgot to Vote and Make Answer as Accepted.
 
Share this answer
 
In addition to the other ideas, you can also dynamically build a connection string using the SqlConnectionStringBuilder class.
 
Share this answer
 
public class Class1
{

public static string connstring = ConfigurationSettings.AppSettings["Connection"].ToString();
SqlConnection objcon = new SqlConnection(connstring);
SqlCommand objcmd = new SqlCommand();

public bool Opencon()
{
try
{
if (objcon.State == ConnectionState.Closed)
{
objcon.Open();
}
objcmd.Connection = objcon;
return true;
}
catch (Exception ex) { throw new Exception("Error: In Open connesction"); return false; }
}

public bool Closecon()
{
try
{
if (objcon.State == ConnectionState.Open)
{
objcon.Close();
}
objcmd.Dispose();
return true;
}
catch (Exception ex) { throw new Exception("Error: In Close connesction"); return false; }
}


public static int ExecuteQuery(SqlCommand sqlcmd)
{
try
{
Class1 objdc = new Class1();
int affectedrecord = 0;
if (objdc.Opencon() == true)
{
sqlcmd.Connection = objdc.objcon;
affectedrecord = sqlcmd.ExecuteNonQuery();
objdc.Closecon();
objdc = null;
return affectedrecord;
}
else { return affectedrecord; }

}
catch (Exception ex) { throw ex;/* new Exception("Error: In ExecuteNonquery");*/ }

}

public static DataTable Generatedatatable(SqlCommand sqlcmd)
{
try
{
Class1 objdc = new Class1();

if (objdc.Opencon() == true)
{
sqlcmd.Connection = objdc.objcon;
SqlDataReader dr;
DataTable objdt = new DataTable();
dr = sqlcmd.ExecuteReader();
objdt.Load(dr);
objdc.Closecon();
objdc = null;
return objdt;
}
else { return null; }
}
catch (Exception Exception) { throw Exception /*new Exception("Error: In Generatedatatable")*/; }
}
public void RetrieveRecords(DataSet ds, SqlCommand cmd)
{
try
{
cmd.Connection = objcon;
SqlDataAdapter da = new SqlDataAdapter(cmd);
Opencon();
da.Fill(ds);

}
catch (Exception ex)
{
throw ex;
}
finally
{
Closecon();
}
}
public void FillGV(GridView gv, SqlCommand cmd)
{
try
{
DataSet ds = new DataSet();
Opencon();
cmd.Connection =objcon;
RetrieveRecords(ds, cmd);
gv.DataSource = ds;
gv.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Closecon();
}
}
 
Share this answer
 
public class Class1
{

public static string connstring = ConfigurationSettings.AppSettings["Connection"].ToString();
SqlConnection objcon = new SqlConnection(connstring);
SqlCommand objcmd = new SqlCommand();

public bool Opencon()
{
try
{
if (objcon.State == ConnectionState.Closed)
{
objcon.Open();
}
objcmd.Connection = objcon;
return true;
}
catch (Exception ex) { throw new Exception("Error: In Open connesction"); return false; }
}

public bool Closecon()
{
try
{
if (objcon.State == ConnectionState.Open)
{
objcon.Close();
}
objcmd.Dispose();
return true;
}
catch (Exception ex) { throw new Exception("Error: In Close connesction"); return false; }
}


public static int ExecuteQuery(SqlCommand sqlcmd)
{
try
{
Class1 objdc = new Class1();
int affectedrecord = 0;
if (objdc.Opencon() == true)
{
sqlcmd.Connection = objdc.objcon;
affectedrecord = sqlcmd.ExecuteNonQuery();
objdc.Closecon();
objdc = null;
return affectedrecord;
}
else { return affectedrecord; }

}
catch (Exception ex) { throw ex;/* new Exception("Error: In ExecuteNonquery");*/ }

}

public static DataTable Generatedatatable(SqlCommand sqlcmd)
{
try
{
Class1 objdc = new Class1();

if (objdc.Opencon() == true)
{
sqlcmd.Connection = objdc.objcon;
SqlDataReader dr;
DataTable objdt = new DataTable();
dr = sqlcmd.ExecuteReader();
objdt.Load(dr);
objdc.Closecon();
objdc = null;
return objdt;
}
else { return null; }
}
catch (Exception Exception) { throw Exception /*new Exception("Error: In Generatedatatable")*/; }
}
public void RetrieveRecords(DataSet ds, SqlCommand cmd)
{
try
{
cmd.Connection = objcon;
SqlDataAdapter da = new SqlDataAdapter(cmd);
Opencon();
da.Fill(ds);

}
catch (Exception ex)
{
throw ex;
}
finally
{
Closecon();
}
}
public void FillGV(GridView gv, SqlCommand cmd)
{
try
{
DataSet ds = new DataSet();
Opencon();
cmd.Connection =objcon;
RetrieveRecords(ds, cmd);
gv.DataSource = ds;
gv.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Closecon();
}
}
 
Share this answer
 
<configuration>
<configsections>

<connectionstrings>
<add name="StockSoftware.Properties.Settings.StockSoftwareConnectionString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PetPlastDB.accdb">
providerName="System.Data.OleDb" />

<appsettings>
<add key="petplast" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\PetPlastDB.accdb">


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900