Class Kullanarak Veritabanına Bağlanma

Bir sınıf ekliyoruz ve şu kodları yazıyoruz.
public class Connection
    {
        public void Con() {
            SqlConnection baglan = new SqlConnection("Data Source=.;Initial Catalog=ders_programi;Integrated Security=True");
        }

        public SqlConnection con { get; set; }
    }

/*sonra Login.aspx.cs sayfamıza şu kodları yazıyoruz. Yani giriş buttonuna yazıyoruz*/


 public partial class Login : System.Web.UI.Page
    {
        Sqlbaglantisi baglan = new Sqlbaglantisi();
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
       // SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ders_programi;Integrated Security=true;");
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand cmdgiris = new SqlCommand("Select * from adminbilgileri where kullaniciadi='" + txtUserName.Text + "' and sifre='" +txtPassword.Text + "'", baglan.baglan());
                SqlDataReader dtgiris = cmdgiris.ExecuteReader();
                if (dtgiris.Read())
                {
                    Session["kullaniciadi"] = dtgiris["kullaniciadi"].ToString();
                    Response.Redirect("Islemler.aspx");
                }

            }
            catch (Exception ex)
            {

                lblWarning.Text = "Hatalı Giriş...";
            }


    }
 
        }

Yorumlar

Bu blogdaki popüler yayınlar

Histogram Eşitleme

Dinamik Label Ve Button Oluşturma