当前位置:编程学习 > C#/ASP.NET >>

C#编程 注册问题

在注册页面中填了用户需要注册的账号,怎么判断此账号已经存在于数据库中?
我在数据库里新建了一个表·表里存的是用户注册的用户名(userName)和密码(userPwd)//userName是表中存放用户名的列名。
但是在注册界面中要求有一个button的功能是实现··用户在注册新账户时点击button后确认此账号是否已被使用·
我想用遍历userName这一列来实现:(思路)——假设用户需要注册的账号写在TextBox1中,如果TextBox1.text的值与数据库里userName列中的一个用户名相同·输出“不可使用此账户名”。----这么想能实现此功能吗?如果能 该怎么遍历存放用户账户的那一列。
我查到可以使用ExecuteReader方法来做
但是我不知道怎么使用 , 能不能把具体使用ExecuteReader方法的代码写给我看!
由于我是个初学者!请尽量写清楚点!谢谢啊··
答案:
双击你在IDE设计窗口内拖在上面的button,
这时进入代码窗口
会自动生成一个响应button的onclick事件的方法,
在方法里写入下面内容
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("server=.;database=pubs;uid=sa,pwd=sa"))
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select userName from authors", con);

System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();

foo = false;

while (reader.Read())
{
if (TextBox1.Text.Equals(reader["userName"]))
{
foo = true;
}
}

if (foo)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "js", "alert('此用户名已经被占用!!!')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "js", "alert('此用户名可以使用!!!')", true);
}
}
要是按这中方法有些麻烦但也可实现,你可一利用用户名为条件,查询出是否有密码出现,因为你要定义用户名不能重复,一个用户名必须也有一个密码,把查询出的密码做为条见判断它是否为空即可。

也可着样:select count(*) from 注册表 where userName

int count = (int)Sqlcommand的对象名.ExecuteNonQue();

if(count==0)
{
//不存在
}
{
//存在
}
你将SQL语句写成"select * from <表名> where userName='+TextBox1.text+'"
在ExecuteReader方法下面加个判断
if(read.read)()
{
Messagebox.Show("不可使用此帐户名");
}
else
{
Messagebox.Show("恭喜该帐户名还没人使用");
}


如果你还有不明白的,可以加我QQ.我对C#数据库编程十分了解~呵呵...请楼主选项为满意答案哈~~
             SqlConnection conn1 = new SqlConnection("Data Source=localhost;Initial Catalog=OAdatabase;User ID=sa;Password=sa");
conn1.Open();
string sqlupd = string.Format("insert into UserInfo values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", this.TextBox1.Text.Trim(), this.TextBox2.Text.Trim(), this.TextBox3.Text.Trim(), this.DropDownList1.Text, this.DropDownList2.Text, this.DropDownList3.Text, this.DropDownList4.Text);
SqlCommand cmdupd = new SqlCommand(sqlupd, conn1);
try
{
cmdupd.ExecuteNonQuery();
Response.Write("<script language='javascript' type='text/ecmascript'>alert('用户注册成功!');window.location.href=> }
catch (SqlException)
{
Response.Write("<script language='javascript' type='text/ecmascript'>alert('此用户名已存在!');window.location.href=> }
finally
{
if (conn1.State == ConnectionState.Open)
{
conn1.Dispose();
conn1.Close();
}
}
自己写的可以直接运行,你参照着该下。其实也不用那么麻烦,你在设计表的时候把用户账号字段设置成主键就可以了。

上一个:有关C#编程问题 救急
下一个:C#编程的小问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,