iT邦幫忙

0

如何同時查詢多個欄位多筆資料 asp.net C#

  • 分享至 

  • xImage

目前用table手動輸入資料,查詢到gridview內的資料並將完整資料填回去table,只查一筆資料時能執行,但查詢多筆資料時就不行了。
https://ithelp.ithome.com.tw/upload/images/20190418/201170251HyUjCMncL.png
p.s 所有conn.open的語法皆無法使用
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class 採購作業 : System.Web.UI.Page
{
public object Database { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Visible = false;
}

protected void Button1_Click(object sender, EventArgs e)
{
    if ((DropDownList2.SelectedIndex != 0))
        GridView1.Visible = true;

    string str2 = DropDownList2.Text;
    string str = "SELECT * FROM dbo.供應商資料 WHERE (供應商編號 LIKE '%" + str2 + "%' )";
    SqlDataSource3.SelectCommand = str;
    SqlDataSource3.DataBind();
    Label3.Visible = true;
    Label3.Text = DropDownList2.SelectedValue;
}
protected void Button2_Click(object sender, EventArgs e)
{
    
}

protected void Button3_Click(object sender, EventArgs e)
{
    TextBox5.Visible = false;
    TextBox4.Visible = false;
    string str1 = TextBox3.Text;
    string s1 = "SELECT * FROM dbo.庫存資料 WHERE (商品編號='" + str1 + "') ";
    SqlDataSource6.SelectCommand = s1;
    if (GridView3.Rows.Count > 0)
    {

        TextBox4.Visible = true;
        TextBox4.Text = GridView3.Rows[0].Cells[1].Text;
        TextBox5.Visible = true;
        TextBox5.Text = GridView3.Rows[0].Cells[2].Text;
    }
    SqlDataSource6.DataBind();

    TextBox11.Visible = false;
    string str3 = TextBox10.Text;
    string s2 = "SELECT * FROM dbo.庫存資料 WHERE (商品編號='" + str3 + "') ";
    SqlDataSource6.SelectCommand = s2;
    if (GridView3.Rows.Count > 0)
    {
        TextBox11.Visible = true;
        TextBox11.Text = GridView3.Rows[1].Cells[1].Text;
    }
    SqlDataSource6.DataBind();
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
小魚
iT邦大師 1 級 ‧ 2019-04-18 23:08:17

1.先學一下SQL語法, 商品編號 = 當然只能查詢一筆資料.
2.建議可以用參數查詢, 要不然會有 SQL Injection的問題.

0
阿偉
iT邦新手 2 級 ‧ 2020-09-03 09:38:23

GridView中,先將要查詢的textbox對應到要對應的Table資料行裡

會基本SQL直接寫

select * from Table
where 商品編號 = @商品編號 
and   商品名稱 = @商品名稱 
.
.
.
.
看你要關聯幾個,依此類推

我要發表回答

立即登入回答