chevron-left chevron-right

Piotr Nalepa Frontend, architecture and AI

[C#.NET][SQL] Formularz logowania w C#.NET

This version
blog.piotrnalepa.pl/2009/07/18/c-netsql-formularz-logowania-w-c-net
Published
Author
, Staff Software Engineer
Category
Other
Feedback
Write to me · Post on X

This post is 17 years old. Browsers and tools may have changed since then, so check the code against current documentation before you use it.

W dzisiejszym artykule przedstawię sposób na prosty formularz logowania w języku C#.NET i SQL. Język C#.NET został stworzony przez Microsoft i jest jedną z alternatyw dla PHP. Daje on naprawdę duże możliwości, a dorzucając do tego technologię SilverLight, to można stworzyć naprawdę efektowne strony, które tez będą efektywne w obsłudze.

Kod C# do obsługi formularza logowania wygląda następująco:

Example 1
private bool Porownaj(string string1, string string2)  
{  
	return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;  
}  

private void button1_Click(object sender, System.EventArgs e)  
{  
	try 
	{  
		SqlConnection UGIcon = new SqlConnection();  
		UGIcon.ConnectionString = "Server=.SQLEXPRESS; Database=Sample; User Id=user; password=haslo";  
		UGIcon.Open();  
	 
		SqlCommand cmd = new SqlCommand("SELECT ISNULL(stUsername, '') AS stUsername, ISNULL(stPassword,'') AS stPassword, ISNULL(stRole,'') AS stRole FROM LoginDetails WHERE stUsername='" + textBoxUsername.Text + "' and stPassword='" + textBoxPassword.Text + "'", UGIcon);  
	 
		SqlDataReader dr = cmd.ExecuteReader();  

		string userText = textBoxUsername.Text;  
		string passText = textBoxPassword.Text;  
		string stRole = "admin";  

		while (dr.Read())  
		{  
			if (this.Porownaj(dr["stUsername"].ToString(), userText) && this.Porownaj(dr["stPassword"].ToString(), passText) && this.Porownaj(dr["stRole"].ToString(), stRole))  
			{  
				MessageBox.Show("OK");  
			}  
			else 
			{  
				MessageBox.Show("Błąd");  
			}  
	 
		}  
		dr.Close();  
		UGIcon.Close();  
	 
	}  
	catch(Exception ex)  
	{  
		MessageBox.Show(ex.Message);  
	}  
}
To jest kod mechanizmu obsługującego stronę z formatkami logowania, gdzie przycisk wysyłania jest nazwany standardowo jako button1, pole gdzie należy wpisać nazwę użytkownika jako textBoxUsername, a pole do wpisania hasła jako textBoxPassword. Należy pamiętać, by ustanowić połączenie z bazą danych zastępując: Database=Sample; User Id=user; password=haslo - swoimi danymi do bazy.

Niestety nie posiadam serwera z obsługą ASP.NET i SQL, więc obejdzie się bez dema. Ten przykład można sprawdzić u siebie w domu na wirtualnym serwerze.

Share
Feedback

About the author

Staff Software Engineer

I've been writing this blog since 2009 and building for the web professionally for 14 years. Today I lead the frontend of the Cosmos platform at Bishop Fox, where I've been since day one of the project. Before that I worked at OLX, KUDO, Auto1 and eZ Systems. Here I write about what I've learned in practice: JavaScript, TypeScript, React, CSS and frontend architecture. I've also spoken at conferences across Poland and Europe. Got a question or an idea for a post? Let me know!