必须声明标量变量'@站点名称';

本文关键字:站点名 #39@ 声明 标量 变量 | 更新日期: 2023-09-27 18:25:21

我必须在网页上绑定并显示asp.net图表。运行此程序时出错。请帮帮我。代码如下:

public partial class chartDummy : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"connectionString");
        cmd = new SqlCommand("select top (1)  Dg_energy_daily, Load_energy_daily, mains_energy_daily,solar_energy_daily  From tbl_energy_report inner join tbl_site_details ON tbl_energy_report.Site_ID=tbl_site_details.site_id where tbl_site_details.site_name=@site_name order by tbl_energy_report.sl_no desc", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        DataView source = new DataView(ds.Tables[0]);
        Chart1.DataSource = ds;
        Chart1.DataBind();

来源:

<asp:Chart ID="Chart1" runat="server" CssClass="auto-style1" Width="987px" Height="276px">
<Series>
<asp:Series Name="Dg_energy_daily" XValueMember="site_name" YValueMembers="Dg_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
<asp:Series Name="Load_energy_daily" XValueMember="site_name" YValueMembers="Load_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
<asp:Series Name="mains_energy_daily" XValueMember="site_name" YValueMembers="mains_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
<asp:Series Name="solar_energy_daily" XValueMember="site_name" YValueMembers="solar_energy_daily"    ChartArea="ChartArea1" Legend="Legend1" ></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Title="RunTime Report" Name="Legend1"></asp:Legend>
</Legends> 
</asp:Chart>

必须声明标量变量'@站点名称';

基本上,您必须将参数及其相应值添加到查询中:

string value=///set the value you want
...
 cmd = new SqlCommand("select top (1)  Dg_energy_daily, Load_energy_daily, mains_energy_daily,solar_energy_daily  From tbl_energy_report inner join tbl_site_details ON tbl_energy_report.Site_ID=tbl_site_details.site_id where tbl_site_details.site_name=@site_name order by tbl_energy_report.sl_no desc", con);         
 cmd.Paramaters.AddWithValue("@site_name ", value);