使用C#windows表单应用程序登录代码 [英] Login code using C# windows forms application
本文介绍了使用C#windows表单应用程序登录代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要c#中的登录按钮代码。我有2个字段的用户名和密码以及登录按钮和后端作为sql server我创建了一个表我的名字注册它有登录名和密码字段。一旦我点击登录按钮,它应该登录,如果用户数据是正确的,它不应该是错误。
任何人都可以帮忙写这些代码。
我尝试过的:
i尝试连接数据库以访问现有字段,即用户名和密码。
i need a code for login button in c#. I HAVE 2 fields username and password and login button and back end as sql server i have created a table my name register it has login and password fields. once i click on login button it should login if the user data is correct of not it should error.
can anyone help with this in writing the code.
What I have tried:
i have tried in connecting to the database to access the existing fields i.e., username and password.
推荐答案
使用System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Data.Sql;
使用System.Data.OleDb;
使用System.Data.SqlClient;
命名空间login_form
{
公共部分类Form1:表格
{
SqlConnection con = new SqlConnection();
public Form1()
{
SqlConnection con = new SqlConnection();
con.ConnectionString =数据源= KRISHNA-PC \\SQLEXPRESS;初始目录=学生;综合安全性=真;
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
// TODO:这行代码将数据加载到'sTUDENTDataSet.login'表中。您可以根据需要移动或删除它。
//this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);
SqlConnection con = new SqlConnection(Data Source = KRISHNA-PC\\SQLEXPRESS; Initial Catalog = STUDENT; Integrated Security = True);
con.Open();
{
}
}
private void button1_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString =数据源= KRISHNA-PC \\SQLEXPRESS;初始目录=学生;综合安全性=真;
con.Open();
string userid = textBox1.Text;
string password = textBox2.Text;
SqlCommand cmd = new SqlCommand(选择用户ID,登录时的密码,其中userid ='+ textBox1.Text +'和password ='+ text Box2.Text +',con) ;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count> 0)
{
MessageBox.Show(登录成功欢迎来到主页http:// krishnasinghprogramming.nlogspot.com);
System.Diagnostics.Process.Start(http://krishnasinghprogramming.blogspot.com);
}
其他
{
MessageBox.Show(无效登录请检查用户名和密码);
}
con.Close();
}
private void button2_Click(object sender,EventArgs e)
{
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace login_form
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
public Form1()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KRISHNA-PC\\SQLEXPRESS;Initial Catalog=STUDENT;Integrated Security=True";
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'sTUDENTDataSet.login' table. You can move, or remove it, as needed.
//this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);
SqlConnection con = new SqlConnection("Data Source=KRISHNA-PC\\SQLEXPRESS;Initial Catalog=STUDENT;Integrated Security=True");
con.Open();
{
}
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=KRISHNA-PC\\SQLEXPRESS;Initial Catalog=STUDENT;Integrated Security=True";
con.Open();
string userid = textBox1.Text;
string password = textBox2.Text;
SqlCommand cmd = new SqlCommand("select userid,password from login where userid='" + textBox1.Text + "'and password='" + text Box2.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Login sucess Welcome to Homepage http://krishnasinghprogramming.nlogspot.com");
System.Diagnostics.Process.Start("http://krishnasinghprogramming.blogspot.com");
}
else
{
MessageBox.Show("Invalid Login please check username and password");
}
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
这篇关于使用C#windows表单应用程序登录代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文