using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace SQLHelp//数据库操作help类
{
    public class SQLCMDFunction
    {   //连接字符串
        public static string  connString="server=.;database=MyfirstDataBase;Trusted_Connection=SSPI;";
        //非查询操作
        #region
        public static int  ExecuteNonquery(string cmdString, params SqlParameter[] ps )
        {
        using(SqlConnection conn=new SqlConnection( connString))
        {
            using(SqlCommand comm=new SqlCommand(cmdString,conn))
            {
                comm.Parameters.AddRange(ps);
                conn.Open();
                return comm.ExecuteNonQuery();
            }
        }
        }
        #endregion
        //查询操作
        #region
        public static object ExecuteScalar(string cmd,params SqlParameter[] ps)
        {
           using(SqlConnection conn=new SqlConnection(connString))
           {
               using(SqlCommand comm=new SqlCommand(cmd,conn))
               {
                   comm.Parameters.AddRange(ps);
                   conn.Open();
                  return  comm.ExecuteScalar();
               }
           }
        }
        #endregion
        //读操作
        #region
        public static SqlDataReader ExecuteReader(string cmd,params SqlParameter[] ps)
        {
            SqlConnection conn = new SqlConnection(connString);
            try
            {
                using (SqlCommand comm = new SqlCommand(cmd, conn))
                {
                    comm.Parameters.AddRange(ps);
                    conn.Open();
                    return comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
            }
            catch(Exception ep)
            {
                conn.Dispose();
                throw ep;
            }
        }
        #endregion
        //DataBase适配器
        #region
        public static System.Data.DataSet GetDataSet(string cmd,params SqlParameter[] ps)
        {
            DataSet da = new DataSet();
            using(SqlDataAdapter sda=new SqlDataAdapter(cmd,connString))
            {
                sda.SelectCommand.Parameters.AddRange(ps);
                sda.Fill(da);
                
            }
            return da;

}
        #endregion
    }

}

//*********************************************************************************************test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace SqlTest
{
    class sqlServerTest
    {
        static void Main(string[] args)
        {
            string connString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
            string commString = "select * from [dbo].[LoginTest];";
            string commString1 = "insert into [dbo].[LoginTest] (userName,password,lastLoginTime) values('狮子','1235','2016-01-01 00:00:00.123');";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = connString;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = commString1;
            conn.Open();
            int count = cmd.ExecuteNonQuery();
            Console.WriteLine("{0}受影响", count);
            Console.ReadKey();
        }
    }
}

//*****************************************************************************************************************实例登录form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using SQLHelp;
namespace StudentSystem
{
    public partial class FormLoginIn : Form
    {
        public FormLoginIn()
        {
            InitializeComponent();
        }

private void LoginIn_Click(object sender, EventArgs e)
        {
            string uID = textBoxUserID.Text.Trim();
            string password = textBoxPassword.Text;
            if (string.IsNullOrEmpty(uID) || string.IsNullOrEmpty(password)) return;
            string cmdString = "select count(*) from stuDB where userName=@uid and password=@pwd;";
            SqlParameter[] ps = { new SqlParameter("@uid", uID), new SqlParameter("@pwd", password) };
            int count = (int)SQLCMDFunction.ExecuteScalar(cmdString, ps);
            if(count>0)
            {
                MessageBox.Show("登录成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            { 
                MessageBox.Show("登录失败");
                this.DialogResult = DialogResult.None;
            }
        }

}
}

//*****************************************************************************************************************************实例主界面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SQLHelp;
namespace StudentSystem
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

private void FormMain_Load(object sender, EventArgs e)
        {
            LoadData();
        }

private void LoadData()
        {
            dgView.DataSource = SQLCMDFunction.GetDataSet("select * from sds;").Tables[0];
        }

}
}

//************************************************************************************************************************************登录参数类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoginTest
{
   public static class Parameter
    {
        private static string connectionString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
        public static  string ConnectionString
        {
            set { connectionString = value; }
            get { return connectionString; }
        }
    }
}

//*********************************************************************************************************************************注册,取消,登录

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LoginTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void button2_Click(object sender, EventArgs e)
        {
            string uid = uidText.Text.Trim();
            string pwd1 = pwdText1.Text;
            string pwd2 = pwdText2.Text;
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd1) || string.IsNullOrEmpty(pwd2))
            {
                MessageBox.Show("请输入完整信息");
                return;
            }
            if (pwd1 != pwd2)
            { MessageBox.Show("请输入一致"); }
            string conString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
            string cmdString = string.Format("insert into [dbo].[LoginTest] (userName,password) values('{0}','{1}');",uid,pwd1);
            string cmdString2 = "insert into [dbo].[LoginTest] (userName,password) values('大想','1235');";
            uidText_Leave(sender, e);
            try
            {
                if (label4.Text == "√")
                {
                    using (SqlConnection conn = new SqlConnection(conString))
                    {
                        using (SqlCommand comm = new SqlCommand(cmdString, conn))
                        {
                            conn.Open();
                            int count = comm.ExecuteNonQuery();
                            MessageBox.Show(count > 0 ? "注册成功" : "注册失败");
                            conn.Close();
                        }

}
                }
                else
                {
                    MessageBox.Show("注册名非法");
                }
            }
            catch(Exception ex)
            { MessageBox.Show(ex.ToString()); }

}

private void uidText_Leave(object sender, EventArgs e)
        {
            string uid = uidText.Text.Trim();
            if (string.IsNullOrEmpty(uid)) return;
            string conString = @"server=.;database=MyfirstDatabase;Trusted_Connection=SSPI;";
            string cmdString = "select count(*) from LoginTest where userName='" + uid + "'"; 
            int count;
           using(SqlConnection conn=new SqlConnection(conString))
           {
               using(SqlCommand comm=new SqlCommand(cmdString,conn))
               {
                   conn.Open();
                   count = (int)comm.ExecuteScalar();
                   conn.Close();
               }
           }
            if(count>0)
            {
                label4.ForeColor = Color.Red;
                label4.Text = "用户已存在";
            }
            else
            {
                label4.ForeColor = Color.Green;
                label4.Text = "√";
            }

}

private void timer1_Tick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(uidText.Text))
            {
                label4.Text = null;
            }
        }

private void butLgin_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
    }
}

//***************************************************************************************************登录

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LoginTest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

private void butLogin_Click(object sender, EventArgs e)
        {
            
            string uid=uidText.Text.Trim();
            string pwd=pwdText.Text;
            string cmd="select count(*) from LoginTest where userName='"+uid+"' and password='"+pwd+"';";
            int count;
            if(string.IsNullOrEmpty(uid)||string.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("用户名或密码不能为空");
                return;
            }
           using(SqlConnection conn=new SqlConnection(Parameter.ConnectionString))
           {
               using(SqlCommand comm=new SqlCommand(cmd,conn))
               {
                   conn.Open();
                   count = (int)comm.ExecuteScalar();
                   conn.Close();
               }
           }
           if(count>0)
           {
               MessageBox.Show("登陆成功");
               Form3 form3 = new Form3();
               form3.Show();
           }
            else
           { 
               MessageBox.Show("登陆失败,用户名或密码错误"); 
           }

}
    }
}

//********************************************************************************************************报告界面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginTest
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

private void Form3_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“MyfirstDataBaseDataSet.LoginTest”中。您可以根据需要移动或删除它。
            this.LoginTestTableAdapter.Fill(this.MyfirstDataBaseDataSet.LoginTest);

this.reportViewer1.RefreshReport();
        }
    }
}

C#sql帮助类(登录查询界面)语句实例相关推荐

  1. MS SQL入门基础:数据查询--SELECT语句

    数据库是为更方便有效地管理信息而存在的人们,希望数据库可以随时提供所需要的数据信息.因此,对用户来说,数据查询是数据 库最重要的功能.本章将讲述数据查询的实现方法. 在数据库中,数据查询是通过SELE ...

  2. scott登录查询常用语句

    一.简单查询 1.简单查询 select * from emp;--查询表emp中的所有数据 select empno as id,ename as name from emp;--查询表emp中的e ...

  3. SQL多表连接查询(具体实例)

    本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student  截图例如以下: 表2:course  截图例如以下: (此时这样建表仅仅是为了演示连接SQL语句.当然实际开发中我们 ...

  4. SQL多表连接查询(详细实例)

    本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际开发中我们不会这样建表, ...

  5. 帝国cms php sql,帝国CMS下在PHP文件中调用数据库类执行SQL语句实例

    帝国CMS下在PHP文件中调用数据库类执行SQL语句实例 例1:连接MYSQL数据库例子.(a.php) db_close(); //关闭MYSQL链接$empire=null; //注消操作类变量? ...

  6. 云服务器怎么执行sql文件在哪里,总结帝国CMS下在PHP文件中怎么调用数据库类执行SQL语句实例...

    总结帝国CMS下在PHP文件中怎么调用数据库类执行SQL语句实例 发布时间:2020-10-19 14:58:08 来源:亿速云 阅读:83 作者:小新 这篇文章将为大家详细讲解有关总结帝国CMS下在 ...

  7. Hibernate→HQL、query.list()返回数据类型、查询相关语句、分页、原生SQL、@注解、持久化对象状态及生命周期、一多关系、继承映射关系、逆向工程

    HQL Query实例与表 session通用工具类 Query对象 from 类→List<类>接收 映射类 仅查询商品 查询商品及所在商家 别名 返回数据类型定义 Iterator接收 ...

  8. sql镶嵌查询_SQL语句 - 嵌套查询

    嵌套查询的意思是,一个查询语句(select-from-where)查询语句块可以嵌套在另外一个查询块的where子句中,称为嵌套查询.其中外层查询也称为父查询,主查询.内层查询也称子查询,从查询. ...

  9. oracle模糊查询like语句,sql语句select like模糊查询用法

    本节内容: sql语句之select like模糊查询语法 1,like语句的语法格式是:select * from 表名 where 字段名 like 对应值(子串),它主要是针对字符型字段的,它的 ...

最新文章

  1. 阿里成立达摩院,引入10位顶尖科学家3年投入预计超1000亿,马云提出4条期待(附:学术咨询委员会成员名单)
  2. 怎样用c语言写一个系统,用C语言写关于操作系统的一个问题。
  3. WPF整理-使用逻辑资源
  4. 在路由器使用ACL防止IP地址欺骗
  5. YOLO (You only look once) 实时目标检测
  6. defender 删除_Java 8中的默认方法(Defender方法)简介
  7. 正则表达式(读书过程所记未整理)
  8. jquery基本过滤选择器(jquery筛选选择器)
  9. 【大地信】新时代GIS发展趋势与未来展望
  10. F28335课后习题记录
  11. 一个高考落榜生的奋斗历程
  12. mysql 竖列变成横行_mysql 横变竖 竖变横
  13. 中国电子科技集团公司家族谱及信息
  14. iphone5s怎么取消iphone系统更新如何关闭系统更新提醒
  15. 深度学习 tensorflow 三维矩阵乘法(batch 迭代必须搞懂的矩阵乘法,维度增加)
  16. 如何把二维数组传参给函数
  17. 用Keras构建神经网络的3种方法
  18. JavaWeb学习心得总结
  19. 第八章 习惯五 知彼解己——移情沟通的原则
  20. 程序员只能吃“青春饭”?IT行业年龄焦虑如何破局?

热门文章

  1. python学习-数据类型(列表→创建、取值、大小、长度)
  2. Eclipse 的快捷键以及文档注释、多行注释的快捷键 一、多行注释快捷键
  3. 怀旧服服务器怎么调整显卡性能,用顶级配置玩wow怀旧服是怎样的体验?
  4. GCC 链接时出现undefined reference to “...”时可能解决办法
  5. python有效变量名_Python变量命名规则
  6. 长安大学研究生院计算机学院,研究生教育
  7. hive运行mysql脚本_用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
  8. linux ftp 553,修复使用vsftp出错553 Could not create file的有效方法
  9. julia有 pytorch包吗_用 PyTorch 实现基于字符的循环神经网络 | Linux 中国
  10. android sqlite更改数据,更新现有的sqlite数据库中的列,但没有任何更改android