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.SqlClient;namespace 项目
{
    public partial class 登陆 : Form
    {
        public 登陆()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {        }        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            this.Close();   
        }        private void button1_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select count(*) from Logins where UserID='{0}'and UserPwd='{1}'", txtId.Text, txtPwd.Text);
            try
            { 
                DBHelper.connection.Open();
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                int num = (int)command.ExecuteScalar();
                DBHelper.connection.Close();
                if (num != 1)//没返回  说明密码不存在
                {
                    MessageBox.Show("用户名或密码不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //判断用户类型  0为普通用户  1为会员
                    int type = 0;
                   
                    MessageBox.Show("登入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    string sql1 = string.Format("select State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text);
                    DBHelper.connection.Open();
                    SqlCommand command1 = new SqlCommand(sql, DBHelper.connection);
                    type = (int)command1.ExecuteScalar();//此处出错  返回值永远是1   难道不是用.ExecuteScalar()方法么?  希望各位老师帮帮忙 
                    Main main = new Main();
                    Second second = new Second();
                    if (type==0)
                    {
                        main.Show();
                        this.Visible = false;
                    }
                    else if(type==1)
                    {
                        second.Show();
                        this.Visible = false;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally {
                DBHelper.connection.Close();
            }
        }
    }
}
实验阶段  按钮名称没改   望见谅

解决方案 »

  1.   

    ExecuteScalar方法返回的类型是object类型,这个方法返回sql语句执行后的第一行第一列的值,由于不知到sql语句到底是什么样的结构(有可能是int,有可能是char等等),所以ExecuteScalar方法返回一个最基本的类型object,这个类型是所有类型的基类,换句话说:可以转换为任意类型。
      

  2.   

    //elect State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text
    你把txtId和txtPwd值放进去,在SQL里执行一下,看看state到底是不是1不就知道了,
      

  3.   

    你把txtId和txtPwd值放进去,在SQL里执行一下,看看state到底是不是1不就知道了,
      

  4.   

    SqlCommand command1 = new SqlCommand(sql, DBHelper.connection);是不是应该是:sql1 string sql1 = string.Format("select State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text);
      

  5.   

     type = (int)command1.ExecuteScalar();LZ 需要的应该是  command1.ExecuteNonQuery()  //此函数返回 sql 影响的函数,应该就是LZ 说的 1
      

  6.   

    ExecuteNonQuery()  的 返回值 是  int 类型 的哦
      

  7.   

    /* 
    SqlCommand command1 = new SqlCommand(sql, DBHelper.connection);
      type = (int)command1.ExecuteScalar();//此处出错 返回值永远是1 难道不是用.ExecuteScalar()方法么? 希望各位老师帮帮忙  
    */
    lz这里用的是sql 
    string sql = string.Format("select count(*) from Logins where UserID='{0}'and UserPwd='{1}'", txtId.Text, txtPwd.Text);当然返回1应该用string sql1 = string.Format("select State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text);
      

  8.   

    哈哈  谢谢mayanglong   谢谢了     同时也谢谢大家