sql 查询语句

  • select * from UserInfo   从userInfo表中查询所有字段
  • select Username from UserInfo   从userInfo表中查询Username字段
  • select Username,Pwd from UserInfo  从userInfo表中查询Username和Pwd俩字段
  • select * from UserInfo where age>15  查询表中年龄大于15的数据
  • select Username,Pwd from UserInfo where age>15  查询表中年龄大于15的用户名和密码
  • select * from UserInfo where username='admin' and pwd=123 查询出用户名为admin 密码为123 的用户
  • select age from UserInfo where username='admin' and pwd=123 查询出用户名为admin 密码为123 的年龄信息
  • select * from UserInfo where sex='男' 查询出所有男生
  • select * from UserInfo where sex='男' and country='上海' 查询出所有在上海的男生
  • select * from UserInfo where age>30 and sex='女' and (country='上海' or country='北京')  查询所有在上海或者北京的女生,并且年龄在30之上
  • select * from UserInfo where age>20 and age<50
    select * from UserInfo where age between 20 and 50
    查询出年龄大于20小于50之间的学生
  • select COUNT(*) from UserInfo where country='上海'  查询出上海的学员总数
  • select COUNT(*) as 总数 from UserInfo where country='上海'    起别名(给查询出来的列起了个名字)
  • select MIN(age) from UserInfo where country='上海'    查询上海地区年龄最小的 (最大MAX)
  • select SUM(age) from UserInfo where country='上海'    上海地区年龄总和
  • select AVG(age) from UserInfo where country='上海'     上海地区平均年龄
  • select * from UserInfo where age>(select AVG(age) from UserInfo where country='上海')    上海地区大于平均值的
  • select * from UserInfo order by age asc     按照年龄升序排列(desc降序)
  • select * from UserInfo where username like '%a%'  中间包含a
    select * from UserInfo where username like 'a%'  以a开头
    select * from UserInfo where username like '%a'  以a结尾
  • select top(3) from UserInfo  前3条数据
  • select COUNT(*) country from UserInfo group by country 给城市分组查询
  • select COUNT(*) country from UserInfo group by country having COUNT(*)>2    分组查询之后的条件查询
  • select distinct country from UserInfo  去掉重复
  • select top 5 * from (
        select ROW_NUMBER() over (order by userid) as rowNum,* from UserInfo
    ) e where rowNum>0      分页查询(0表示第一页,每页5条,通过userid查询)
  • select top 5 * from (
        select ROW_NUMBER() over (order by userid) as rowNum,* from UserInfo
    ) e where rowNum>0  and sex='男'   分页查询 男(0表示第一页,每页5条,通过userid查询)
  • select userid,age,sex,
    case
    when age<30 then '大一'
    when age>30 and age<40 then '大二'
    when age>40 and age<50 then '大三'
    else '大四'
    end as classname from UserInfo  条件查询 类似switch case
  • 对null值进行设置,为空就以0填充select ISNULL(age,0) from UserInfor
  • select GETDATE() 获取到年月日时分秒
  • select YEAR(GETDATE()) 获取年
  • select MONTH(GETDATE()) 获取月
  • select DAY(GETDATE()) 获取日
  • select DATEADD(yy,100,GETDATE()) 设置到未来的某个时间(100年)
  • select DATEADD(mm,1,GETDATE())  一个月之后
  • select DATEADD(dd,10,GETDATE()) 10天之后
  • select DATEDIFF(dd,GETDATE(),2019/12/12) 倒计时 到某个时间节点的
  • update UserInfo set username='aa' and pwd='111' where userid=1  修改 id=1 里的username和密码
  • delete UserInfo where userid=1  删除id=1的这条数据
  • insert into UserInfo values('admin','111',11,'2019-08-07','男','null')
  • 添加一条数据,不需要的字段用null
  • insert into UserInfo (username,pwd) values('admin123','222') 按需求添加字段内容
  • 打开表--userInfo -- 找到age字段 --右键-- check 约束 -- 添加 --右键填写表达式(check约束  比如年龄不能大于200这种)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace dlapss { public partial class Frm_Login : Form { public Frm_Login() { InitializeComponent(); } //登录 private void btt_Login_Click(object sender, EventArgs e) { loginValidate(); } /// <summary> /// 登录验证方法 /// </summary> public void loginValidate() { if (txt_UserloginId.Text.Trim() == "" || string.IsNullOrEmpty(txt_UserloginId.Text)) { MessageBox.Show("用户名不能为空!", "登录提示"); txt_UserloginId.Focus(); } else if (txt_UserPass.Text.Trim() == "" || string.IsNullOrEmpty(txt_UserPass.Text)) { MessageBox.Show("密码不能为空!", "登录提示"); txt_UserPass.Focus(); } else if (cbo_loginType.Text.Trim() == "" || string.IsNullOrEmpty(cbo_loginType.Text.Trim())) { MessageBox.Show("请选择登录类型!", "登录提示"); } else { UserInfo u = null; SqlConnection con = new SqlConnection(DBHelper.conStr); try { con.Open(); int userRole = cbo_loginType.SelectedIndex; string sql = string.Format("select * from userInfo where UserloginId='{0}' and UserPass='{1}' and UserRole={2}", txt_UserloginId.Text, txt_UserPass.Text, userRole); SqlCommand com = new SqlCommand(sql, con); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { u = new UserInfo(); u.UserId = Convert.ToInt32(dr["UserId"]); u.UserloginId = dr["UserloginId"].ToString(); u.UserName = dr["UserName"].ToString(); u.UserPass = dr["UserPass"].ToString(); u.UserRole = dr["UserRole"].ToString(); } dr.Close(); if (u != null) { LoginInfo.LoginUserInfo = u;//保存登录用户信息 this.Visible = false; Frm_Main fm = new Frm_Main(); fm.Show(); } else { MessageBox.Show("用户名或密码错误!", "登录提示"); } } catch (Exception) { MessageBox.Show("请注意数据库连接字符串!", "登录提示"); } finally { con.Close(); } } } //重置 private void btt_Reset_Click(object sender, EventArgs e) { txt_UserloginId.Text = ""; txt_UserPass.Text = ""; txt_UserloginId.Focus(); } private void cbo_loginType_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter)//当点击回车键 { if (txt_UserloginId.Text != "" && txt_UserPass.Text != "") loginValidate(); else SendKeys.Send("{TAB}"); } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值