Here is a pattern to follow, place your data operations in a separate class and call in your form. Here Login
class could be whatever you want to call it with required properties.
public class Login
{
public int Id { get; set; }
. . .
}
Mocked data class, here since select *
is used rather than specifying only required columns I assume the first column is the primary key which it may or may not be so adjust as needed and add other columns/properties to the Login
class instance.
public class DataOperations
{
// caller validates parameters have values
public static Login DoSomething(string userName, string sifre)
{
Login login = new Login();
var selectStatement = "SELECT * FROM login WHERE username= @userName AND sifre = @sifre";
using (var cn = new SqlConnection("TODO"))
{
using (var cmd = new SqlCommand(selectStatement, cn))
{
cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = userName;
cmd.Parameters.Add("@sifre", SqlDbType.NVarChar).Value = sifre;
cn.Open();
var reader = cmd.ExecuteReader();
if (!reader.HasRows) return login;
reader.Read();
login.Id = reader.GetInt32(0);
}
}
return login;
}
}
Call the method above in your form
Login login = DataOperations.DoSomething("TODO", "TODO");
2
solved How to add SQL data into variable?