我正在尝试将用户添加到两个不同的表,从SQL Server Management Studio Express运行,仅为一个会话添加详细信息,请人们帮助解答如何将此数据用于最后会话结束后的数据库(我为糟糕的代码道歉,我目前只是一名学徒软件工程师)
namespace AddingANewUser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void AddNewUser(String employeeID, String firstName, String lastName, String username, String password, String managerID, String accessLevelID, String phoneNumber, String departmentID)
{
using (var connection = new SqlConnection(Properties.Settings.Default.CitySecretHRSystemConnectionString))
{
connection.Open();
using (var updateEmployees = connection.CreateCommand())
{
updateEmployees.CommandText = "INSERT INTO Employees (EmployeeID, FirstName, LastName, PhoneNumber, AccessLevelID, ManagerID, DepartmentID) VALUES (@employeeID, @firstName, @lastName, @phoneNumber, @ accessLevelID, @managerID, @departmentID)";
updateEmployees.Parameters.AddWithValue("@firstName", firstName);
updateEmployees.Parameters.AddWithValue("@lastName", lastName);
updateEmployees.Parameters.AddWithValue("@employeeID", employeeID);
updateEmployees.Parameters.AddWithValue("@phoneNumber", phoneNumber);
updateEmployees.Parameters.AddWithValue("@accessLevelID", accessLevelID);
updateEmployees.Parameters.AddWithValue("@managerID", managerID);
updateEmployees.Parameters.AddWithValue("@departmentID", departmentID);
}
using (var updateLogIn = connection.CreateCommand())
{
updateLogIn.CommandText = "INSERT INTO LogInDetails (EmployeeID, Username, KeyWord) VALUES (@employeeID, @username, @keyWord)";
updateLogIn.Parameters.AddWithValue("@employeeID", employeeID);
updateLogIn.Parameters.AddWithValue("@username", username);
updateLogIn.Parameters.AddWithValue("@keyWord", password);
}
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
AddNewUser(tbEmployeeID.Text, tbFirstName.Text, tbLastName.Text, tbUsername.Text, tbPassword.Text, tbManagerID.Text, tbAccessLevel.Text, tbPhoneNumber.Text, tbDeptID.Text);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
答案 0 :(得分:5)
我猜你错过了execute命令:
using (var updateEmployees = connection.CreateCommand())
{
//...
//...
updateEmployees.ExecuteNonQuery();
//...
}
正如其他人正确指出的那样:
using语句以正确的方式调用对象上的Dispose方法,并且(如前所示使用它时) 一旦调用Dispose,它也会导致对象本身超出范围。
简单来说,不需要调用connection.Close()
作为using
来处理它并在完成后关闭连接。
答案 1 :(得分:3)
你必须使用下面提到的代码
using (var connection = new SqlConnection(Properties.Settings.Default.CitySecretHRSystemConnectionString))
{
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var updateEmployees = connection.CreateCommand())
{
updateEmployees.CommandText = "INSERT INTO Employees (EmployeeID, FirstName, LastName, PhoneNumber, AccessLevelID, ManagerID, DepartmentID) VALUES (@employeeID, @firstName, @lastName, @phoneNumber, @ accessLevelID, @managerID, @departmentID)";
updateEmployees.Parameters.AddWithValue("@firstName", firstName);
updateEmployees.Parameters.AddWithValue("@lastName", lastName);
updateEmployees.Parameters.AddWithValue("@employeeID", employeeID);
updateEmployees.Parameters.AddWithValue("@phoneNumber", phoneNumber);
updateEmployees.Parameters.AddWithValue("@accessLevel", accessLevel);
updateEmployees.Parameters.AddWithValue("@managerID", managerID);
updateEmployees.Parameters.AddWithValue("@departmentID", departmentID);
updateEmployees.ExecuteNonQuery();
}
using (var updateLogIn = connection.CreateCommand())
{
updateLogIn.CommandText = "INSERT INTO LogInDetails (EmployeeID, Username, KeyWord) VALUES (@employeeID, @username, @keyWord)";
updateLogIn.Parameters.AddWithValue("@employeeID", employeeID);
updateLogIn.Parameters.AddWithValue("@username", username);
updateLogIn.Parameters.AddWithValue("@keyWord", password);
updateLogIn.ExecuteNonQuery();
}
}
您已经实施了using
阻止,因此无需关闭连接
答案 2 :(得分:-1)
您已撰写@phoneNumber, @ accessLevelID, @managerID,
:@
和accessLevel
之间没有空格。此外,一旦您拨打accessLevel
和其他时间accessLevelID
,我认为accessLevel没有ID ......