没有行插入DataTable

问题描述 投票:0回答:1

尝试创建登录功能。我创建了一个类,它具有从sql查询填充数据表的功能,但它不起作用。没有错误只是没有数据插入。

这是我的登录功能类:

namespace GymCalculator
{
    public class LoginFunction
    {
        public DataTable Login (string username, string pword)
        {
            using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionHelper.CnnVal("GymDB"))) {
                string query = ("Select * from [USER] where username = '{username}'  and password = '{pword}'");
                SqlDataAdapter sda = new SqlDataAdapter(query, connection);
                DataTable dtbl = new DataTable();
                sda.Fill(dtbl);
                return dtbl;
            }
        }
    }
}

这是我调用类和函数的代码:

public partial class Login : Form
{
    public Login()
    {
        InitializeComponent();
    }

    private void Loginbtn_Click(object sender, EventArgs e)
    {
        var LoginFunction = new LoginFunction();
        var DataTable = new DataTable();

        DataTable = LoginFunction.Login(Usernametxt.Text, Passwordtxt.Text);
        if (DataTable.Rows.Count == 1) {
            CalculatorMain calculatorMain = new CalculatorMain();
            this.Hide();
            calculatorMain.Show();
        } else {
            MessageBox.Show("You entered the wrong username or password");
        }
    }
 }
c# sql class login datatable
1个回答
0
投票

这是我最后的工作代码......我最终使用了一个存储过程。

{
public class LoginFunction
{
    public DataTable Login (string username, string pword)
    {
        using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionHelper.CnnVal("GymDB")))

        {


            SqlCommand cmd = new SqlCommand("LoginUser", connection)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@USERNAME", username);
            cmd.Parameters.AddWithValue("@PASSWORD", pword);

            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dtbl = new DataTable();
            sda.Fill(dtbl);
            return dtbl;



        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.