Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 10, 2011 09:54 PM by chohmann
Member
106 Points
91 Posts
May 10, 2011 02:03 PM|LINK
Environment : Visual Studio 2005 Sqlserver Express
Pl. tell me how to connect and execute query with sql database in asp.net in both cases :
1. When connection is stored in web.config and
when the connection is not stored.
Pl. give me examples for that.
thanks
Contributor
2658 Points
745 Posts
May 10, 2011 03:10 PM|LINK
Hi
If connection is stored in web.cofig as below
<connectionStrings> <add name="connstrng" connectionString="Data Source=DataSourceName;Initial Catalog=db;Persist Security Info=True;User ID=user;Password=pwd" providerName="System.Data.SqlClient"/> </connectionStrings>
Access it from c# as below
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStrng"].ToString()))
If it is directly from code behind then as below
using (SqlConnection conn = new SqlConnection("Data Source=DatasourceName;Initial Catalog=dbname;Integrated Security = SSPI"))
Examples:
http://www.codeproject.com/KB/database/sql_in_csharp.aspx
http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx
Hope this helps.
May 10, 2011 03:14 PM|LINK
will it work using vb.net or some what different.
Pl. guide me.
Thanks
Star
9385 Points
1644 Posts
May 10, 2011 09:54 PM|LINK
Here's swapna's code converted to VB:
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnStrng").ToString()) 'Insert Your Code Here End Using
Using conn As New SqlConnection("Data Source=DatasourceName;Initial Catalog=dbname;Integrated Security = SSPI") 'Insert Your Code Here End Using
Neeraj Bhush...
Member
106 Points
91 Posts
sql data connection
May 10, 2011 02:03 PM|LINK
Environment : Visual Studio 2005
Sqlserver Express
Pl. tell me how to connect and execute query with sql database in asp.net in both cases :
1. When connection is stored in web.config and
when the connection is not stored.
Pl. give me examples for that.
thanks
swapna.anu
Contributor
2658 Points
745 Posts
Re: sql data connection
May 10, 2011 03:10 PM|LINK
Hi
If connection is stored in web.cofig as below
<connectionStrings> <add name="connstrng" connectionString="Data Source=DataSourceName;Initial Catalog=db;Persist Security Info=True;User ID=user;Password=pwd" providerName="System.Data.SqlClient"/> </connectionStrings>Access it from c# as below
If it is directly from code behind then as below
using (SqlConnection conn = new SqlConnection("Data Source=DatasourceName;Initial Catalog=dbname;Integrated Security = SSPI"))Examples:
http://www.codeproject.com/KB/database/sql_in_csharp.aspx
http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx
Hope this helps.
Neeraj Bhush...
Member
106 Points
91 Posts
Re: sql data connection
May 10, 2011 03:14 PM|LINK
will it work using vb.net or some what different.
Pl. guide me.
Thanks
chohmann
Star
9385 Points
1644 Posts
Re: sql data connection
May 10, 2011 09:54 PM|LINK
Here's swapna's code converted to VB:
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnStrng").ToString()) 'Insert Your Code Here End UsingUsing conn As New SqlConnection("Data Source=DatasourceName;Initial Catalog=dbname;Integrated Security = SSPI") 'Insert Your Code Here End Using