我正在尝试将经过身份验证的用户重定向到另一个c#表单。
用户使用他的用户名和密码进行身份验证后,他将被发送到另一个表单。遗憾的是,我无法从旧的表单元素中访问旧参数。
我的代码如下所示:
表格1:
using MaterialSkin;
using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Helpful
{
public partial class Form1 : MaterialForm
{
public Form1()
{
InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
private void materialRaisedButton1_Click(object sender, EventArgs e)
{
try
{
string username = materialSingleLineTextField1.Text;
string password = materialSingleLineTextField2.Text;
database_connector dbConnect = new database_connector();
bool db_response = dbConnect.user_check(username, password);
if (db_response == true)
{
MessageBox.Show("User authentificated.");
new Form2().Show();
this.Hide();
}
else
{
MessageBox.Show("Please try again, wrong user credentials.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
表格2:
using MaterialSkin;
using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Helpful
{
public partial class Form2 : MaterialForm
{
public Form2()
{
InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
}
}
database_connector类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Helpful
{
class database_connector
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
// Constructor
public database_connector()
{
Initialize();
}
//Initialize values
private void Initialize()
{
server = "xxx";
database = "xxx";
uid = "xxx";
password = "xxx";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator.");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
// Select statement
public bool user_check(string username, string password)
{
string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'";
bool hasRecords = false;
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
if (dataReader.HasRows)
{
while (dataReader.Read())
{
hasRecords = true;
}
}
dataReader.Close();
this.CloseConnection();
}
return hasRecords;
}
}
}
我的问题是,如果没有用户再次输入,我怎么能在表格2中使用变量用户名?
我会感激任何帮助。
答案 0 :(得分:2)
为了达到你想要的效果,按照POO和图层编程,你必须创建一个保存数据的类。
通过以下示例,您可以在第一个屏幕中设置参数,并且可以从任何其他可以看到此类的表单访问它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class User
{
static private string cdUser;
static private int cdAcess;
static private string nmUser;
public static string CdUser
{
get
{
return cdUser;
}
set
{
cdUser = value;
}
}
public static int CdAcess
{
get
{
return cdAcess;
}
set
{
cdAcess = value;
}
}
public static string NmUser
{
get
{
return nmUser;
}
set
{
nmUser = value;
}
}
}
}
要保存数据,您可以执行以下操作:
User.CdUser = _login;
User.CdAcess = Convert.ToInt32(rdr["acess"].ToString());
User.NmUser = rdr["name"].ToString();
答案 1 :(得分:0)
为Form2创建一个很好地获取名称的构造函数,或者在Form2中创建一个包含它并设置它的字段。
//first approach
new Form2(username).show();
//second approach
var f2 = Form2();
f2.username = username;
f2.show()