如何从数据库中循环读出某个字段,然后再textbox中显示出来?
叶面上是一个表单,txtposition需要从数据库中读出3个职位填入,应该怎么写?下面的代码总是出错。请大家帮我看看。谢谢啦。c#
private void showInfo()
{
string strConn = ConfigurationSettings.AppSettings["connectionstring"];
SqlConnection conn = new SqlConnection(strConn);
string id = Request.QueryString["id"];
string strSQL = "proc_GetCompanyStuff";
SqlCommand comm = new SqlCommand(strSQL, conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@id", id);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
this.txtPosition.Value = reader["job_name "].ToString();
}
conn.Close();
} --------------------编程问答-------------------- 老大,用dataset哈:
DataSet ds = dsDataBind("select * from part");
string name = ds.Tables[0].Rows[0]["part_remark"].ToString();//把双引号里面的字符替换为你要查找的字段名。
string id = ds.Tables[0].Rows[0]["part_id"].ToString();
grade = Convert.ToInt32(ds.Tables[0].Rows[0]["part_id"].ToString());
public DataSet dsDataBind(string sqlSelect)
{
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlCon);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
--------------------编程问答-------------------- this.txtPosition.Text = reader["job_name "].ToString(); --------------------编程问答--------------------
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
this.txtPosition.Value = reader["job_name "].ToString();
}
这样的写法,你只会得到最后一行的值,
你可以读成一个datatable
SqlCommand comm = new SqlCommand(strSQL, conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@id", id);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(comm);
dataTable dtb = new DataTable();
da.Fill(dtb);
然后循环
for(int i=0;i< dt.Rows.Count;i++)
{
this.txtPosition.Text = dt.Rows[i]["job_name "].ToString();//循环显示
}
--------------------编程问答-------------------- 显示 Column 'job_name ' does not belong to table Table --------------------编程问答-------------------- while (reader.Read())
{
this.txtPosition.Value = reader["job_name "].ToString();
}
覆盖了前面的,
this.txtPosition.Value +=reader["job_name "].ToString();
--------------------编程问答--------------------
Table里不包含 job_name这一列 --------------------编程问答-------------------- 查询分析中执行存储过程,字段名是否一致
while (reader.Read())
{
this.txtPosition.Value+ = reader["job_name"].ToString()+",";
}
补充:.NET技术 , ASP.NET