我的设计习惯是让子窗体填充 SqlConnection 对象,显示子窗体,建立 SqlConnection 对象并将填充的对象传递回继续执行的父窗体。.NET 2.0 及更高版本 0 如下:
父表格:
public SqlConnection sqlcon; //should be property but made it public for this example
public MainForm_Load(object sender, EventArgs e)
{
Login frm = new Login(this);
frm.ShowDialog();
if (this.sqlcon == null)
{ //no login
this.Close();
}
//else, continue execution
}
登录表格:
Mainform parent;
public Login(RandomParade frm)
{
InitializeComponent();
parent = frm;
}
private void btnOK_Click(object sender, EventArgs e)
{
//code to capture login details from textboxes
//test connection
parent.sqlcon = new SqlConnection("connection string with login details here");
this.Close();
}
由于这被编译成单个可执行文件(dll 访问修饰符被单独处理等......),让 sqlConnection 对象公开存在安全风险吗?有人可以在运行时获取信息吗?将登录信息传递给“主”表单的更好实施方式是什么?