最近在做个管理系统的小项目的登陆界面
为了测试
我创建了一个窗体
2个textbox 1个button
string sqlconstring = "server=.;database=db_csglxt;uid=sa;pwd=123456;";
SqlConnection myconn = new SqlConnection(sqlconstring);
myconn.Open();
string sqlstruser = "select * from  user_info where username = '" + textBox1.Text + "'";
SqlCommand cmd = new SqlCommand(sqlstruser, myconn);
if (cmd.ExecuteNonQuery() == 1)//这个函数的问题
{
     textBox2.Text = "用户名存在";
}
else
{
     textBox2.Text = "用户名不存在";
}1.以前用insert 的时候ExecuteNonQuery()这个函数没问题
select 的话就会出现问题
查了下说select 用这个函数会一直返回-1,看那些解释不是很明白
为什么2.我想实现一个功能
用到数据库中查询用户名是否存在,存在的话继续查询用户名对应的密码是否正确
否则提示错误
请问有什么函数可以实现不?3.我打算把所有的SQL语句全部封到一个类里面,包括后面可能用到的删除,修改等操作
有什么特别的讲究或者封的时候要注意什么么?
请各位给点建议。谢谢大家

解决方案 »

  1.   

    1、ExecuteNonQuery()返回的是true或false
       你要查询用DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter();
                da.Fill(ds, "table");
    2、int count = int.Parse(cmd.ExecuteScalar().ToString());
       if(count>0)
       就能解决你的问题
    3、我还没想到有什么要注意的问题
      

  2.   

    1.ExecuteNonQuery()返回的是影响的行数,并不是返回bool,返回的是int
    2.ExecuteScalar()值是返回第一行第一列,不信可以取值看看
    3、最好就融成一个数据层
      

  3.   

    不好意思上面解释错了您可以使用 ExecuteNonQuery 来执行目录操作(例如查询数据库的结构或创建诸如表等的数据库对象),或通过执行 UPDATE、INSERT 或 DELETE 语句,在不使用 DataSet 的情况下更改数据库中的数据。
    虽然 ExecuteNonQuery 不返回任何行,但映射到参数的任何输出参数或返回值都会用数据进行填充。
    对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。这些方法具体看一下
    http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executenonquery(VS.80).aspx
      

  4.   

    ExecuteNonQuery
    一般执行没有返回值的操作(或者说无所谓返回值),
    如create table|trigger|procedure,再如update,delete,alter等
      

  5.   

    数据库操作封装成一个类,,外部只传Sql语句嘛
            #region 属性字段
            /// <summary>
            /// 数据库连接
            /// </summary>
            private OleDbConnection conn = null;
            /// <summary>
            /// 对数据源执行的 SQL 语句和过程
            /// </summary>
            private OleDbCommand comm = null;
            /// <summary>
            /// 用于填充 DataSet 和更新数据源
            /// </summary>
            private OleDbDataAdapter adapter = null;
            /// <summary>
            /// 读取数据行的只进流
            /// </summary>
            private OleDbDataReader reader = null;
            #endregion        /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="strconn">连接字符串</param>
            public CDBOperate(string strconn)
            {
                try
                {
                    if (null == this.conn || ConnectionState.Closed == this.conn.State)
                    {
                        this.conn = new OleDbConnection(strconn);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            
            /// <summary>
            /// 获得读取数据行的只进流
            /// </summary>
            /// <param name="sqlstr">被执行的Sql语句</param>
            /// <returns></returns>
            public IDataReader executeReader(string sqlstr)
            {
                try
                {
                    this.comm = new OleDbCommand();
                    this.comm.Connection = this.conn;
                    this.comm.CommandText = sqlstr;
                    this.conn.Open();
                    this.reader = this.comm.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    this.Close();
                }
                return this.reader;
            }        /// <summary>
            /// 获得DataSet数据集
            /// </summary>
            /// <param name="sqlstr">被执行的Sql语句</param>
            /// <returns></returns>
            public DataSet getDataSet(string sqlstr)
            {
                DataSet ds = new DataSet();
                try
                {
                    this.adapter = new OleDbDataAdapter(sqlstr, this.conn);
                    this.adapter.Fill(ds);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    this.Close();
                }
                return ds;
            }        /// <summary>
            /// 对数据库执行查询,删除,插入,更新操作
            /// </summary>
            /// <param name="sqlstr">被执行的Sql语句</param>
            /// <returns>数据库受影响行</returns>
            public int executeSql(string sqlstr)
            {
                int num = 0;
                try
                {
                    this.comm = new OleDbCommand();
                    this.comm.CommandText = sqlstr;
                    this.comm.Connection = this.conn;
                    this.conn.Open();
                    num = this.comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    this.Close();
                }
                return num;
            }
    给你不分代码参考
      

  6.   

    谢谢楼上几楼刚才2楼给的这个DataSet ds = new DataSet(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    da.Fill(ds, "table"); 怎么用了不成功
    郁闷了
    谁能给下完整一点的代码或者告诉我下用什么函数好么
    谢谢了
      

  7.   

    string sqlconstring = "server=.;database=db_csglxt;uid=sa;pwd=123456;";
    SqlConnection myconn = new SqlConnection(sqlconstring);
    myconn.Open();
    string sqlstruser = "select count(*) from  user_info where username = '" + textBox1.Text + "'";
    SqlCommand cmd = new SqlCommand(sqlstruser, myconn);
    if ((int)cmd.ExecuteScalar() == 1)//这个函数的问题
    {
         textBox2.Text = "用户名存在";
    }
    else
    {
         textBox2.Text = "用户名不存在";
    }没测试过
      

  8.   


    conn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand();
            da.SelectCommand.Connection = conn;
            da.SelectCommand.CommandType = CommandType.Text;
            da.SelectCommand.CommandText = "select * from t_dm_param";
            da.Fill(ds, "table");
            conn.Close();
            return ds;
      

  9.   

    string sqlconstring = "server=.;database=db_csglxt;uid=sa;pwd=123456;"; 
    SqlConnection myconn = new SqlConnection(sqlconstring); 
    myconn.Open(); 
    string sqlstruser = "select count(*) from  user_info where username = '" + textBox1.Text + "'"; 
    SqlDataAdapter sda=new SqlDataAdapter(sqlstruser,myconn)
    dataset ds=new dataset()
    sda.Fill(ds);
    if(ds.count>0)
    {
       textBox2.Text = "用户名存在"; 
    }
    else
    {
       textBox2.Text = "用户名不存在"; 
    }
    试试看
      

  10.   


    1.这个方法返回的是影响的行数,比如更新了一行就为1,更新了2行就为2,查询没有影响数据,就为-1
    2.这个用检索saldataadapter,sqldatareader都可以
    3.建议用现成的操作类sqlhelper,网上很多的
      

  11.   

    谢谢楼上的各位
    问题解决了
    string sqlconstring = "server=.;database=db_csglxt;uid=sa;pwd=123456;";
                SqlConnection myconn = new SqlConnection(sqlconstring);
                myconn.Open();
                string sqlstruser = "select * from  user_info where username = '" + textBox1.Text + "'";
                SqlCommand cmd = new SqlCommand(sqlstruser, myconn);
                int count1 = Convert.ToInt32(cmd.ExecuteScalar());
                if (count1>0)//这个函数的问题
                {
                    textBox2.Text = "用户名存在";
                }
                else
                {
                    textBox2.Text = "用户名不存在";
                }
    这样就实现了
    但是这句
    int count1 = Convert.ToInt32(cmd.ExecuteScalar());不是很明白
    有人能帮忙解释下么
      

  12.   

    int count1 = Convert.ToInt32(cmd.ExecuteScalar());不是很明白 cmd.ExecuteScalar()读出来的数据不是int类型的了啦  把鼠标放到ExecuteScalar()上面看看他返回什么类型不就ok了吗?