using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Data.SqlClient; using System.Text; using System.Windows.Forms;namespace ERP_FZ { public partial class Form1 : Form { DataClass.Class1 ID_Name = new ERP_FZ.DataClass.Class1(); public Form1() { InitializeComponent(); } SqlConnection conn;
private void button3_Click(object sender, EventArgs e) { if (textBox1.Text.Trim () == "" || textBox2.Text.Trim () == "") { MessageBox.Show("绋娣彐桢犱稿负绌猴?); textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus(); } else { try { if (conn.State == ConnectionState.Open) //澶姝姣濡驽兼版? { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select count(*) from data001 where id='" + textBox1.Text.Trim() + "'and pw='" + textBox2.Text.Trim() + "' "; cmd.CommandType = CommandType.Text; int i = Convert.ToInt32(cmd.ExecuteScalar()); if (i > 0) { DataClass.Class1.ID_Name = textBox1.Text.Trim(); conn.Close(); this.Hide(); Form2 frm2 = new Form2(); frm2.Show();
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Data.SqlClient; using System.Text; using System.Windows.Forms;namespace ERP_FZ { public partial class Form1 : Form { DataClass.Class1 ID_Name = new ERP_FZ.DataClass.Class1(); public Form1() { InitializeComponent(); } SqlConnection conn;
private void button3_Click(object sender, EventArgs e) { if (textBox1.Text.Trim () == "" || textBox2.Text.Trim () == "") { MessageBox.Show("绋娣彐桢犱稿负绌猴?); textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus(); } else { try { if (conn.State == ConnectionState.Open) //澶姝姣濡驽兼版? { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select count(*) from data001 where id='" + textBox1.Text.Trim() + "'and pw='" + textBox2.Text.Trim() + "' "; cmd.CommandType = CommandType.Text; int i = Convert.ToInt32(cmd.ExecuteScalar()); if (i > 0) { DataClass.Class1.ID_Name = textBox1.Text.Trim(); conn.Close(); this.Hide(); Form2 frm2 = new Form2(); frm2.Show();
新建一个页面 拖几个控件,再在CS文件写操作,连接数据库方法不就好了?
总体思路就是从界面上的用户名+密码,作为数据库的查询条件,如果在数据库中找到相应记录,说明用户名+密码正确,就可以登录了。如果没有找到相应记录,就给出提示,不让登录就行了!
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;namespace ERP_FZ
{
public partial class Form1 : Form
{
DataClass.Class1 ID_Name = new ERP_FZ.DataClass.Class1();
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim () == "" || textBox2.Text.Trim () == "")
{
MessageBox.Show("绋娣彐桢犱稿负绌猴?);
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
else
{
try
{
if (conn.State == ConnectionState.Open) //澶姝姣濡驽兼版?
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select count(*) from data001 where id='" + textBox1.Text.Trim() + "'and pw='" + textBox2.Text.Trim() + "' ";
cmd.CommandType = CommandType.Text;
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
DataClass.Class1.ID_Name = textBox1.Text.Trim();
conn.Close();
this.Hide();
Form2 frm2 = new Form2();
frm2.Show();
}
else
{
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
MessageBox.Show("绋娣彐桢犳璇艰殒拌惧ワ?);
}
}
else
{
MessageBox.Show("缃缁楫棰瀵艰存版鸿挎ヤ告甯革?);
} } catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
textBox1.Focus();
conn = new SqlConnection("server=168.1.1.242;database=ERP2010;uid=sa;pwd=123456");
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} } private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
textBox2.Focus();
} private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
button3.Focus();
} private void button2_Click(object sender, EventArgs e)
{
conn.Close();
conn.Dispose();
Application.Exit();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
conn.Close();
Application.Exit();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;namespace ERP_FZ
{
public partial class Form1 : Form
{
DataClass.Class1 ID_Name = new ERP_FZ.DataClass.Class1();
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim () == "" || textBox2.Text.Trim () == "")
{
MessageBox.Show("绋娣彐桢犱稿负绌猴?);
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
else
{
try
{
if (conn.State == ConnectionState.Open) //澶姝姣濡驽兼版?
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select count(*) from data001 where id='" + textBox1.Text.Trim() + "'and pw='" + textBox2.Text.Trim() + "' ";
cmd.CommandType = CommandType.Text;
int i = Convert.ToInt32(cmd.ExecuteScalar());
if (i > 0)
{
DataClass.Class1.ID_Name = textBox1.Text.Trim();
conn.Close();
this.Hide();
Form2 frm2 = new Form2();
frm2.Show();
}
else
{
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
MessageBox.Show("绋娣彐桢犳璇艰殒拌惧ワ?);
}
}
else
{
MessageBox.Show("缃缁楫棰瀵艰存版鸿挎ヤ告甯革?);
} } catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
textBox1.Focus();
conn = new SqlConnection("server=168.1.1.242;database=ERP2010;uid=sa;pwd=123456");
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} } private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
textBox2.Focus();
} private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
button3.Focus();
} private void button2_Click(object sender, EventArgs e)
{
conn.Close();
conn.Dispose();
Application.Exit();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
conn.Close();
Application.Exit();
}
}
}
这可以
http://download.csdn.net/source/2418601
找不到类型或命名空间名称“DataClass”(是否缺少 using 指令或程序集引用?)
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Login
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
//退出按钮消息提示,是否确认退出
private void button2_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("你确定要退出?", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
result = MessageBox.Show("你真的确定要退出?", "退出提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
this.Close();
}
} }
//判断用户是否输入用户名密码,选择用户类型 private bool ValidateInput()
{
if (txtLoinId.Text.Trim() == "")
{
MessageBox.Show("请输入用户名");
txtLoinId.Focus();
return false;
}
else if (txtPassword.Text.Trim() == "")
{
MessageBox.Show("请输入密码");
txtPassword.Focus();
return false;
}
else if (cboType.Text.Trim() == "")
{
MessageBox.Show("请选择用户类型");
cboType.Focus();
return false;
}
else
{
return true;
} } //验证用户是否有效,--------------分方法做各个功能----------
//private bool ValidateUser()
//{
// //如果监控没有异常,则ValidateUser()的值为true;如果监控出现异常,那么ValidateUser()的值为false // bool isfind = false; //默认未找到用户
// try//只是执行一个监控的任务
// {
// //打开连接,链接数据库,好接收从数据库中传来的信息
// DBHelper.connection.Open(); // //SQL语句。将我们要对数据库操作的语句赋给一个字符串 // string sql = "";
// switch (cboType.Text)
// {
// case "学员":
// sql = string.Format("select count(*) from student where loginid ='{0}'and loginpwd ='{1}' ", txtLoinId.Text, txtPassword.Text);
// break;
// case "管理员":
// sql = string.Format("select count(*) from admin where loginid='{0}' and loginpwd ='{1}'", txtLoinId.Text, txtPassword.Text);
// break;
// default:
// sql = string.Format("select count(*) from teacher where loginid='{0}' and loginpwd ='{1}'", txtLoinId.Text, txtPassword.Text);
// break;
// }
// //命令执行。使用connection的对象和sql字符串
// SqlCommand cmd = new SqlCommand(sql, DBHelper.connection); // //执行操作
// int count = Convert.ToInt32(cmd.ExecuteScalar());
// if (count > 0)
// {
// isfind = true;
// string ff = cboType.Text + "数据库中有" + count + "条数据";
// MessageBox.Show(ff, "显示提示");
// }
// else
// {
// //MessageBox.Show("用户名和密码不存在,请重新输入!", "输入提示",MessageBoxButtons .OKCancel ,MessageBoxIcon.Information );
// // return false ;
// //消息框的返回值是一个dialogresult类型
// DialogResult result;
// result = MessageBox.Show("用户名和密码不存在,请重新输入!", "输入提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); // if (result == DialogResult.OK)
// { // result = MessageBox.Show("你选择了确定按钮,确定要离开吗!", "输入提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
// if (result == DialogResult.OK)
// { // DialogResult result1 = MessageBox.Show("你真的要离开吗?考虑一下吧", "");
// if (result == DialogResult.OK)
// { // MessageBox.Show("滚吧!");
// }
// else
// { // MessageBox.Show("你觉悟了!还不算晚!");
// }
// }
// else
// { // MessageBox.Show("真好,你觉悟了!"); // }
// }
// else
// { // MessageBox.Show("你选择了取消按钮", "输入提示");
// }
// }
// }
// catch (Exception ex)
// {
// //提示错误
// MessageBox.Show(ex.Message);
// }
// finally
// {
// //关闭连接
// DBHelper.connection.Close();
// }
// return isfind;
//} private void type()
{ switch (cboType.Text)
{
case "管理员":
AdminForm Admin = new AdminForm();
Admin.Show();
this.Visible = false;
break;
case "教员":
jiayuan jy = new jiayuan();
jy.Show();
break;
case "学员":
TianJiaXY xy = new TianJiaXY();
xy.Show();
break;
default:
MessageBox.Show("请检查输入类型", "输入提示");
break;
} } private void button1_Click(object sender, EventArgs e)
{
if (ValidateInput())
{
//先保存,后打开窗体
//if (ValidateUser())
//{
//将输入的用户名保存到静态变量中
Userhelper.loginId = txtLoinId.Text;
//将选择的登录类型保存到静态变量中
Userhelper.loginType = cboType.Text;
type(); //}
//else
//{
// return; //} }
} }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FileOpeat.DataSetLoginTableAdapters;namespace FileOpeat
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "MSN.ssk";
} private void button2_Click(object sender, EventArgs e)
{
this.txtname.Clear();
this.txtpwd.Clear();
} /// <summary>
/// 主要进行登录操作;
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if(txtname.Text==""||txtpwd.Text==""){ MessageBox.Show("请输入数据 ","提示",MessageBoxButtons.OK,MessageBoxIcon.Question); return;
}
tb_userinfoTableAdapter userinfo = new tb_userinfoTableAdapter();
FileOpeat.DataSetLogin.tb_userinfoDataTable usertalbe=userinfo.GetDataByGetName(txtname.Text.Trim());
if(usertalbe.Count<=0){ MessageBox.Show("用户名错误","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); return;
}
///用户名存在,进行下面的操作;获取行
for (int i = 0; i < usertalbe.Rows.Count;i++ )
{/// 获取行数 FileOpeat.DataSetLogin.tb_userinfoRow rowname = usertalbe[i];
string username = rowname.username;
string userpwd = rowname.userpwd;
if(username!=txtname.Text||userpwd!=txtpwd.Text){ MessageBox.Show("输入的数据有误","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
///数据正确,登录成功!
MainShow mainshow = new MainShow();
this.Hide();
mainshow.Show(); }
} private void txtname_TextChanged(object sender, EventArgs e)
{ }
}
}