如何使用存储过程在ASP.NET中使用MSSQL中的平均值填充标签? [英] How to populate a label with average value from MSSQL in ASP.NET using stored procedures?

查看:95
本文介绍了如何使用存储过程在ASP.NET中使用MSSQL中的平均值填充标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在填写带有MSSQL数据库平均值的Label时遇到问题。

用数据库写的代码如下:

I'm having problem populating a Label with average value from MSSQL database.
The code written in database is this:

ALTER PROCEDURE [dbo].procTakeAverage

(
@ID_Person int


)
AS
	BEGIN
	SELECT AVG (RATING.Grade) from RATING
	inner join SUBJECT on SUBJECT.ID_Subject= RATING.ID_Subject
	where RATING.ID_Subject = @ID_Person and RATING.ID_RatingType=1
	END



这个st ored程序完美运行并返回平均值。

问题是如何使用ASP.NET技术(代码)在Label中显示此值。



这是我到目前为止所尝试的。



我尝试了什么:




This stored procedure works perfectly and returns the average value.
The problem is how to show this value in a Label using ASP.NET technology (code).

This is what I tried so far.

What I have tried:

<pre><pre lang="c#">

public void AverageValue()

{

string average = String.Empty;

DataTable dt = new DataTable();

SqlConnection sqlConn = new SqlConnection(这是字符串连接) ;





试试

{

sqlConn.Open(); < br $>


SqlCommand sqlCmd = new SqlCommand(procTakeAverage,sqlConn);





sqlCmd.CommandType = CommandType.StoredProcedure;





sqlCmd.Parameters.Add(@ ID_Person,SqlDbType.Int).Value = average;





sqlCmd.Parameters [@ ID_Person]。Value = Convert.ToInt32(lblID_Nxenesi.Text);



sqlCmd.ExecuteNonQuery();

average = lblAverage.Text .ToString();



sqlConn.Close();



}

catch {}



}



此代码未向我显示lblAverage的平均值。



提前感谢您的时间。

干杯。

public void AverageValue()
{
string average= String.Empty;
DataTable dt = new DataTable();
SqlConnection sqlConn = new SqlConnection("Here is the string connection");


try
{
sqlConn.Open();

SqlCommand sqlCmd = new SqlCommand("procTakeAverage", sqlConn);


sqlCmd.CommandType = CommandType.StoredProcedure;


sqlCmd.Parameters.Add("@ID_Person", SqlDbType.Int).Value = average;


sqlCmd.Parameters["@ID_Person"].Value = Convert.ToInt32(lblID_Nxenesi.Text);

sqlCmd.ExecuteNonQuery();
average = lblAverage.Text.ToString();

sqlConn.Close();

}
catch { }

}

This code is not showing me the average value in lblAverage.

Thank you in advance for your time.
Cheers.

推荐答案

为什么不在任何地方设置标签文本?



Why would it when you're not setting the label text anywhere?

sqlCmd.ExecuteNonQuery();





以上行执行SELECT但忽略结果





The above line executes the SELECT but ignores the results

average = lblAverage.Text.ToString();





这将局部变量average设置为lblAverage中的文本,但是你没有在任何地方设置该文本。您还将average作为@ID_Person参数传递,但它是一个空字符串,因此也不会执行任何操作。



如果您的SP返回单个值并且单个然后使用ExecuteScalar而不是ExecuteNonQuery来运行SP并返回该值。然后将lblAverage.Text设置为从SP返回的值,并且还需要传入正确的人员ID。



Google如何使用ExecuteScalar if你不知道怎么做。



This sets the local variable average to be the text that is in lblAverage, but you haven't set that text anywhere. You also pass "average" as the @ID_Person param but it's an empty string so that's not going to do anything either.

If your SP returns a single value and single row then use ExecuteScalar rather than ExecuteNonQuery to run the SP and return that value. You then set lblAverage.Text to be the value returned from the SP, and you also need to pass in the proper person ID.

Google for how to use ExecuteScalar if you don't know how.


我根据
F-ES Sitecore

的建议找到了解决方案。

首先,我必须使用CASTING将MSSQL中的值转换为FLOAT值。

之后在ASP.NET中使用了ExecuteScalar()方法。

再次感谢大家都答应了。

suggestions.
First I have had to use CASTING the values in MSSQL to the FLOAT Values.
Afterwards used the ExecuteScalar() method in the ASP.NET.
Once again thank you all for your reply.


这篇关于如何使用存储过程在ASP.NET中使用MSSQL中的平均值填充标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆