简单的登陆界面,无法把信息插入数据库。
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(
@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\mydb.mdf;Integrated Security=True;User Instance=True"))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
= "Select * from Table1 where username=@un";
cmd.Parameters.Add(new SqlParameter("un", textBox1.Text));
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string pw = reader.GetString(reader.GetOrdinal("password"));
if (pw == textBox2.Text)
{
MessageBox.Show("登陆成功!");
}
else
{
MessageBox.Show("登录失败!");
}
}
else
{
MessageBox.Show("用户名不存在!");
}
}
}
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(
@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\mydb.mdf;Integrated Security=True;User Instance=True"))
{
con.Open();
string upd = "Insert into Table1(username,password) values('textBox1.Text' ,'textBox2.Text')";
SqlCommand mycmd = new SqlCommand(upd,con);
MessageBox.Show("注册成功!!");
}
}
}
}
说明:快速注册的功能是向数据库插入数据,但是调试之后去数据库查看,并没有插入新数据。编译没有出错~