5个sql语句准备方案

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 prjTest
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            1.
            //string loginId = this.txtLoginId.Text;
            //string loginPwd = this.txtLoginPwd.Text;
            //string sql = string.Format("Select * From UserInfos Where loginId='{0}' And loginPwd='{1}'",loginId,loginPwd);

            2.
            //string connString = @"server=.\sqlexpress;uid=sa;pwd=;database=TestDB;";
            //SqlConnection conn = new SqlConnection(connString);
            //SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandText = sql;

            //conn.Open();
            //SqlDataReader sdr = cmd.ExecuteReader();
            //bool b = false;
            //if (sdr.Read())
            //{
            //    b = true;
            //}
            //conn.Close();
            3.
            //if (!b)
            //{
            //    MessageBox.Show("登录名或密码错误。");
            //}
            //else
            //{
            //    MessageBox.Show("登录成功");
            //}



            1.
            //string loginId = this.txtLoginId.Text;
            //string loginPwd = this.txtLoginPwd.Text;

            命名参数
            //string sql = "Select * From UserInfos Where loginId=@loginId And loginPwd=@loginPwd";
            //SqlParameter p1=new SqlParameter("@loginId",loginId);
            //SqlParameter p2 = new SqlParameter("@loginPwd", loginPwd);

            2.
            //string connString = @"server=.\sqlexpress;uid=sa;pwd=;database=TestDB;";
            //SqlConnection conn = new SqlConnection(connString);
            //SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandText = sql;

            .........
            //cmd.Parameters.Add(p1);
            //cmd.Parameters.Add(p2);

            //conn.Open();
            //SqlDataReader sdr = cmd.ExecuteReader();
            //bool b = false;
            //if (sdr.Read())
            //{
            //    b = true;
            //}
            //conn.Close();
            3.
            //if (!b)
            //{
            //    MessageBox.Show("登录名或密码错误。");
            //}
            //else
            //{
            //    MessageBox.Show("登录成功");
            //}





            1.
            //string loginId = this.txtLoginId.Text;
            //string loginPwd = this.txtLoginPwd.Text;

            命名参数
            //string sql = "Select * From UserInfos Where loginId=@loginId And loginPwd=@loginPwd";
            //SqlParameter[] ps = {new SqlParameter(),new SqlParameter() };
            2.
            //string connString = @"server=.\sqlexpress;uid=sa;pwd=;database=TestDB;";
            //SqlConnection conn = new SqlConnection(connString);
            //SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandText = sql;

            .........
            

            //conn.Open();
            //SqlDataReader sdr = cmd.ExecuteReader();
            //bool b = false;
            //if (sdr.Read())
            //{
            //    b = true;
            //}
            //conn.Close();
            3.
            //if (!b)
            //{
            //    MessageBox.Show("登录名或密码错误。");
            //}
            //else
            //{
            //    MessageBox.Show("登录成功");
            //}







            1.
            //string loginId = this.txtLoginId.Text;
            //string loginPwd = this.txtLoginPwd.Text;

            命名参数  利用存储过程
            //string sql = "execute CheckLogin @loginId,@loginPwd";
            //SqlParameter[] ps = { 
            //    new SqlParameter("@loginId",loginId),
            //    new SqlParameter("@loginPwd",loginPwd) 
            //};
            2.
            //string connString = @"server=.\sqlexpress;uid=sa;pwd=;database=TestDB;";
            //SqlConnection conn = new SqlConnection(connString);
            //SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandText = sql;

            //cmd.Parameters.AddRange(ps);
            .........


            //conn.Open();
            //SqlDataReader sdr = cmd.ExecuteReader();
            //bool b = false;
            //if (sdr.Read())
            //{
            //    b = true;
            //}
            //conn.Close();
            3.
            //if (!b)
            //{
            //    MessageBox.Show("登录名或密码错误。");
            //}
            //else
            //{
            //    MessageBox.Show("登录成功");
            //}





            //1.
            string loginId = this.txtLoginId.Text;
            string loginPwd = this.txtLoginPwd.Text;

            //命名参数
            string procName = "CheckLogin";
            SqlParameter[] ps = { 
                new SqlParameter("@loginId",loginId),
                new SqlParameter("@loginPwd",loginPwd) 
            };
            //2.
            string connString = @"server=.\sqlexpress;uid=sa;pwd=;database=TestDB;";
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = procName;

            cmd.Parameters.AddRange(ps);
            //.........


            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            bool b = false;
            if (sdr.Read())
            {
                b = true;
            }
            conn.Close();
            //3.
            if (!b)
            {
                MessageBox.Show("登录名或密码错误。");
            }
            else
            {
                MessageBox.Show("登录成功");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值