我無法從下面的查詢中提取我需要的SQL數據。我查看了MSDN並遵循了他們的例子,但出於某種原因,執行讀取器沒有讀取數據並將其返回到文本框,點擊按鈕後。我添加了一個響應來確保連接已打開,並且每次都按下。這是代碼的提取物,我從此處省略的唯一事情就是在SqlConnection的細節:C#ExecuteReader不返回ASP.Net中的SQL查詢數據
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
//set up the SQL command
SqlCommand command = new SqlCommand("Use Waste_Test; select * from Bidston_HWRC where MCN_No = @MCN", conn);
//open the server connection
conn.Open();
//define parameter for SQL query
command.Parameters.Add("@MCN", SqlDbType.Int);
command.Parameters["@MCN"].Value = TextBox17.Text;
Response.Write("<script>alert('You are connected')</script>");
//Execute the query on the server
SqlDataReader rdr = command.ExecuteReader();
Response.Write("<script>alert('It's worked')</script>");
while (rdr.Read())
{
TextBox6.Text = rdr["Waste_Description"].ToString();
}
}
}
你說的參數是int類型,然後給它一個字符串,也許這與它有什麼關係? –
嘗試使用command.Parameters.AddWithValue(「MCN」,TextBox17.Text);如Lasse V. Karlsen所述,考慮鑄造類型爲int。如果需要,參數類型爲整數。 – MMK
爲什麼* while(rdr.Read())'只爲* 1 *值* loop *'? 'if(rdr.Read())TextBox6.Text = rdr [「Waste_Description」]。ToString();' –