首页 新闻 会员 周边

求助 SQL代码

0
[已解决问题] 解决于 2012-09-24 14:00

(select password,Id from u_users where (LoginName='{0}' or handphone='{0}' or eMail='{0}')", userName ) 这里面的userName是什么意思?

 

 

以下是整体代码 求解释!

  public static int ValidateUserInfo(string userName, string userPwd)
        {
            SqlDataReader tablePws = null;
            string md5pws = ToMD5(userPwd);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = staticValue.staticValue.userDB;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;

            cmd.CommandText = string.Format("select password,Id from u_users where (LoginName='{0}' or handphone='{0}' or eMail='{0}')", userName);
            conn.Open();
            tablePws = cmd.ExecuteReader();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();

            if (tablePws == null)
            {
                return -1;
            }
            else
            {
                string key = Encoding.Unicode.GetString(tablePws["password"] as byte[]);
                if (md5pws == Decrypt(key))
                {
                    int userId = 0;
                    int.TryParse(tablePws["Id"].ToString(), out userId);
                    return userId;
                }
                return -1;
            }
        }

水嫩小白菜的主页 水嫩小白菜 | 初学一级 | 园豆:3
提问于:2012-09-20 11:21
< >
分享
最佳答案
0

逗号后边的变量用来替换'{}'。逗号后边第一个代替'{0}',第二个代替'{1}'……而这个语句中只有'{0}',userName 。就是LoginName=userName  or handphone=userName  or eMail=userName 。

public static int ValidateUserInfo(string userName, string userPwd)

这里面的为userName 赋值,就表示为查询语句中的userName赋值。

奖励园豆:5
苏门答腊麝香猫 | 初学一级 |园豆:18 | 2012-09-21 11:00
其他回答(4)
0

你的一个是个string.format()吧,username是去填充{0}的,如果 你有{1},那就需要usernaem,这里再写一个变量来填充{1}

chenping2008 | 园豆:9836 (大侠五级) | 2012-09-20 11:25

那我要是写一个这样的SQL语句查询一个表里的信息怎么写?

查询姓名 性别 荣誉 头像地址

支持(0) 反对(0) 水嫩小白菜 | 园豆:3 (初学一级) | 2012-09-20 11:35
0

这里面的userName用于替换{0}

dudu | 园豆:30994 (高人七级) | 2012-09-20 11:46

能加个企鹅聊么?有问题请教!

支持(0) 反对(0) 水嫩小白菜 | 园豆:3 (初学一级) | 2012-09-20 12:03

@水嫩小白菜: 很少用企鹅

支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2012-09-20 15:20
0

这里面的userName用于替换{0}

jason2013 | 园豆:1998 (小虾三级) | 2012-09-20 15:58
0

这里的string.format()函数有2个参数,第一个参数就是格式化的格式字符串,这里就是那个一长串sql【"select password,Id from u_users where (LoginName='{0}' or handphone='{0}' or eMail='{0}')"】,然后这个字符串里出现的{},就会用第二个参数userName的值来替换。可以参照http://msdn.microsoft.com/en-us/library/system.string.format.aspx这个学习。

念念啊 | 园豆:415 (菜鸟二级) | 2012-09-21 15:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册