我是初学者编码器,我正在使用C#Asp.Net构建一个项目,其中我正在注册用户ID的用户,现在我的问题是如何在用户表中检查用户ID是否已存在于用户表中试图注册,我使用的是SQL Server 2000?
Car*_*000 10
根据评论中的代码,我会说你需要的是一个关于如何使用ADO.NET查询数据的好的入门教程,如果有人可以推荐一个.
首先,您不能在查询中使用"username.Text",SQL Server对您的ASP.NET页面一无所知,而且它是"用户名"TextBox.
你需要一个参数传递到您的SqlCommand(不要过建立一个像一个字符串"选择tbl_userlogin其中USER_ID =*" + username.Text,如果你想知道为什么谷歌为"SQL注入攻击"),就像这样:
SqlCommand cmd = new SqlCommand("select * from tbl_userlogin where User_id = @UserID", con);
SqlParameter param = new SqlParameter();
param.ParameterName = "@UserID";
param.Value = username.Text;
cmd.Parameters.Add(param);
Run Code Online (Sandbox Code Playgroud)
然后,您需要实际执行该命令并从中获取SqlDataReader.你不能只在C#代码中引用数据库中的字段,这就是你得到CS0103编译错误的原因.
SqlDataReader reader = cmd.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)
现在,您的SqlDataReader具有查询结果.因为你关心的是它是否找到了什么,你可以使用HasRows属性来检查它是否返回任何东西.
if (reader.HasRows)
{
MessageBox.Show("User Id already exists");
}
Run Code Online (Sandbox Code Playgroud)
阅读SqlDataReader - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx - 了解如何在需要时实际访问结果.
| 归档时间: |
|
| 查看次数: |
17885 次 |
| 最近记录: |