gpt4 book ai didi

c# - 如何在向导事件步骤中执行 SQL 插入?

转载 作者:行者123 更新时间:2023-11-30 22:11:36 25 4
gpt4 key购买 nike

asp:Wizard 的第一步,我使用 DirectoryServices 进行登录以进行身份​​验证。但后来我想获取 UserIDDateSCOPE_IDENTITY() 并将其插入到表中。这是我试过的。当我点击下一步时,信息没有插入,但 AD 功能被正确检查。我不确定我做错了什么

protected void OnActiveStepChanged(object sender, EventArgs e)
{
//check for the employee in AD
string Domain = "mydomain.local";
string EmployeeID = txtEmpID.Text;
string Password = txtPassword.Text;
string ADStatus = null;

// If the ActiveStep is changing to Step2, check to see whether the
// user authenticated the AD Login Process. If it is, skip to the Step2 step.
if (Wizard1.ActiveStepIndex == Wizard1.WizardSteps.IndexOf(this.WizardStep2))
{
if (AuthenticateActiveDirectory(Domain, EmployeeID, Password) == true)
{
//If success ...
ADStatus = "Success";
Session["SessionADStatus"] = ADStatus;


string strDepartment = ddlDepartment.SelectedValue;
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
SqlCommand cmd1 = new SqlCommand("INSERT INTO [pharm_OrderID](UserID, RequestType, CreateDate) values (@UserID, @RequestType, @CreateDate);", conn1);
cmd1.CommandType = CommandType.Text;
conn1.Open();

string strUserID = txtEmpID.Text;
cmd1.Parameters.Add("@UserID", SqlDbType.NVarChar, 50);
cmd1.Parameters["@UserID"].Value = strUserID;

string strRequestType = ddlReturnType.SelectedValue;
cmd1.Parameters.Add("@ReturnType", SqlDbType.NVarChar, 50);
cmd1.Parameters["@ReturnType"].Value = strRequestType;

string strCreateDate = lblOrderAttemptTime.Text;
cmd1.Parameters.Add("@CreateDate", SqlDbType.NVarChar, 50);
cmd1.Parameters["@CreateDate"].Value = strCreateDate;

conn1.Dispose();
cmd1.Dispose();
Wizard1.ActiveStepIndex = Wizard1.WizardSteps.IndexOf(this.WizardStep2);

}
else
{
ADStatus = "Failure";
Session["SessionADStatus"] = ADStatus;
lblADError.Visible = true;
lblADError.Text = "Unable to authenticate Employee ID or Password.";
Wizard1.ActiveStepIndex = Wizard1.WizardSteps.IndexOf(this.WizardStep1);
}


}

}

最佳答案

我不是AD专家,但需要执行命令才能产生任何结果。

尝试添加

 cmd1.ExecuteNonQuery(); 

在配置连接和命令之前

using(SqlConnection conn1 = new SqlConnection(........))
using(SqlCommand cmd1 = new SqlCommand("INSERT INTO [pharm_OrderID]" +
"(UserID, RequestType, CreateDate) " +
"values (@UserID, @RequestType, @CreateDate);", conn1))
{
conn1.Open();
string strUserID = txtEmpID.Text;
cmd1.Parameters.Add("@UserID", SqlDbType.NVarChar, 50);
cmd1.Parameters["@UserID"].Value = strUserID;

string strRequestType = ddlReturnType.SelectedValue;
cmd1.Parameters.Add("@ReturnType", SqlDbType.NVarChar, 50);
cmd1.Parameters["@ReturnType"].Value = strRequestType;

string strCreateDate = lblOrderAttemptTime.Text;
cmd1.Parameters.Add("@CreateDate", SqlDbType.NVarChar, 50);
cmd1.Parameters["@CreateDate"].Value = strCreateDate;

cmd1.ExecuteNonQuery();
}
Wizard1.ActiveStepIndex = Wizard1.WizardSteps.IndexOf(this.WizardStep2);
...

还添加了 using 语句来关闭连接并处理命令和连接。
您应该始终使用此模式正确关闭连接,即使在 using block 内抛出异常也是如此

关于c# - 如何在向导事件步骤中执行 SQL 插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20155297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com