- 博客(0)
- 资源 (1)
空空如也
登录有数据库连接窗口
很简单!刚学的!有登录和注册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.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
SqlConnection conn = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try{
SqlCommand com = conn.CreateCommand();
string users = textBox1.Text;
string password = textBox2.Text;
com.CommandText = "select * from T_login where LOGIN_user='"+users+"'";
SqlDataReader reader = com.ExecuteReader();
if (reader.Read())
{
//用户名正确!
string dbpassword = reader.GetString(reader.GetOrdinal("LOGIN_password"));
if (password == dbpassword)
{
MessageBox.Show("登陆成功!");
}
else
{
MessageBox.Show("密码错误!");
}
}
else
{
MessageBox.Show("用户名错误!");
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
string connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
try
{
conn = new SqlConnection(connString);
conn.Open();
MessageBox.Show("成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
conn.Dispose();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
SqlCommand com = conn.CreateCommand();
string users = textBox1.Text;
string password = textBox2.Text;
com.CommandText = "select * from T_login";
SqlDataReader reader = com.ExecuteReader();
label3.Text = "id,用户名,密码\n";
while (reader.Read())
{
label3.Text = label3.Text +"\n"+ string.Format("{0},{1},{2}", reader[0], reader[1], reader[2]);
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
SqlCommand com = conn.CreateCommand();
string users = textBox1.Text;
string password = textBox2.Text;
com.CommandText = "select * from T_login where LOGIN_user='" + users + "'";
SqlDataReader reader = com.ExecuteReader();
if (reader.Read())
{
//用户名存在!
MessageBox.Show("用户名已经存在");
reader.Close();
}
else
{
reader.Close();
//可以注册
com.CommandText = "INSERT INTO T_login (LOGIN_user, LOGIN_password) VALUES ('"+users+"', '"+password+"')";
com.ExecuteNonQuery();
MessageBox.Show("注册成功!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
2011-12-16
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人