没有行被插入到 DataTable
No rows are being inserted into DataTable
正在尝试创建登录功能。我创建了一个 class,它将具有从 sql 查询填充数据表的功能,但它不起作用。没有错误只是没有数据插入。
这是我的登录函数class:
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;
}
}
}
}
这是我调用 class 和函数的代码:
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");
}
}
}
这是我最终的工作代码...我最终使用了一个存储过程。
{
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;
}
}
}
正在尝试创建登录功能。我创建了一个 class,它将具有从 sql 查询填充数据表的功能,但它不起作用。没有错误只是没有数据插入。
这是我的登录函数class:
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;
}
}
}
}
这是我调用 class 和函数的代码:
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");
}
}
}
这是我最终的工作代码...我最终使用了一个存储过程。
{
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;
}
}
}