iT邦幫忙

0

sql語法沒有運行

  • 分享至 

  • xImage

原先是寫好sql後能順利跑出資料庫裡的表格
但後來發現要做出來的結果得加上條件式
奇怪的是條件式能跑了
但sql語法卻故障,找了老半天也看不出來是出了甚麼問題
visual也沒提示code有問題
希望有大佬能幫忙解決QQ

https://ithelp.ithome.com.tw/upload/images/20210824/20140388WjIWGy4tYo.jpg

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;

namespace 試作.搜尋引擎
{
    public partial class 搜尋2 : System.Web.UI.Page
    {
        string Search_String = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string mainconn = ConfigurationManager.ConnectionStrings["G HConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(mainconn);
            sqlconn.Open();
            SqlCommand sqlcomm = new SqlCommand();
            sqlcomm.Connection = sqlconn;
            DataTable AAA = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(sqlcomm);

            sqlcomm.Parameters.AddWithValue("內文", TextBox1.Text);
            sqlcomm.Parameters.AddWithValue("留言", TextBox1.Text);

            GridView1.DataSource = AAA;
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            

            

            string Search_String = "";
            Boolean u_select = false;
            int word_length = 0;

            for (int i = 0; i < DDL1.Items.Count; i++)
            {
                if (DDL1.Items[i].Selected)
                {
                    Search_String += DDL1.Items[i].Text;
                    u_select = true; /*這邊要判斷是真*/
                }

            }


            if (u_select)
            {
                string Left(string param, int length)
                {

                    string result = param.Substring(0, length);

                    return result;
                }
                        word_length = Search_String.Length; /*length 要用英文的長度來判斷*/

                        Search_String = Left(Search_String, (word_length)); /*length 要看情況使用-or+*/

                        Label1.Text = Search_String;
            }


            else
                {

                    Label1.Text = "您尚未點選任何一個DowndropList子選項";

                    return;
                }

           

            SqlDataSource1.SelectCommand = "SELECT * FROM  [dbo].[ptt123] where 內文 like '" + Search_String + "'";


        }




       /* String sqlquery = "select * from [dbo].[ptt123] where 內文 like '%'+@Search_String+'%'";*/

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["G HConnectionString"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from [dbo].[ptt123]";
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            object dt = null;
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }


        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["G HConnectionString"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT [日期], [內文], [留言] FROM  [dbo].[ptt123] where 內文 like '" + Search_String + "'";
            cmd.Connection = con;
            SqlDataReader rd = cmd.ExecuteReader();
            object dt = null;
            GridView2.DataSource = dt;
            GridView2.DataBind();
        }

        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

    }

}

    

    




圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
mis2000lab
iT邦好手 1 級 ‧ 2021-09-02 16:09:18

先把您組合成功的SQL指令,呈現在畫面上,看看組合後的結果
SQL Command能否執行

也就是按下按鈕以後,這一段字串能否呈現在畫面上
用一個 Label看看他的結果?
"SELECT * FROM [dbo].[ptt123] where 內文 like '" + Search_String + "'"

另外,為什麼要用 "中文"幫你的專案、程式命名啊?
資料表的欄位也是 "中文",是特殊需求嗎?

namespace 試作.搜尋引擎
{
public partial class 搜尋2 : System.Web.UI.Page

感謝回應,之後有做修正了
程式已經能正常運行!

我要發表回答

立即登入回答