将参数打印到c#winform
我正在用c语言开发winform应用程序,使用3层架构。我需要在winform消息中打印sql server错误(@ErrorPrint)。我试过好几种方法,但都得不到结果。我的代码是。。。 SQL SERVER过程作为 我的数据逻辑层是这样的 类似的表示代码如下 私有无效按钮1\u单击(对象发送者,事件参数e) { 我需要从WinForms打印SP@ErrorPrint将参数打印到c#winform,c#,sql-server,3-tier,C#,Sql Server,3 Tier,我正在用c语言开发winform应用程序,使用3层架构。我需要在winform消息中打印sql server错误(@ErrorPrint)。我试过好几种方法,但都得不到结果。我的代码是。。。 SQL SERVER过程作为 我的数据逻辑层是这样的 类似的表示代码如下 私有无效按钮1\u单击(对象发送者,事件参数e) { 我需要从WinForms打印SP@ErrorPrint 使用ExecuteNonQuery而不是ExecuteScalar,并在执行查询后检索输出参数值: cmd.Paramete
使用
ExecuteNonQuery
而不是ExecuteScalar
,并在执行查询后检索输出参数值:
cmd.Parameters["@ErrorPrint"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string message = cmd.Parameters["@ErrorPrint"].Value.ToString();
connection.Close();
return message;
请避免使用表示系统存储过程的sp
存储过程名称前缀。请参阅
public string login_details(Login_Entity Users)
{
SqlConnection connection = new SqlConnection(conn);
connection.Open();
SqlCommand cmd = new SqlCommand("sp_CheckLogin", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@userName", Users.UserName_Details);
cmd.Parameters.AddWithValue("@PassCode", Users.Password_Details);
cmd.Parameters.Add("@ErrorPrint", SqlDbType.Char, 500);
cmd.Parameters["@ErrorPrint"].Direction = ParameterDirection.Output;
string message = cmd.Parameters["@ErrorPrint"].Value.ToString();
cmd.ExecuteScalar().ToString();
connection.Close();
return message;
}
if (txtUserName.Text == "" && txtPassword.Text == "")
{
lblError.Text = ("UserName and Password is Empty");
txtUserName.Focus();
}
else if (txtUserName.Text == "")
{
lblError.Text = ("UserName is Empty");
txtUserName.Focus();
}
else if (txtPassword.Text == "")
{
lblError.Text = ("Password is Empty");
txtPassword.Focus();
}
else
{
Login_Entity LE = new Login_Entity();
Login_BL LB = new Login_BL();
LE.UserName_Details = txtUserName.Text;
LE.Password_Details = txtPassword.Text;
try
{
string _login = LB.Login_BLL(LE);
int i = int.Parse(_login);
if (i > 0)
{
Home hm = new Home();
hm.Show();
username = txtUserName.Text;
hm.lblusername.Text = username;
this.Hide();
}
else
{
lblError.Text = ("UserName/Pasword Error");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
LB = null;
}
}
}
cmd.Parameters["@ErrorPrint"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string message = cmd.Parameters["@ErrorPrint"].Value.ToString();
connection.Close();
return message;