PDA

View Full Version : سوال: توضیح در خصوص SqlConnection



Milad_ATM
پنج شنبه 25 اسفند 1401, 07:58 صبح
سلام
دوستان من پس از یک مدت طولانی مجددا #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

Mahmoud.Afrad
دوشنبه 29 اسفند 1401, 02:47 صبح
https://f4n3x6c5.stackpathcdn.com/article/get-connectionstring-for-sql-server/Images/004.png


https://f4n3x6c5.stackpathcdn.com/article/get-connectionstring-for-sql-server/Images/005.png

ShayanFiroozi
جمعه 18 فروردین 1402, 00:49 صبح
https://www.connectionstrings.com/sql-server

پرستو پارسایی
جمعه 25 فروردین 1402, 23:13 عصر
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();
}
}
}
}

پرستو پارسایی
جمعه 25 فروردین 1402, 23:24 عصر
البته به نظر من کد شما رو میشه کمی روانتر اجرا کرد .
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();
}
}
}
}
}