公共信息 [英] Public information
问题描述
你好,
在我的应用程序中,用户必须使用用户名和密码登录。在
我的数据库中的所有表中,都有一个字段用户。通过保存新的或
更改的记录,用户名将被写入该字段。
现在我有一个带有用户信息的课程。
通过打开程序,我在mainform上有这样的代码:
Session sess = new Session()。
sess.UserName =" John" ;;
当我打开一个新表格时,我这样做:
frmNew frm = new frmNew(sess) ;
当我写入数据库时,我这样做:
文章艺术=新文章(sess);
等
这是有效的,但我自己也在问,也许必须有另外一种方式来做这个。
我可以将信息存储在公共类或结构中,并且我可以从我的程序中的每个其他表单或类中读取这些信息。
还有另外一种方法吗?我怎么能这样做?
谢谢
Gerrit
Hallo,
In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.
Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:
Session sess = new Session().
sess.UserName = "John";
When I open a new form, then I do this:
frmNew frm = new frmNew(sess);
When I write to the Database, I do this:
Article art = new Article(sess);
etc.
This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I can
read this from every other form or class in my program.
Is there another way? And how can I do that?
thank you
Gerrit
推荐答案
您好Gerrit,
Hi Gerrit,
在我的应用程序中,用户必须使用用户名和密码登录。在
我的数据库中的所有表中,都有一个字段用户。通过保存新的或
更改的记录,用户名将被写入该字段。
In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.
您应该考虑通过创建Users表
并为每个用户分配唯一标识符(GUID或INT)来规范化数据库。然后,您可以为每个表添加一个外键约束,以便跟踪执行上一次修改的用户
。而不是存储名称
你会将用户的标识符存储在所有相关的表中。
数据库规范化
http://en.wikipedia.org/wiki/Database_normalization
现在我有一个带有用户信息的类会话。
通过打开程序,我在mainform上有这样的代码: br />
会话sess =新会话()。
sess.UserName =" John";
当我打开一个新表格然后我这样做:
frmNew frm = new frmNew(sess);
当我写入数据库时,我这样做:
文章艺术=新文章(sess);
等
这是有效的,但我自己也在问,也许必须有另外一种方法来实现这一点。
我可以将信息存储在公共场所类或结构,我可以从我的程序中的每个其他表单或类中读取这个。
还有其他方法吗?我怎么能这样做?
Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:
Session sess = new Session().
sess.UserName = "John";
When I open a new form, then I do this:
frmNew frm = new frmNew(sess);
When I write to the Database, I do this:
Article art = new Article(sess);
etc.
This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.
Is there another way? And how can I do that?
是的,您可以向业务对象添加静态成员:
class用户
{
private User currentUser;
public static User CurrentUser {get {return currentUser; } $
}
确定用户设置属性后,例如:
用户user =新用户(" Gerrit");
User.CurrentUser = user;
当你需要在另一个业务对象中访问它时,只需阅读
属性:
class Article
{
public void保存()
{
用户currentUser = User.CurrentUser;
... < br $>
}
}
-
Dave Sexton
Yes, you can add a static member to your business object:
class User
{
private User currentUser;
public static User CurrentUser { get { return currentUser; } }
...
}
After you identify the user set the property, for example:
User user = new User("Gerrit");
User.CurrentUser = user;
And when you need to access it in another business object just read the
property:
class Article
{
public void Save()
{
User currentUser = User.CurrentUser;
...
}
}
--
Dave Sexton
解决方案取决于。解决这个问题的一种方法是创建一个
GenericIdentity并设置当前主体:
System.Security.Principal.IIdentity identity = new
System.Security.Principal.GenericIdentity(" USERNAM E");
System.Threading.Thread.CurrentPrincipal = new
System.Security.Principal.GenericPrincipal(identit y,新字符串[0]);
然后,您的代码中的任何位置都可以获得USERNAME如下:
System.Threading.Thread.CurrentPrincipal.Identity。名称
如果您正在使用SQL Server身份验证,您可以让数据库设置
每个插入的用户字段,更新...使用触发器。 />
Gabriel Lozano-Morán
" Gerrit" < gs ************* @ hotmail.comwrote in message
news:c1 ***************** **********@news.chello.nl。 ..
The solution depends. One way you could solve this is by creating a
GenericIdentity and setting the current principal:
System.Security.Principal.IIdentity identity = new
System.Security.Principal.GenericIdentity("USERNAM E");
System.Threading.Thread.CurrentPrincipal = new
System.Security.Principal.GenericPrincipal(identit y, new string[0]);
Then anywhere in your code you can get the "USERNAME" as follows:
System.Threading.Thread.CurrentPrincipal.Identity. Name
If you are using SQL Server authentication you could let the database set
the user field with every insert, update ... using triggers.
Gabriel Lozano-Morán
"Gerrit" <gs*************@hotmail.comwrote in message
news:c1***************************@news.chello.nl. ..
你好,
在我的应用程序中,用户必须使用用户名和密码登录。在
我的数据库中的所有表中,都有一个字段用户。通过保存新的或
更改的记录,用户名将被写入该字段。
现在我有一个带有用户信息的课程。
通过打开程序,我在mainform上有这样的代码:
Session sess = new Session()。
sess.UserName =" John" ;;
当我打开一个新表格时,我这样做:
frmNew frm = new frmNew(sess) ;
当我写入数据库时,我这样做:
文章艺术=新文章(sess);
等
这是有效的,但我自己也在问,也许必须有另外一种方式来做这个。
我可以将信息存储在公共类或结构中,并且我可以从我的程序中的每个其他表单或类中读取此信息。
还有另外一种方法吗?我怎么能这样做?
谢谢
Gerrit
Hallo,
In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.
Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:
Session sess = new Session().
sess.UserName = "John";
When I open a new form, then I do this:
frmNew frm = new frmNew(sess);
When I write to the Database, I do this:
Article art = new Article(sess);
etc.
This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.
Is there another way? And how can I do that?
thank you
Gerrit
你好Gerrit,
我的代码示例中的更正:
Hi Gerrit,
Correction in my code sample:
public static User CurrentUser {get {return currentUser; }
public static User CurrentUser { get { return currentUser; } }
public static User CurrentUser {
get {return currentUser; }
set {currentUser = value; } //写访问需要
}
-
Dave Sexton
Dave Sexton < dave @jwa [remove.this] online.comwrote in message
news:OA ************* @ TK2MSFTNGP06.phx.gbl ...
public static User CurrentUser {
get { return currentUser; }
set { currentUser = value; } // required for write-access
}
--
Dave Sexton
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
您好Gerrit,
Hi Gerrit,
>在我的应用程序中,用户必须使用用户名和密码登录。在我的数据库中的所有表中,有一个字段用户。通过保存新的或
更改的记录,用户名将被写入该字段。
>In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.
您应该考虑通过创建Users表
并为每个用户分配唯一标识符(GUID或INT)来规范化数据库。然后,你
可以为你要保留的每个表添加一个外键约束
跟踪执行上一次修改的用户。而不是
存储一个名称,你将把用户的标识符存储在所有相关的
表中。
数据库规范化
http://en.wikipedia.org/ wiki / Database_normalization
>现在我有一个带有用户信息的课程。
通过打开程序,我有类似的代码这个在主体上:
会话sess =新会话()。
sess.UserName =" John" ;;
当我打开一个新表格时,那么我这样做:
frmNew frm = new frmNew(sess);
当我写入数据库时,我这样做:
文章艺术=新文章(sess);
这是有效的,但我自己也在问,也许必须有另外一种方法可以做到这一点。
我可以将信息存储在公共类或结构中,并且我可以从中读取此信息我的课程中的其他形式或课程。
还有其他办法吗?我怎么能这样做?
>Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:
Session sess = new Session().
sess.UserName = "John";
When I open a new form, then I do this:
frmNew frm = new frmNew(sess);
When I write to the Database, I do this:
Article art = new Article(sess);
etc.
This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.
Is there another way? And how can I do that?
是的,您可以向业务对象添加静态成员:
class用户
{
private User currentUser;
public static User CurrentUser {get {return currentUser; } $
}
确定用户设置属性后,例如:
用户user =新用户(" Gerrit");
User.CurrentUser = user;
当你需要在另一个业务对象中访问它时,只需阅读
属性:
class Article
{
public void保存()
{
用户currentUser = User.CurrentUser;
... < br $>
}
}
-
Dave Sexton
Yes, you can add a static member to your business object:
class User
{
private User currentUser;
public static User CurrentUser { get { return currentUser; } }
...
}
After you identify the user set the property, for example:
User user = new User("Gerrit");
User.CurrentUser = user;
And when you need to access it in another business object just read the
property:
class Article
{
public void Save()
{
User currentUser = User.CurrentUser;
...
}
}
--
Dave Sexton
这篇关于公共信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!