[b]我刚做了一个简单的登录功能,但提示user附近错误,附加源代码忘各位帮忙,谢谢
首先建立了一个架构并在里面建了一个类UserDataAccess:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace MVC_TeMail.DataAccess
{
    public  class UserDataAccess
    {
        SqlConnection Conn;
        SqlDataAdapter UserDa;        public UserDataAccess()
        {
            Conn = new SqlConnection("server=zhujun;database=test;user id=sa;pwd=654321");
            
        }        /// <summary>
        /// 检验用户是否可以登录
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns>DataSet</returns>
        public DataSet UserLogon(string name, string pwd)
        {
            DataSet UserDs = new DataSet();
            string sqlstr = "SELECT * FROM User WHERE NAME=@Name AND PWD=@Pwd";
            UserDa = new SqlDataAdapter();
            UserDa.SelectCommand = new SqlCommand(sqlstr, Conn);            UserDa.SelectCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.NVarChar,50));
            UserDa.SelectCommand.Parameters["@Name"].Value = name;
            UserDa.SelectCommand.Parameters.Add(new SqlParameter("@Pwd", SqlDbType.NVarChar,50));
            UserDa.SelectCommand.Parameters["@Pwd"].Value = pwd;
      
                    
            return UserDs;            
        }
    }
}
然后又建立了另一个架构及类UserRules:(注意要添加上面的引用)
using System;
using System.Collections.Generic;
using System.Text;
using MVC_TeMail.DataAccess;
using System.Data;namespace MVC_TeMail.Rule
{
    
    public  class UserRules
    {
        DataAccess.UserDataAccess Uda = new UserDataAccess();        /// <summary>
        /// 检测用户是否存在
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns>bool</returns>
        public bool UserCheck(string name,string pwd)
        {
            DataSet Uds= Uda.UserLogon(name,pwd);
            if (Uds.Tables[0].Rows.Count > 0)
                return true;
            return false;
        }
    }
}
最后在新建的网页中添加两个文本框,和一个button,一个label:Button按钮事件如下:
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MVC_TeMail.Rule.UserRules Urule = new UserRules();
       
        if (Urule.UserCheck(txtName.Text.Trim(), txtPwd.Text.Trim()))
        {
            lblMsg.Text = "登录成功!";
        }
        else
        {
            lblMsg.Text="登录失败!";
        }
            }
结果运行时提示提示user附近错误,就是我标记红色的地方。