【问题标题】:c# : How to get data from database and pass to another form?c#:如何从数据库中获取数据并传递给另一个表单?
【发布时间】:2016-10-02 06:07:19
【问题描述】:

我正在构建一个桌面应用程序,当用户在新登录时,他的 ID 将出现在文本框中。但在我的情况下,查询运行成功,但 id 没有出现在 textBox.. 谁能帮我找出来吗?

用户登录的第一种形式(Form1.cs)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace EmployeeApp
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        public string employeeID;
        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void loginButton_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=INCEPSYS-SE\TEST;Initial Catalog=Employee;Integrated Security=True");
            connection.Open();
            String query = "select * from Employees where Name = '" + nameTextBox.Text + " ' and Password = '" + passwordTextBox.Text + "'";

            SqlCommand command = new SqlCommand(query, connection);
            SqlDataReader myReader = command.ExecuteReader();
            while (myReader.Read())
            {
                string employeeID = myReader["EmployeeID"].ToString();
            }
            myReader.Close();
            SqlDataAdapter sda  = new SqlDataAdapter(query,connection);            
            connection.Close();
            DataTable dt = new DataTable();
            sda.Fill(dt);
                if (dt.Rows.Count == 1)
            {
                this.Hide();
                Entry ss = new Entry(employeeID);
                ss.Show();
            }
            else
            {
                MessageBox.Show("Please Check your Username & password");
            }
        }

    }
}

第二种形式(Entry.cs)

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 EmployeeApp
{
    public partial class Entry : Form
    {
        public Entry()
        {
            InitializeComponent();
        }
        public Entry(string employeeId)
        {
              InitializeComponent();
              idTextBox.Text = employeeId;
        }


       private void reportButton_Click(object sender, EventArgs e)
        {
            Report report = new Report();
            report.Show();
        }
    }
}

【问题讨论】:

    标签: c# winforms desktop-application


    【解决方案1】:

    删除局部变量声明,因为employeeID 是一个全局变量并且已经首先声明,所以当您使用string 为其添加前缀时,它会创建另一个在此范围之外无法访问的局部变量

    while (myReader.Read())
      {
          employeeID = myReader["EmployeeID"].ToString();
      }
    

    【讨论】:

      【解决方案2】:

      你有一个局部变量。您可以像这样更正和优化您的代码:

          private void loginButton_Click(object sender, EventArgs e)
          {
              //If use set quote into your textbox
              string name = nameTextBox.Text.Replace("'", "''");
              string pass = passwordTextBox.Text.Replace("'", "''");
      
              String query = string.Format("select * from Employees where Name = '{0}' and Password = '{1}'", name, pass);
              string employeeID = "";
      
      
              using (SqlConnection connection = new SqlConnection(@"Data Source=INCEPSYS-SE\TEST;Initial Catalog=Employee;Integrated Security=True"))
              {
                  connection.Open();
      
                  using (SqlDataAdapter sda = new SqlDataAdapter(query, connection))
                  {
                      DataTable dt = new DataTable();
                      sda.Fill(dt);
      
                      if (dt.Rows.Count > 0)
                      {
                          employeeID = dt.Rows[0]["EmployeeID"].ToString();
                          this.Hide();
                          Entry ss = new Entry(employeeID);
                          ss.Show();
                      }
                      else
                      {
                          MessageBox.Show("Please Check your Username & password");
                      }
      
                      dt.Dispose();
      
                  }
              }
      
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        • 2020-03-22
        • 2013-03-17
        • 1970-01-01
        • 2014-03-18
        • 2017-06-01
        • 1970-01-01
        相关资源
        最近更新 更多