hi guys can you help me create a database login form of this code to windows form application? Because this code is made for console application. also I already know how to create a database but I do not know how I can make a windows login form for this code, can someone manipulate this for me plsss, It's quite confusing for me right now, Thanks to those who will help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;

namespace C_database
{
    class Program
    {
        static void Main(string[] args)
        {
            string conectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection connection = new SqlConnection(conectionstring);
            try
            {
                connection.Open();
                Console.WriteLine("Connection Open");
                Console.WriteLine("");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.Read();

            }
            Console.Write("Username> ");
            string username = Console.ReadLine();
            Console.Write("Password> ");
            string password = Console.ReadLine();
            SqlCommand command = new SqlCommand("SELECT * FROM [Table1] WHERE Username='" + username + "' AND Password='" + password + "'", connection);
            SqlDataReader reader = null;
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine("Welcome: " + reader["username"].ToString());
                Console.Read();
            }
            Console.WriteLine("User: " + username + "does not exist");
            Console.WriteLine("Press any key to continue");
            Console.Read();


        }
    }
}

You just need to use textboxes and buttons and execute the correct code when the button is clicked. For instance when the button is clicked, open your DB, query it and place the result in a textbox. Simple way.

okay, so there's no need for me to place this code in the main class? a windows form class without the main method will do?

Basically, yes. Drag two tetxboxes and a button onto your form and double click the button to open the button_click event in the code window. Add your code into that method and replace the two lines where you read in the user's input to instead access the text in the textboxes.

string username = textBox1.text; // if you haven't renamed it
string password = textBox2.text;

All other console lines can be removed or altered to output text to a label.

This article has been dead for over six months. Start a new discussion instead.