-2
我有一個程序,用戶必須登錄,所以他們有一個ID和密碼。具有用戶登錄詳細信息的數據庫存儲在本地.mdf文件中。顯示已登錄的用戶信息
我想設置它,以便在用戶登錄時,程序的其餘部分在右上角顯示他們的詳細信息,例如他們的名字和他們的ID。
不幸的是,我不知道如何做到這一點,而我瀏覽過的所有人都使用實際的系統登錄,這不是我想要的。
代碼登錄表單:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace InventoryManager
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void frmLogin_Load(object sender, EventArgs e)
{
this.AcceptButton = btnSubmit;
}
string cs = @"Data Source= (LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Users.mdf;Integrated Security = True;";
private void btnSubmit_Click(object sender, EventArgs e)
{
if (txtUserID.Text == "" || txtPassword.Text == "")
{
MessageBox.Show("Please enter a User ID and Password");
return;
}
try
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_Login WHERE UserID = @userid AND Password = @password", con);
cmd.Parameters.AddWithValue("@userid", txtUserID.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
if (count == 1)
{
MessageBox.Show("Login Successful!");
this.Hide();
frmOverview fo = new frmOverview();
fo.Show();
}
else
{
MessageBox.Show("Login Failed");
txtPassword.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
建議的意見:在將其添加到查詢之前對輸入進行清理。 –