0

プログラムを実行しようとするとエラーが発生します。

varcharデータ型を日時データ型に変換すると、値が範囲外になりました。

selectステートメントに何か問題がありますか?

  int month=0;

    if (RadioButtonList1.SelectedIndex == 0)
    { month = 1; }
    else if(RadioButtonList1.SelectedIndex == 1)
    { month = 2; }
    else if(RadioButtonList1.SelectedIndex == 2)
    { month = 3; }

    SqlCommand cmd = new SqlCommand("SELECT thDate, thType, thAmountIn, thAmountOut from [Transaction] Where thDate > dateadd(month, -" + month + ", GETDATE())", myConnection);
    da = new SqlDataAdapter(cmd);
    myConnection.Open();
    da.Fill(ds, "[Transaction]");
    myConnection.Close();
    if (!IsPostBack)
    {
        ViewState["vs_Transaction"] = (DataTable)ds.Tables["Transaction"];
        GridView1.DataSource = ds.Tables["Transaction"];
        GridView1.DataBind();

以下はthDateのデータベーステーブルです

ここに画像の説明を入力してください

ここに画像の説明を入力してください

4

1 に答える 1

0

問題は、データを として保存しVarchar、 を埋めているdatatableことですDatetime。次の2 つの場所にcast値を指定する必要があります。query

  • select、として戻るためDate
  • では、 aと a を Where比較しているためです。DateVarchar

だから、あなたのselect句は

SqlCommand cmd = new SqlCommand("SELECT Convert(datetime, thDate,103) as thDate, thType, thAmountIn, thAmountOut from [Transaction] Where Convert(datetime, thDate,103)> dateadd(month, -" + month + ", GETDATE())", myConnection);
于 2012-07-28T04:08:30.153 に答える