Results 1 to 6 of 6

Thread: Connection with MS SQL

  1. #1
    Junior Member
    Join Date
    Jan 2009
    Answers
    1

    Connection with MS SQL

    How to connect with MS SQL using C# .Net


  2. #2
    Junior Member
    Join Date
    Jun 2008
    Answers
    1

    Re: Connection with MS SQL

    You use ADO.Net to achieve this.
    Typically you will create a Connection Object which is present in System.Data.SQLClient NameSpace.
    The main property that you need to specify for connection object is connection string.
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source = (local);Initial Catalog = master;Integrated Security = SSPI";
    con.Open();
    where
    Data Source = Server Name
    Initial Catalog = DataBase Name;


  3. #3
    Junior Member
    Join Date
    Jan 2007
    Answers
    1

    Re: Connection with MS SQL

    create a table with name strbuilder with the given field in the program.
    make this changes according to your configuration

    server = yourserver name;" + " integrated security = SSPI;" + " database = yourdatabasename


    using System;
    using System.Data;
    using System.Data.SqlClient;

    class ins
    {
    public static void Main( )
    {
    string source = " server = localhost;" + " integrated security = SSPI;" + " database = sandy" ;

    SqlConnection con = new SqlConnection( source);
    con.Open();
    SqlDataAdapter ad= new SqlDataAdapter ("select * from strbuilder",con);
    SqlCommandBuilder build =new SqlCommandBuilder (ad);
    DataSet ds = new DataSet( );
    ad.Fill(ds, "strbuilder");
    foreach ( DataTable d in ds.Tables)
    foreach (DataRow dr in d.Rows)
    {
    Console.WriteLine (" {0} {1}", dr["code"],dr["name"]);
    }
    Console.WriteLine("___________________________");

    DataRow row = ds.Tables["strbuilder"].NewRow();
    Console.WriteLine("enter the code");
    string co = Console.ReadLine();
    int cod = Int32.Parse(co);
    Console.WriteLine(" ");
    Console.WriteLine("enter the name");
    string nam = Console.ReadLine();

    row ["code"] = cod;
    row["name"] = nam;
    ds.Tables["strbuilder"].Rows.Add(row);
    ad.Update(ds,"strbuilder");

    Console.WriteLine("------------------------------------------------");
    foreach ( DataTable d in ds.Tables)
    foreach (DataRow dr in d.Rows)
    {
    Console.WriteLine (" {0} {1}", dr["code"],dr["name"]);
    }
    con.Close();
    }
    }

    still if you have doubt u can contact me


  4. #4
    Junior Member
    Join Date
    Jan 2009
    Answers
    1

    Re: Connection with MS SQL

    there are two environment in which there will be connection with ms sql and .net:
    1)connected mode
    2)disconnected mode.
    for disconnected mode there are following code can be used:
    first using namespaces as
    using system.data;
    using system.data.sqlclient;
    than..
    sqldataadapter da;
    dataset ds;
    da=new sqldataadapter("select *from<table1>";initial catalog=master;data source=.";UID=sa;pwd=sa");
    da=new dataset();
    da.fill(ds);


  5. #5
    Junior Member
    Join Date
    Feb 2009
    Answers
    4

    Re: Connection with MS SQL

    simply u pass the connection string in the program
    firstly u import the namespace system.data.sqlclient & the pass the connection string
    sqlconnection con= new sqlconnection("data source=server name;initial catalog=db name;integrated security=true or user id="";password=""")


  6. #6

    Re: Connection with MS SQL

    In web.config file the connection string has to be written
    <connectionStrings>
    <add name "name_of_string" connectionString="DataSource=server_name;uid=user_id;pwd=password;database=db_name" providerName="System.Data.SqlClient"/>
    </connectionStrings>


    after this in aspx.cs files you can open the connection using the code

    sqlconnection con = new sqlconnection(ConfigurationManager.connectionStrings["name_of_string"].connectionstring);


    The adwantage of this is whenever you change your db, user id or password then no need to change that in every pages. Instead simply change it in web.config file only once.


  7.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact