为什么用户注册成功,信息却添加不进数据库里呢?请高手指教,大家帮小妹看看到底是哪出错了,纠结好几天了。
附代码:
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.OleDb;
using System.Data.SqlClient;namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("用户名或密码不能为空!");
return;
}
if (textBox3.Text == "")
{
MessageBox.Show("确认密码不能为空!");
return; }
if (textBox2.TextLength < 3 || textBox3.TextLength > 16)
{
MessageBox.Show("密码不符合标准!");
return;
}
else
{
if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("二次密码不一致,请重新输入!"); textBox2.Text = "";
textBox3.Text = "";
return;
} string mypath = Application.StartupPath + "\\db1.accdb";
string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + mypath;
OleDbConnection mycon = new OleDbConnection(constr);
mycon.Open();
string mysql = "select * from biao1 where user='" + textBox1.Text + "'and password='" + textBox2.Text + "'"; OleDbCommand cmd = new OleDbCommand(mysql, mycon); OleDbDataReader dr = cmd.ExecuteReader(); if (!dr.Read())
{
dr.Close();//关闭reader //重新定义数据库查询语言
string CommandText = "INSERT INTO biao1 (user,password) Values ('" + this.textBox1.Text + "','" + this.textBox2.Text + "')";
// cmd= new OleDbCommand(mysql, mycon);
// cmd = new OleDbCommand(CommandText, mycon);
cmd.ExecuteNonQuery();
// oo.ExecuteNonQuery();//执行查询语言
mycon.Close();
MessageBox.Show("注册成功"); //给出提示,注册成功
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Form1 frm = new Form1();
frm.Show();
this.Hide();
}
else {
MessageBox.Show("该用户存在");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
mycon.Close();
}
}
} private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ }
}
}
附代码:
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.OleDb;
using System.Data.SqlClient;namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("用户名或密码不能为空!");
return;
}
if (textBox3.Text == "")
{
MessageBox.Show("确认密码不能为空!");
return; }
if (textBox2.TextLength < 3 || textBox3.TextLength > 16)
{
MessageBox.Show("密码不符合标准!");
return;
}
else
{
if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("二次密码不一致,请重新输入!"); textBox2.Text = "";
textBox3.Text = "";
return;
} string mypath = Application.StartupPath + "\\db1.accdb";
string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + mypath;
OleDbConnection mycon = new OleDbConnection(constr);
mycon.Open();
string mysql = "select * from biao1 where user='" + textBox1.Text + "'and password='" + textBox2.Text + "'"; OleDbCommand cmd = new OleDbCommand(mysql, mycon); OleDbDataReader dr = cmd.ExecuteReader(); if (!dr.Read())
{
dr.Close();//关闭reader //重新定义数据库查询语言
string CommandText = "INSERT INTO biao1 (user,password) Values ('" + this.textBox1.Text + "','" + this.textBox2.Text + "')";
// cmd= new OleDbCommand(mysql, mycon);
// cmd = new OleDbCommand(CommandText, mycon);
cmd.ExecuteNonQuery();
// oo.ExecuteNonQuery();//执行查询语言
mycon.Close();
MessageBox.Show("注册成功"); //给出提示,注册成功
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
Form1 frm = new Form1();
frm.Show();
this.Hide();
}
else {
MessageBox.Show("该用户存在");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
mycon.Close();
}
}
} private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ }
}
}
string CommandText = "INSERT INTO biao1 (user,password) Values ('" + this.textBox1.Text + "','" + this.textBox2.Text + "')";
// cmd= new OleDbCommand(mysql, mycon);
// cmd = new OleDbCommand(CommandText, mycon);
cmd.CommandText = CommandText;
cmd.ExecuteNonQuery();