Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用access数据库将用户登录数据从form1传递到form2 listview_C#_Ms Access_Visual Studio 2015 - Fatal编程技术网

C# 使用access数据库将用户登录数据从form1传递到form2 listview

C# 使用access数据库将用户登录数据从form1传递到form2 listview,c#,ms-access,visual-studio-2015,C#,Ms Access,Visual Studio 2015,我创建了一个程序,需要将员工编号登录数据(登录)传递到第二个表单(form1)。我已经创建了由3个表组成的数据库:Employee\u Information、SN\u Incoming和Duplicate。我希望在员工信息中找到的员工编号移到SNU Incoming表中。然后,该数据将显示在列表视图中 登录表单代码: using System; using System.Collections.Generic; using System.ComponentModel; using System

我创建了一个程序,需要将员工编号登录数据(登录)传递到第二个表单(form1)。我已经创建了由3个表组成的数据库:Employee\u Information、SN\u Incoming和Duplicate。我希望在员工信息中找到的员工编号移到SNU Incoming表中。然后,该数据将显示在列表视图中

登录表单代码:

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 System.Data.OleDb;

namespace Serial_Number_Checker
{
public partial class Log_In : Form
{

    public Log_In()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(textBox1.Text))
        {
            MessageBox.Show("Please enter your employee #!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            textBox1.Focus();
            return;
        }
        try
        {
            Duplicate_Serial_Number_CheckerDataSetTableAdapters.Employee_InformationTableAdapter Employee_Number = new Duplicate_Serial_Number_CheckerDataSetTableAdapters.Employee_InformationTableAdapter();
            Duplicate_Serial_Number_CheckerDataSet.Employee_InformationDataTable dt = Employee_Number.GetDataByEmployee_Number(textBox1.Text);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); // Process your login here
                this.Hide();
                Form1 form1 = new Form1();
                if (form1.ShowDialog() != DialogResult.OK)
                Application.Exit();
            }
            else
            {
                MessageBox.Show("Your Employee # is not registered. Please contact your Supervisor to be registered.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
            button1.PerformClick();
    }
}
}

表格1代码:

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 System.Configuration;
using System.Data.OleDb;

namespace Serial_Number_Checker
{
public partial class Form1 : Form
{
    static string conString= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=";
    OleDbConnection con = new OleDbConnection(conString);
    OleDbCommand cmd;
    OleDbDataAdapter adapter;
    DataTable dt = new DataTable();

    public Form1()
    {
        InitializeComponent();

        listView1.SelectedIndexChanged += new EventHandler        (listView1_SelectedIndexChanged); // Adding columns to listView1

        // list view properities
        listView1.View = View.Details;
        listView1.FullRowSelect = true;

        // Add Columns
        listView1.Columns.Add("Employee #", 150);
        listView1.Columns.Add("Serial Number", 150);
        listView1.Columns.Add("Date/Time", 150);            
    }







    // Add Row
    private void InsertSerialNumber(string sn)
    {
        using (OleDbCommand odc = new OleDbCommand("INSERT INTO SN_Incoming (SN) VALUES(@SN)", con))
        {
            odc.Parameters.AddWithValue("@SN", sn);
            try
            {
                con.Open();
                odc.ExecuteNonQuery();
            }
            catch (Exception e) { MessageBox.Show(e.Message);
            }
            finally { con.Close();
            }
        }
    }





    // Add To ListView1
    private void populate(String employeeid, String sn, String timestamp)
    {
        // Row
        String[] row = { employeeid, sn, timestamp };

        ListViewItem item = new ListViewItem(row);

        listView1.Items.Add(item);
    }





    private void clearTxts()
    {
        textBox1.Text = "";
    }



    // Retrieve Check In
    private void LoadSerialNumbers() {
    listView1.Items.Clear();
    DataTable dt = new DataTable();
    using (OleDbCommand odc = new OleDbCommand("SELECT * FROM SN_Incoming", con)) {
    try {
        con.Open();
        using (OleDbDataAdapter oda = new OleDbDataAdapter(odc))
            oda.Fill(dt);
    } catch (Exception e) { MessageBox.Show(e.Message); } finally { con.Close(); }
}

    List<ListViewItem> items = new List<ListViewItem>();
    foreach (DataRow row in dt.Rows) {
    ListViewItem lvi = items.SingleOrDefault(s => s.Tag == row[1].ToString());
    if (lvi != null)
        continue;

    lvi = new ListViewItem(new string[] { row[0].ToString(), row[1].ToString(), row[2].ToString() });
    lvi.Tag = row[1].ToString();
    items.Add(lvi);
}

listView1.Items.AddRange(items.ToArray());
}


我已经将我的主键设置为Employee_Number。我需要学习如何将这些信息传递到下一个表。因此,我可以检索并放入我的listview。非常感谢你的帮助。谢谢

只需在
表单1的构造函数中添加一个参数

public Form1(string id)
{
    InitializeComponent();
    //Do whatever you like with the passed-in id:
    MessageBox.Show($"you passed me Id {id}");
}         
使用:

Form1 form1 = new Form1(TextBox1.Text);
if (form1.ShowDialog() != DialogResult.OK)
    Application.Exit();
尽管一般来说,我还是建议将所有数据库内容和“当前登录的用户”代码移动到单独的存储库/服务类中,这些类在任何地方都可以使用

Form1 form1 = new Form1(TextBox1.Text);
if (form1.ShowDialog() != DialogResult.OK)
    Application.Exit();