2 ضمیمه
توضیح در خصوص SqlConnection
سلام
دوستان من پس از یک مدت طولانی مجددا #c رو با نوشتن یک کد Login شروع کردم و با مشکل مواجه شدم .
using System.Data.SqlClient;
namespace LoginTrain4
{
public partial class Form1 : Form
{
private SqlConnection objconnection = new SqlConnection(@"Data Source=(local);Initial Catalog=TestNo1ForLogin");
private SqlCommand objcommand;
public Form1()
{
InitializeComponent();
}
private void BtnLogin_Click(object sender, EventArgs e)
{
int Var = 0;
try
{
if ((txtUsername.Text == string.Empty) || (txtPassword.Text == string.Empty))
{
MessageBox.Show("Enter Data !");
return;
}
objcommand = new SqlCommand("Select count(*) from table" +
"WHERE UserName=@UserName AND Password=@Password", objconnection);
objcommand.Parameters.AddWithValue("@UserName", txtUsername.Text.ToString());
objcommand.Parameters.AddWithValue("@Password", txtPassword.Text.ToString());
if (objconnection.State==ConnectionState.Closed)
{
objconnection.Open();
Var = (int)objcommand.ExecuteScalar();
}
objconnection.Close();
if (Var > 0 )
{
Form2 frm2 = new Form2();
frm2.Show();
}
else
MessageBox.Show("No Authentication !");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
Var = 0;
objconnection.Close();
}
}
}
}
با نوشتن این کد با این پیغام مواجه شدم :
ضمیمه 154584
حدس خودم این هست که مشکل اتصال به Database هست و اون قسمت باید اصلاح بشه!
ممنون میشم از دوستان راهنمایی کنید که با کمک شما شروع دوباره خوبی داشته باشم...
private SqlConnection objconnection = new SqlConnection(@"Data Source=(local);Initial Catalog=TestNo1ForLogin");
private SqlCommand objcommand;
ضمیمه 154585
نقل قول: توضیح در خصوص SqlConnection
نقل قول: توضیح در خصوص SqlConnection
نقل قول: توضیح در خصوص SqlConnection
using System.Data.SqlClient;
namespace LoginTrain4
{
public partial class Form1 : Form
{
private SqlConnection objconnection = new SqlConnection(@"Data Source=(local);Initial Catalog=TestNo1ForLogin");
private SqlCommand objcommand;
public Form1()
{
InitializeComponent();
}
private void BtnLogin_Click(object sender, EventArgs e)
{
int Var = 0;
try
{
if ((txtUsername.Text == string.Empty) || (txtPassword.Text == string.Empty))
{
MessageBox.Show("Enter Data !");
return;
}
// اتصال به دیتابیس در ابتدای دستور try باز شده است
objconnection.Open();
objcommand = new SqlCommand("Select count(*) from table " +
// فاصله بین کلمه "table" و "WHERE" در دستور Select ایجاد شده است
"WHERE UserName=@UserName AND Password=@Password", objconnection);
objcommand.Parameters.AddWithValue("@UserName", txtUsername.Text.ToString());
objcommand.Parameters.AddWithValue("@Password", txtPassword.Text.ToString());
// دستور ExecuteScalar در داخل شرط if حذف شده است
Var = (int)objcommand.ExecuteScalar();
objconnection.Close();
if (Var > 0 )
{
Form2 frm2 = new Form2();
frm2.Show();
}
else
MessageBox.Show("No Authentication !");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
// متغیر Var در این بخش حذف شده است
objconnection.Close();
}
}
}
}
نقل قول: توضیح در خصوص SqlConnection
البته به نظر من کد شما رو میشه کمی روانتر اجرا کرد .
using System.Data.SqlClient;
namespace LoginTrain4
{
public partial class Form1 : Form
{
private string connectionString = @"Data Source=(local);Initial Catalog=TestNo1ForLogin";
public Form1()
{
InitializeComponent();
}
private async void BtnLogin_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(txtUsername.Text) || string.IsNullOrWhiteSpace(txtPassword.Text))
{
MessageBox.Show("Enter Data !");
return;
}
int count = await CheckAuthenticationAsync(txtUsername.Text, txtPassword.Text);
if (count > 0)
{
Form2 frm2 = new Form2();
frm2.Show();
}
else
{
MessageBox.Show("No Authentication !");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async Task<int> CheckAuthenticationAsync(string username, string password)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
await connection.OpenAsync();
using (SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM table WHERE UserName=@UserName AND Password=@Password", connection))
{
command.Parameters.AddWithValue("@UserName", username);
command.Parameters.AddWithValue("@Password", password);
return (int)await command.ExecuteScalarAsync();
}
}
}
}
}