Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi all

I can't able to connect to my sql2005 database, please any provide me a link or code how to connect

Thanks in adavance

Asgar
Posted
Updated 6-Dec-10 1:55am
v3
Comments
thatraja 6-Dec-10 2:52am    
Always make your question clear. Your Tag says "SQL2005" but you mentioned "my sql" in your question. Here after take some more time & complete your question. Happy coding.
Toli Cuturicu 6-Dec-10 3:26am    
Here "my" is the pronoun, meaning that the sql database is "his" own.
Manfred Rudolf Bihy 6-Dec-10 3:28am    
@thatraja: May be you should take some time read OP'S question. He wants to connect to his sql database. He wrote "my sql database" and not "MySQL".
Manfred Rudolf Bihy 6-Dec-10 3:29am    
@Toli: Looks like somebody else noticed too :)

Add this in web.config file,
<add name="sqlConn" connectionString="server='servername';Initial Catalog='dbname';user id='userid';password='password';Connection Timeout=0;"/>


Then in code use the below code,
public SqlConnection sqlCon;

sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings("sqlConn").ConnectionString);
 
Share this answer
 
v3
Comments
Toli Cuturicu 6-Dec-10 3:27am    
Have anyone told you anything about this being a web project?
asgar ali 6-Dec-10 6:13am    
thank u very much
Toniyo Jackson 6-Dec-10 6:15am    
If its working fine vote it.
In the .aspx file:
Include the system files as:


using System.data.sqlclient;


Sqlconnection con = new sqlconnection(ConfigurationManager.appsettings["ConnectionString"].ToSrting());


In the webconfig file:

XML
<appSettings>
    <add key="ConnectionString" value="Data Source=SMAAPC0104\SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True"/>

  </appSettings>
    <connectionStrings>
    <add name="ImageLoader" connectionString="Data Source=SMAAPC0104\SQLEXPRESS;Initial Cataolg=Demo"/>
    </connectionStrings>




Try it out.

Cheers
Gokul.N
 
Share this answer
 
Comments
asgar ali 6-Dec-10 6:13am    
Thank you man

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