写程序判断用户名是否存在,可以按下面路子走:
1.在命名空间处写上新的名子:using System.Data.SqlClient
2.写SqlConnection new(server=.;数年据库;Window身份;);--连接
conn.Open();
3.SqlCommand new -----写入数据库语句,检验用户名是否存在用读取:select
常用语句:
select * from...
update users set where id=1,conn
delete users where
insert into users(userName,password) valuse(@userName,@password)
4.加参数:
cmd.Parameters.Add("@userName", SqlDbType.VarChar,20).Value=this.txtUserName.Text;
5.只读器
SqlDataReader dr= cmd.ExecuteReader();
6.int i(i表示查出的总数)
7.打开只读器
while (dr.Read())
{
i=Convert.ToIn32 ( dr[0].ToString());
}
8.关闭只读器
Dr.Close();
具体程序如下:
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 _月13日studentSystem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnlogin_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=StudentSystem;trusted_Connecting=sspi;");
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from users where userName= @userName",conn);
cmd.Parameters.Add("@userName", SqlDbType.VarChar,20).Value=this.txtUserName.Text;
SqlDataReader dr= cmd.ExecuteReader();
int i =0;//给i赋初值为零
while (dr.Read())
{
i=Convert.ToIn32 ( dr[0].ToString());
}
dr.Close();
//至此判断用户名是否存在己经完成,
阅读(1470) | 评论(0) | 转发(0) |