哎,学了半年的C#,弄了程序忙活了大半天,结果老是错误,请教 这段代码哪错了,代码如下:  private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\duyu.mdb");
            OleDbCommand ocomd1 = new OleDbCommand();
            ocomd1.Connection = conn;
            ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+"");
            conn.Open();
            Form Form2 = new Form();
            try
            {
                if (textBox2.Text == ocomd1.ExecuteScalar().ToString())
                {
                    Form2.Show();
                    this.Hide();
                }
                else
                { MessageBox.Show("密码错误"); }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:因" + ex.Message + ",无法执行:");
                return;
            }
            
            
                conn.Close();在运行时提示:“错误:至少有一个参数未被指定值,无法执行"

解决方案 »

  1.   

    可以调试程序,在输入name 和 mima
    点登陆进入Form2前,C#中并没有提示哪句话错误,是运行的时候MessageBox给出的
      

  2.   

    private void button1_Click(object sender, EventArgs e)
            {
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\duyu.mdb");
                OleDbCommand ocomd1 = new OleDbCommand();
                ocomd1.Connection = conn;
                ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+"",conn);
                conn.Open();
                Form Form2 = new Form();
                try
                {
                    if (textBox2.Text == ocomd1.ExecuteScalar().ToString())
                    {
                        Form2.Show();
                        this.Hide();
                    }
                    else
                    { MessageBox.Show("密码错误"); }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:因" + ex.Message + ",无法执行:");
                    return;
                }
                
                
                    conn.Close();
      

  3.   

    name是什么类型?
    ocomd1.CommandText = ("select mima from duyu where name= '"+textBox2.Text+"'",conn); 
      

  4.   


    name 是access数据库中的,文本类型
      

  5.   

    断点调试是走了这一步
    conn.Open(); 然后呢,是否加到try语句中的if里面后,又提示:”因.ExecuteScalar已打开可用连接,连接的当前状态已关闭,无法执行“
    ,,急
      

  6.   

    ocomd1.CommandText = ("select mima from duyu where name= '"+textBox2 .Text+"'");
      

  7.   

    ocomd1.CommandText = ("select mima from duyu where name= '"+textBox2.Text+"'",conn); 
    文本类型少引号
      

  8.   

    你那么写能联上数据库吗?
    你要考虑到你的where子句里是传的参数,传参应该用''引起来.
      

  9.   

    ocomd1.CommandText = ("select count(*)from duyu where name= '"+textBox2 .Text+"'");
      

  10.   

    我是用了"的啊,我程序中改为这样了还是不行的,ocomd1.CommandText = ("select mima from duyu where name='"+textBox2 .Text+"'" );提示:”因.ExecuteScalar已打开可用连接,连接的当前状态已关闭,无法执行“
      

  11.   

    要返回一个ExecuteScalar必须是聚合函数 也就是说你的sql语句应该写成 select count(*)from duyu where name= '"+textBox2 .Text+"'
      

  12.   

    你把conn.Open(); 放在if的第一句试试,一般情况是你需要用到连接数据库操作的时候才打开的
      

  13.   

    private void button1_Click(object sender, EventArgs e)
            {
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\duyu.mdb");
                OleDbCommand ocomd1 = new OleDbCommand();
                ocomd1.Connection = conn;
                ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+"");
                conn.Open();
                Form Form2 = new Form();
                try
                {
                    if (textBox2.Text == ocomd1.ExecuteScalar().ToString())
                    {
                        Form2.Show();
                        this.Hide();
                    }
                    else
                    { MessageBox.Show("密码错误"); }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:因" + ex.Message + ",无法执行:");
                    return;
                }
                 finally
    {
                
                    conn.Close();
    }
    }
      

  14.   

    SqlConnection con = new SqlConnection(@"Data Source=WWW-8ED57AF494E;Initial Catalog=mydb;Integrated Security=True");
                SqlCommand com = new SqlCommand("select count(*) from tbl_user where name='"+textBox1.Text.Trim()+"' and pass='"+textBox2.Text.Trim()+"'",con);
                con.Open();
                int i=(int)com.ExecuteScalar();
                con.Close();
                if (i != 0) 
                {
                    Form1 f1 = new Form1();
                    f1.ShowDialog();
                }else if(i==0)
                {
                    MessageBox.Show("密码错误");
                }
      

  15.   

    这样就不会报这个错了```
    或者如下:
    private void button1_Click(object sender, EventArgs e)
            {
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\duyu.mdb");
                OleDbCommand ocomd1 = new OleDbCommand();
                ocomd1.Connection = conn;
                ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+"");
                if(conn.State == System.Data.ConnectionState.Closed)
                {
                conn.Open();
                }
                Form Form2 = new Form();
                try
                {
                    if (textBox2.Text == ocomd1.ExecuteScalar().ToString())
                    {
                        Form2.Show();
                        this.Hide();
                    }
                    else
                    { MessageBox.Show("密码错误"); }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:因" + ex.Message + ",无法执行:");
                    return;
                }
                 finally
    {
                if(conn.State== System.Data.ConnectionState.Open)
                    conn.Close();
    }
    }
    上面的写法就不会报数据库连接已打开的错误了```
      

  16.   

    提示未处理的OleDbExcaption什么什么的
      

  17.   

    一样提示未处理的OleDbExcaption什么什么的
      

  18.   

    那就是你的 Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\\duyu.mdb 
    这个字符串有问题了```你查下连你这个数据库要用什么字符串``
      

  19.   

    if (textBox2.Text == ocomd1.ExecuteScalar().ToString())
    ExecuteScalar()返回是首行首列,是不是你的TextBox里写的东西啊?
      

  20.   


    ocomd1.CommandText = ("select mima from duyu where name= '"+textBox2 .Text+"'");好像是缺少一个CommandType没有指定
      

  21.   

    ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+""); 
    差单引号,如果还有错误,添加断点调试下。看具体错在哪!
      

  22.   

    ----ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+"");----
    ----if (textBox2.Text == ocomd1.ExecuteScalar().ToString())----
    上面两条语句均有textBox2.Text,第一次出现的textBox2.Text是指用户名,第二次出现的textBox2.Text是用户密码,这是怎么回事?我有点看不懂了……
      

  23.   

    把OleDbCommand ocomd1 = new OleDbCommand();
    改成OleDbCommand cmd = conn.CreateCommand();就ok了,我测试过了
      

  24.   

         哎!!首先感谢各位朋友的积极回答,晚上请教了专业老师,终于把答案找出来啦,分享出来,当然上面有些朋友也已经发觉到一些错误了,直接表述.....这个程序共有三处错误其中上面的代码可见的有两处错误:
    一:在这一句代码中ocomd1.CommandText = ("select mima from duyu where name= "+textBox2 .Text+"");所用的textBox2.Text 是用户登陆的密码控件,而在下面的if (textBox2.Text == ocomd1.ExecuteScalar().ToString())
                    {
                        Form2.Show();
                        this.Hide();
                    }
       中同样用到了textBox2.Text,这里是指的用户名,,重复了就当然错的啦!!!~~~~上面有位朋友发现的了,,
    二:conn.Open(); 这句代码应该将其放到try  catch 语句中,具体就放在if语句上面;否则在运行的时候就会MessageBox中提示语句异常。我也不知道是什么意思,希望那位补充一下..........三:这个是我一时疏忽,在access数据库中,把name和mima这两个未放置在字段中,也就是主键不是他们两个中的任何一个,或者他两个不是字段(要怎么才说得清楚啊....)       在运行时MessageBox中提示无法连接,或不能指定连接等。
           这里是 我做C#程序中所遇到的问题,也希望对在编程道路以及探索C#的朋友有所帮助,thanks!!