![](/img/trans.png)
[英]I am trying to parameterized Sql query but I am getting the error“'Must declare the scalar variable ”
[英]i am new to sql .Not Getting query result . Runs fine with no error
private void button1_Click(object sender, EventArgs e)
{
DataTable DataTab = new DataTable();
DaSql = new SqlDataAdapter("SELECT * FROM Student where Gender = '" + textBox1.Text + "' ", conSql);
DaSql.Fill(DataTab);
DataGridQueryResult.DataSource = DataTab;
}
在您的代碼運行之前,不要過多關注 SQL INJECTION。 讓它工作,然后保護它。 嘗試設置一個變量
var sqlText = "SELECT * FROM Student where Gender = '" + textBox1.Text + "' ";
然后在調試器中點擊它以檢查您的完整查詢語句。 確保你沒有像額外的空間那樣有任何愚蠢的東西。
使用參數修復 SQL INJECTION 漏洞
Query 沒有錯誤似乎是 DataGrid 的綁定問題。 我的情況是一樣的,我使用 SQL、DataTable、DataGrid。 你會試試這個嗎?
DataTable DataTab = new DataTable("Student");
DaSql = new SqlDataAdapter("SELECT * FROM Student where Gender = '" + textBox1.Text + "' ", conSql);
DaSql.Fill(DataTab);
DataGridQueryResult.ItemsSource = DataTab.DefaultView;
並且需要檢查窗口設計器中的 DataGrid 設置是否正確,包括 AutoGeneratingColumns=True。 我總是使用 ItemsSource 效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.