新手 望大家指导
我用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("用户名或密码不能为空");
                }
            }
        }
}还有一个性格测试:
我想从数据库中找到匹配的人后 将信息显示在一个表单上 应该
怎么实现 谢谢 

解决方案 »

  1.   

     数据库放程序目录里
    string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\liqi.mdb";
      

  2.   

      adapter =new OleDbDataAdapter(sql,str);  OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
      adapter.InsertCommand = builder.GetInsertCommand();是读取数据干嘛用InsertCommand,而不用selectCommand
    adapter.selectCommand= builder.selectCommand();
      table.Clear();
      adapter.Fill(table);
      

  3.   


    具体点行吗?我在debug里面用acess创建了一个liqi 行不行?
    cmd.ExecuteNonQuery();
    运行时 会在这里出错 是什么查询值的数目与目标字段中的数目不同
      

  4.   

    “查询值的数目与目标字段中的数目不同”,
    错误提示已经非常明确,就是你的"insert into 信息(帐号,密码,姓名,邮箱) values('" + a1 + "','" + b1 + "','" + c1 + "','" + d1 + "')"语句有问题:可能是你的数据库字段跟你插入的的字符数量不一致!
    你仔细检查,是不是少了哪个或哪几个字段!另外:你断点调试,直接把cmd.CommandText的值拷贝到数据库查询分析器里执行,问题会一目了然