@sql server中未提供的输出

本文关键字:输出 server @sql | 更新日期: 2023-09-27 18:24:35

C#代码:

protected void btnsearch_Click(object sender, EventArgs e)
{
    SqlConnection con = Connection.DBconnection();   
    SqlCommand com = new SqlCommand("sp_studentresult", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@id", textstudentid.Text);
    SqlDataAdapter adp = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {                   
        txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
        txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
        txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
        txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
        txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();                     
    }
    SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50);
    retval.Direction = ParameterDirection.Output;
    com.Parameters.Add(retval);
    com.ExecuteNonQuery();
    string Output = retval.Value.ToString();   
}

存储过程:

    ALTER PROCEDURE sp_studentresult
(
    @id int,
    @output varchar(50) output,
    @id_student varchar(50)
)
AS
begin
SELECT * from studentresult where id_student=@id
End
IF EXISTS (SELECT * FROM student WHERE id=@id_student)
BEGIN
SET @output='EXIST'
END

我是.net的新手。当我输入学生id并搜索时,我会得到

过程或函数sp_studentsort指定的参数太多。

我可以知道我在上面的代码中犯了什么错误吗?

如有任何帮助,我们将不胜感激。

谢谢,

@sql server中未提供的输出

我打赌,您正试图返回一个sql结果集和一个输出参数,对吧?如果是,请尝试以下代码:

protected void btnsearch_Click(object sender, EventArgs e)
{
    SqlConnection con = Connection.DBconnection();
    SqlCommand com = new SqlCommand("sp_studentresult", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@id", textstudentid.Text);
    com.Parameters.AddWithValue("@id_student", textIDStudent.Text);
    SqlParameter retval2 = new SqlParameter("@output", SqlDbType.VarChar, 50);
    retval2.Direction = ParameterDirection.Output;
    com.Parameters.Add(retval2);
    SqlDataAdapter adp = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
        txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
        txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
        txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
        txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
    }
    SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50);
    retval.Direction = ParameterDirection.Output;
    com.Parameters.Add(retval);
    com.Parameters.AddWithValue("@id", textstudentid.Text);
    com.Parameters.AddWithValue("@id_student", textIDStudent.Text);
    com.ExecuteNonQuery();
    string Output = retval.Value.ToString();
}

存储过程的名称不同。

  SqlCommand com = new SqlCommand("sp_studentresult", con);

这里您指的是"sp_studentsort"。您插入的存储过程代码是

ALTER PROCEDURE sp_searchupdate

显然,这些程序的参数列表应该有所不同。

这是因为存储过程没有获得所有参数的值,检查从SqlCommand传递给SP的参数数。如果可能,请更新存储过程

您缺少必须提供的参数id_student。只有标记为Output的参数才不需要提供。像添加@id一样添加它例如。com.Parameters.AddWithValue("@id",Value);

我不确定它的值是多少,所以你可以用正确的值来代替变量value