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();
}
}
}
5个sql语句准备方案
最新推荐文章于 2022-08-10 16:06:33 发布