Posts

Showing posts from October 13, 2014

How to connect to sql server to c# (.net ) using code

Image
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
    class query
    {
     public   SqlConnection con;

        SqlCommand cmd;
        public SqlDataReader sdr;

        public query()
        {
            con = new SqlConnection("Data Source=LAB1PC7\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");

        }

        public int modify(string s)
        {
            con.Open();
            cmd = new SqlCommand(s, con);
            int r = cmd.ExecuteNonQuery();
            con.Close();
            return r;
        }

        public SqlDataReader select(string s)
        {
            con.Open();
            cmd = new SqlCommand(s, con);
            sdr = cmd.ExecuteReader();
            //con.Close();
            return sdr;

        }

    }

}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; us…