I'm trying to solve this problem: I would like to associate an authentication page (login) to a database. I can't figure out how to use the connection string...
Practically, in the C # code behind the button created in ASP, I wrote the following code:
using (SqlConnection connection = new SqlConnection("server=(LocalDB); database=DBProva;"))
{
connection.Open();
}
I was suggested to configure the connection also in the Web.config, but nevertheless it gives me an error on the line Connection.Open ();
it was my old education code u should try maybe
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Uye_UyeLogin : System.Web.UI.Page
{
SqlConnection baglanti = new
SqlConnection(WebConfigurationManager.ConnectionStrings["LocalDbYemekSiparis"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
Label1.Text = "Kaydınız başarıyla gerçekleşmiştir.";
}
protected void Button1_Click(object sender, EventArgs e)
{
// Üye Girişi
string sql = "SELECT * FROM Tbl_Musteriler where TcKimlik=@tc and MusteriSifre=HASHBYTES('MD5',@Sif)";
SqlCommand komut = new SqlCommand();
komut.CommandText = sql;
komut.Connection = baglanti;
komut.Parameters.AddWithValue("@Tc", TextBox1.Text);
komut.Parameters.AddWithValue("@Sif", TextBox2.Text);
baglanti.Open();
SqlDataReader satir = komut.ExecuteReader();
if (satir.Read())
{
Session["KullaniciTipi"] = "Musteri";
Session["KullaniciId"] = (int)satir["MusteriId"];
Session["OturumTc"] = satir["TcKimlik"].ToString();
Session.Timeout = 120; // Oturum süresi dk
Response.Redirect("/Uye/UyeDefault.aspx");
}
else
{
Label2.Text = "Hatalı Giriş.";
}
}
}
Try declaring a Myconn string in your global.asax such as the following
public static string MyConn= ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
and when you need it in your Code just call
SqlConnection con = new SqlConnection(Global.MyConn);
and Most important don't forget to declare a connection string that corresponds your database in your Web.Config ex:
<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=(local);Initial Catalog=DBNAME;Integrated Security=True" />
</connectionStrings>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.