如何在sql server 2008的c#windows窗体中执行另一个sql命令的数据读取器执行sql命令 [英] how to execute a sql command within data reader execution of another sql command in c# windows forms with sql server 2008
问题描述
您好我的名字是vishal我想知道如何在sql server 2008的c#windows窗体中执行另一个sql命令(在while循环中)的数据读取器执行sql命令。
所以我有登录表单名为: frmLogin ,下面给出的是c#代码:
Hi my name is vishal i was wondering of how to execute a sql command within data reader execution of another sql command(in while loop) in c# windows forms with sql server 2008.
So i have login form named:frmLogin and given below is it's c# code:
namespace Mini_Project
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if ((txtPassword.Text == "password") && (txtUsername.Text.ToLower() == "admin"))
{
Module.AUser_ID=1;
MDIParent1 h = new MDIParent1();
h.Show();
this.Close();
}
else
{
string username = txtUsername.Text;
string password = txtPassword.Text;
bool validUser = ValidateUser(username, password);
if (validUser)
{
Module.User_ID = 1;
MDIParent1 m = new MDIParent1();
m.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid user name or password. Please try tomorow ", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUsername.Focus();
}
} private bool ValidateUser(string username, string password)
{
bool success = false;
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Task;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd = new SqlCommand("Select * from [dbo].[UserDetail2] where username='"+txtUsername.Text+"' and password='"+txtPassword.Text+"' and LoginAttempts< 3", conn);
SqlDataReader rd = cmd.ExecuteReader();
while(rd.Read())
{
success=true;
Module.User_ID = Convert.ToInt32(rd[0].ToString());
Module.UserName = rd[1].ToString();
}
rd.Close();
conn.Close();
return success;
}
}
}
上面的c#代码在某种程度上运行正常!
下面给出的是我班级的c#代码:模块
The above c# code works OK to some extent!
Given below is my c# code of my class named:Module
using System.Threading.Tasks;
namespace Mini_Project
{
class Module
{
public static int User_ID;
public static string UserName;
}
}
我想要的是因为我正在开发一个登录,使用户能够进入应用程序(迷你项目)LoginAttempts小于3( 2次尝试失败),如果该用户的LoginAttempts大于3,则阻止该用户进入应用程序( Mini Project )通过登录表单( frmLogin )。
所以我想在登录成功后执行sql命令。以下是sql命令:
What i want is since i am developing a login which enables a user to enter into application(Mini Project) with LoginAttempts less than 3(2 unsuccessful attempts) and if LoginAttempts of that user is greater than 3 then it blocks that user from entering into application(Mini Project) via login form(frmLogin).
So i want is execute a sql command after successful login.Given below is that sql command:
cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
cmd.ExecuteNonQuery();
我也想在登录失败后执行另一个sql命令。以下是给出的sql命令:
I also want to execute another sql command after unsuccessful login.Given below is that sql command:
cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
cmd.ExecuteNonQuery();
任何人都可以告诉我/指导我/帮助我如何在/内部功能中实现此功能(我的登录表单中的ValidateUser )( frmLogin )?或者是否有其他方法可以达到这个要求的结果那么请帮助我/指导我?!
任何人都可以告诉我必须在我的c#登录表单代码( frmLogin )中进行哪些修改才能达到我要求的结果!
任何人都可以帮助我!任何帮助/指导解决这个问题将不胜感激!
Can anyone tell me/guide me/help me on how to implement this function in/inside function(ValidateUser) in my login form(frmLogin)? Or is there another way to achieve this required result then Please help me/guide me?!
Can anyone tell me what modifications must i need to do in my c# code of login form(frmLogin) to achieve my required result!
Can anyone help me Please! Any help/guidance in solving of this problem would be greatly appreciated!
推荐答案
if (validUser)
{
cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=0 where username='" + txtUsername.Text + "'", conn);
cmd.ExecuteNonQuery();
Module.User_ID = 1;
MDIParent1 m = new MDIParent1();
m.Show();
this.Close();
}
else
else
{
cmd = new SqlCommand("Update [dbo].[UserDetail2] set LoginAttempts=LoginAttempts+1 where username='" + txtUsername.Text + "'", conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Invalid user name or password. Please try tomorow ", "Task", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUsername.Focus();
}
这篇关于如何在sql server 2008的c#windows窗体中执行另一个sql命令的数据读取器执行sql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!