Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am a new programmer with C# programming. I don't know how to connect database Microsoft SQL server 2008 with Visual Studio 2010. I create an application as a window application. Please help me? I wonder that when connect with database finish so we have a file app.config, then we have to create a file to connect database more?

Thanks all,
Posted

go through the below links
http://www.dofactory.com/Connect/Connect.aspx[^]

for connection strings for all databases
http://connectionstrings.com/[^]
Thanks
--RA
 
Share this answer
 
app.config file with connection string like this

<configuration>
    <configsections>
    </configsections>
    <connectionstrings>
        <add name="ConnectionString">
            connectionString="Data Source=MyPC\MyDB;Initial Catalog=MyDB;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </add></connectionstrings>
</configuration>




Connecting to db
class DBConnection
    {
        #region Database connection method
        public SqlConnection Connect()
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
 
            try
            {
                conn.Open();
            }
            catch
            {
            }
            return conn;
        }
        #endregion
    }


Thanks

SP
 
Share this answer
 
For connectivity in visual studio
open Server Explorer
right click add Connection
Follow simple steps
After Creating Connection Right click on Database
Click on properties
In Proprties Connection String will be found.
Copy This String.


C#
private void button1_Click(object sender, EventArgs e)
       {
           string cmd="select *from Admin where ID='"+txtFilenO.Text+"' and Password='"+textBox1.Text+"' ";
          SqlConnection con=new SqlConnection();
 
           con.connectionString="Paste Connection String";
            con.Open();
SqlCommand cmd1 = new SqlCommand(cmd,con);
 
           SqlDataReader dr = cmd1.ExecuteReader();
           if (dr.Read())
           {
               AdminMenu n = new AdminMenu();
               n.lblsser.Text = dr[0].ToString();
               n.Show();
               this.Dispose();
               this.Close();
               cmd1.Connection.Close();
 
           }
           else
           {
               cmd1.Connection.Close();
               MessageBox.Show("ID/Password is not valid");
           }
 
       }

it is connection from wizard,But You should learn meaning of each keyword, So see
Connection Strings

if you want to put Connectiion string in app.config
C#
<appsettings>
    <add key="cnn" value="your connection string" />
  </appsettings>

and use as

C#
string str = System.Configuration.ConfigurationSettings.AppSettings["cnn"].ToString();
           OleDbConnection con = new OleDbConnection(str);
           con.Open();
 
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