当我在文本框中编写此代码以便在文本框中写入名称后自动从数据库中检索数据但是它无效...请帮帮我
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
string sqlQuery = "SELECT EmployeeID, Weight, Amount FROM Supplier where EmployeeName=@EmployeeName";
var DS = new DataSet();
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["inventoryConnectionString"].ConnectionString);
var SDA = new SqlDataAdapter(sqlQuery, conn);
SDA.SelectCommand.Parameters.AddWithValue("@EmployeeName", TextBox2.Text);
try
{
SDA.Fill(DS);
foreach (DataRow row in DS.Tables[0].Rows)
{
txtboxid.Text = row["EmployeeID"].ToString();
txtboxw.Text = row["Weight"].ToString();
txtboxam.Text = row["Amount"].ToString();
}
}
catch (Exception ex)
{
}
}
答案 0 :(得分:1)
尝试使用SqlDataReader
string selectQuery = "SELECT EmployeeID, Weight, Amount FROM Supplier where EmployeeName=@EmployeeName";
SqlCommand cmd = new SqlCommand(selectQuery, conn);
SqlDataReader dataReader;
conn.Open();
dataReader = cmd.ExecuteReader();
if (dataReader != null)
{
while (dataReader.Read())
{
//Retrieving data vales from select query in variables
txtboxid.Text = dataReader["EmployeeID"].ToString();
txtboxw.Text = dataReader["Weight"].ToString();
txtboxam.Text = dataReader["Amount"].ToString();
}
}
答案 1 :(得分:0)
SqlConnection Conn = new SqlConnection(Connection_String);
SqlCommand Comm1 = new SqlCommand(Command, Conn);
Conn.Open();
textBox.Text = Comm1.ExecuteScalar();
Conn.Close();
尝试
答案 2 :(得分:0)