-3

我写信请求有关我的 c# 语法的一些帮助。我不断在“sda.Fill(dt);”中获得语法 我无法弄清楚原因。

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;

  public partial class Default2 : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
 {
    if (!IsPostBack)
    {
        string query = "select distinct Price_type from Price";
        DataTable dt = GetData(query);
        ddlPrice.DataSource = dt;
        ddlPrice.DataTextField = "Price_type";
        ddlPrice.DataValueField = "Price_type";
        ddlPrice.DataBind();

        ddlPrice2.DataSource = dt;
        ddlPrice2.DataTextField = "Price_type";
        ddlPrice2.DataValueField = "Price_type";
        ddlPrice2.DataBind();
        ddlPrice2.Items[1].Selected = true;
    }
}

     protected void Compare(object sender, EventArgs e)
    {
      string query = string.Format("select price, date from Price where Price_type = '{0}' order by date)", ddlPrice.SelectedItem.Value);
       DataTable dt = GetData(query);

    string[] x = new string[dt.Rows.Count];
    decimal[] y = new decimal[dt.Rows.Count];
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        x[i] = dt.Rows[i][0].ToString();
        y[i] = Convert.ToInt32(dt.Rows[i][1]);
    }
        LineChart1.Series.Add(new AjaxControlToolkit.LineChartSeries { Name = ddlPrice.SelectedItem.Value, Data = y });

        query = string.Format("select price, date from Price where Price_type = '{0}' order by date)", ddlPrice2.SelectedItem.Value);
    dt = GetData(query);

     y = new decimal[dt.Rows.Count];
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        x[i] = dt.Rows[i][0].ToString();
        y[i] = Convert.ToInt32(dt.Rows[i][1]);
    }
         LineChart1.Series.Add(new AjaxControlToolkit.LineChartSeries { Name = ddlPrice2.SelectedItem.Value, Data = y });
    LineChart1.CategoriesAxis = string.Join(",", x);

        LineChart1.ChartTitle = string.Format("{0} and {1} Order Distribution", ddlPrice.SelectedItem.Value, ddlPrice2.SelectedItem.Value);
    LineChart1.Visible = true;
}

private static DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["bwic testConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
        return dt;
    }
}
4

7 回答 7

1

date可能是保留关键字,具体取决于您使用的版本。[date]改为写。还有这个

"select price, date from Price where Price_type = '{0}' order by date)"

应该

"select price, date from Price where Price_type = '{0}' order by date"
于 2013-03-22T16:58:44.497 回答
1

您可以尝试使用此代码

using (var con = new SqlConnection(constr))
{

    using (var cmd = new SqlCommand(query))
    {
        using (var sda = new SqlDataAdapter())
        {
            con.Open();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            sda.Fill(dt);
        }
    }
    return dt;
}
于 2013-03-22T17:05:55.950 回答
1

GetData()如您的原始帖子所示,您的方法在语法上没有任何问题。您可以尝试注释掉整个方法体,然后开始一次一个地取消注释以隔离实际问题。

但显然您似乎没有语法错误:您的 SQL 无效。您尝试执行的 SQL 是:

select price, date from Price where Price_type = '{0}' order by date)

它有两个问题:

  • 最后的右括号是您的语法错误,并且
  • 该参数不应包含在引号中。

您的 SQL 应该类似于

select price, date from Price where Price_type = {0} order by date

您的方法应如下所示:

private static DataTable GetData1( string query , string p0 )
{
  DataTable dt = new DataTable();
  string constr = ConfigurationManager.ConnectionStrings["bwic testConnectionString"].ConnectionString;
  using ( SqlConnection con = new SqlConnection( constr ) )
  using ( SqlCommand cmd = con.CreateCommand() )
  using ( SqlDataAdapter sda = new SqlDataAdapter(cmd) )
  {
    cmd.CommandText = query;
    cmd.CommandType = CommandType.Text;
    SqlParameter p = new SqlParameter() ;
    p.Value = p0Value ;
    cmd.Parameters.Add( p ) ;
    con.Open();
    sda.Fill( dt );
    con.Close();
  }
  return dt;
}
于 2013-03-22T17:10:45.017 回答
0

不要忘记在 SELECT 子句和 ORDER BY 子句中使用括号两次。

于 2013-03-22T17:02:54.787 回答
0

在您的代码中,如下所示

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand(query))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            sda.Fill(dt);
        }
    }
    return dt;
}

将其更改为

using (SqlConnection con = new SqlConnection(constr))
{

    using (SqlCommand cmd = new SqlCommand(query))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            con.Open();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            sda.Fill(dt);
        }
    }
    return dt;
}

这里的问题是你在填充之前没有打开任何连接。

于 2013-03-22T17:03:06.653 回答
0

您还没有在任何地方打开连接。您必须先打开连接,然后才能使用它。

于 2013-03-22T17:04:02.587 回答
0

如果您的整个课程代码确实是这样,那么您在文件末尾缺少一个结束 }

 public partial class Default2 : System.Web.UI.Page
 {

宣言。

于 2013-03-22T17:11:56.167 回答