日期:2014-05-18  浏览次数:20869 次

c# 与数据库连接
新手 望大家指导
我用c#写了一个注册登录 窗体 就是注册信息存入数据库 登陆时检验 但是运行时不对

注册时的代码:
namespace WindowsFormsApplication1
{
  public partial class Form6 : Form
  {
  OleDbDataAdapter adapter;//数据库连接
  DataTable table = new DataTable();
  string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liqi.mdb";
  OleDbConnection con = new OleDbConnection();
  public Form6()
  {
  InitializeComponent();
  con.ConnectionString = str;
  }
  public bool a,b,c,d;
  private void button1_Click(object sender, EventArgs e)
  {
  textBox1.Text = "";
  textBox2.Text = "";
  textBox3.Text = "";
  textBox4.Text = "";
  textBox5.Text = "";
  Application.Exit();
  }//取消注册

  private void Form6_Load(object sender, EventArgs e)
  {

  }

  private void button2_Click(object sender, EventArgs e)
  {
  string a1 = textBox1.Text;
  string b1 = textBox2.Text;
  string c1 = textBox4.Text;
  string d1 = textBox5.Text;
  OleDbCommand cmd = new OleDbCommand(@"insert into 信息(帐号,密码,姓名,邮箱) values('" + a1 + "','" + b1 + "','" + c1 + "','" + d1 + "')", con);
  con.Open();
  cmd.Connection = con;
  cmd.ExecuteNonQuery();
  con.Close();

  Form1 f1 = new Form1();
  f1.Show();
  this.Hide();
  }


登陆时的代码:
namespace WindowsFormsApplication1
{
  public partial class Form3 : Form
  {
  OleDbDataAdapter adapter;//数据库连接
  DataTable table = new DataTable();
  string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liqi.mdb";
  OleDbConnection con = new OleDbConnection();
  public Form3()
  {
  InitializeComponent();
  }

  private void Form3_Load(object sender, EventArgs e)
  {
  }

  private void button1_Click(object sender, EventArgs e)
  {
  if (textBox1.Text != "" && textBox2.Text != "")
  {
  string sql = "select * from 信息 where 学号='"+ textBox1.Text + "'and 密码='" + textBox2.Text + "'";
  adapter =new OleDbDataAdapter(sql,str);
  OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
  adapter.InsertCommand = builder.GetInsertCommand();
  table.Clear();
  adapter.Fill(table);
  if (table.Rows.Count > 0)
  {
  Form4 f4 = new Form4();
  f4.Show();
  this.Hide();
  }
  else
  {
  MessageBox.Show("用户名或密码不能为空");
  }
  }
  }


还有一个性格测试:
我想从数据库中找到匹配的人后 将信息显示在一个表单上 应该
怎么实现 谢谢

------解决方案--------------