[csharp]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data.OleDb;
namespace 登录数据库
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
MessageBox.Show("提示:请输入用户名和密码!", "警告");
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from 用户 where 用户名='" + textBox1.Text.Trim() + "' and 密码='" + textBox2.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
MessageBox.Show("登录成功!", "提示");
else
MessageBox.Show("提示:学生用户名或密码错误!","警告");
conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
MessageBox.Show("请输入用户名、密码!", "警告");
else
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Student;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from 用户 where 用户名='" + textBox1.Text.Trim()+"'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (sdr.HasRows)
MessageBox.Show("该用户已注册,请使用其他用户名", "提示");
else
{
sdr.Close();
string myinsert = "insert into 用户(用户名,密码) values ('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand mycom = new SqlCommand(myinsert, conn); //定义OleDbCommnad对象并连接数据库
mycom.ExecuteNonQuery(); //执行插入语句
conn.Close(); //关闭对象并释放所占内存空间
conn.Dispose();
MessageBox.Show("您已注册成功!");
}
}
}
}
}
登录界面
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。