我正在使用 Microsoft Visual Web Developer 和 C# based-ASP.Net 开发一个网站作为学校项目。
我已经实现了一个简单的类来表示用户,因为它是在数据库中形成的。该类具有代表 db 中的列的成员,以及一些用于从 DataTable 结果创建实例的静态方法。使用以下代码(MyDbase 是我们获得的一些帮助类):
//User.cs, placed in A
using System;
public class User
{
private int id;
private String email;
private String firstName;
private String lastName;
private String username;
private String passwd;
private String birthDate;
private bool isMale;
private String phone;
private String avatarUrl;
private bool isAdmin;
... //Accessors and mutators
public User( int _id, String _email, String _firstName, String _lastName, String _username, String _passwd,
String _birthDate, bool _isMale, String _phone, String _avatarUrl, bool _isAdmin )
{
this.id = _id;
this.email = _email;
this.firstName = _firstName;
this.lastName = _lastName;
this.username = _username;
this.passwd = _passwd;
this.birthDate = _birthDate;
this.isMale = _isMale;
this.phone = _phone;
this.avatarUrl = _avatarUrl;
this.isAdmin = _isAdmin;
}
public static User getUserForSqlResult(System.Data.DataTable result)
{
User newUser = null;
if (result.Rows.Count == 1)
{
newUser = new User((int)result.Rows[0]["id"], (String)result.Rows[0]["email"],
(String)result.Rows[0]["firstName"], (String)result.Rows[0]["lastName"], (String)result.Rows[0]["username"],
(String)result.Rows[0]["passwd"], (String)result.Rows[0]["birthDate"], (bool)result.Rows[0]["isMale"],
(String)result.Rows[0]["phone"], (String)result.Rows[0]["avatarUrl"], (bool)result.Rows[0]["isAdmin"]);
}
return newUser;
}
public static User getUserById(int id)
{
System.Data.DataTable dt = MyDbase.SelectFromTable("select * from tUsers where id=" + id, "db.mdb");
return getUserForSqlResult(dt);
}
public void saveUserChanges()
{
MyDbase.ChangeTable("UPDATE tUsers SET email='" + this.email + "', firstName='" + this.firstName + "', lastName='" + this.lastName +
"', passwd='" + this.passwd + "', birthDate='" + this.birthDate + "', phone='" + this.phone + "', avatarUrl='" +
this.avatarUrl + "' WHERE id=" + this.id , "db.mdb");
}
}
我尝试运行以下示例代码只是为了确保选择和更新功能正常工作(Default.aspx):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
User user = User.getUserById(3);
user.setEmail("different@email.com");
user.saveUserChanges();
}
}
但是,我收到以下错误:
错误 1“System.Security.Principal.IPrincipal”不包含“getUserById”的定义,并且找不到接受“System.Security.Principal.IPrincipal”类型的第一个参数的扩展方法“getUserById”(您是否缺少使用指令还是程序集引用?)
任何想法为什么会发生这种情况?天呐!