日期:2014-05-20  浏览次数:20877 次

从数据表里查询出的一条记录,如何实现按要求分别显示到相应的TextBox框中
从数据表里查询出的一条记录,如何实现按要求分别显示到相应的TextBox框中

------解决方案--------------------
string QueryStr= "select * from tablename ";
OleDbCommand Cmd = new OleDbCommand(QueryStr, myConnection);
myConnection.Open();
OleDbDataReader dr= cmd.ExecuteReader();
try
{
while (dr.Read())
{
for (int i = 0; i < dr.FieldCount; i++)
{
if (myDataReader.GetName(i) == "columnsname1 ")
{
TextBox1.Text = dr.GetString(i);
}
if (dr.GetName(i) == "columnsname2 ")
{
TextBox2.Text = dr.GetString(i);
}

}
}
catch
{
}
finally
{
dr.Close();
myConnection.Close();
}

}
------解决方案--------------------
public DataTable getEmployeeDao()
{
return this.getEmployeeDao( "Employee ");
}

public DataTable getEmployeeDao(string Tablename)
{
DataTable dt = new DataTable(Tablename);
SqlConnection conn = ManageConn.getConnection();
SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = conn;
cmd.CommandText = "select EmpNo,CardID,EmpName, DeptName, CreateDate, Sex , JoinDay, Birthday , Duty , Nationstate, IDCard, MarryState,Tel, Addr , JTWeekName from employee where DimissionDay is null ";
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
//dt.Columns[0].Unique = true;
// dt.Columns[2].Unique = true;
//dt.Columns[3].Unique = true;
}
catch (SqlException e1)
{
MessageBox.Show( "錯誤訊息: " + e1.Message, "錯誤提示 ");
return null;
}
catch (Exception e2)
{
MessageBox.Show( "錯誤訊息: " + e2.Message, "錯誤提示 ");
return null;
}
finally
{
cmd = null;
ManageConn.freeConnection(conn);
}
return dt;
}
public Employee()
{
InitializeComponent();
dao = new EmpDao();
ds = new DataSet();
ds.Tables.Add(dao.getEmployeeDao());
dv = new DataView(ds.Tables[ "Employee "]);

}

private void getEmployeInfo(int row)
{

DataRow dr1 = ds.Tables[ "Employee "].Rows[row];
textEmpNo.DataBindings.Add( "Text ", ds, "Employee.EmpNo ");
textCardID.DataBindings.Add( "Text ", ds, "Employee.CardID ");
textEmpName.DataBindings.Add( "Text ", ds, "Employee.EmpName ");
textDeptName.DataBindings.Add( "Text ", ds, "Employee.DeptName ");
textCreateDate.DataBindings.Add( "Text ", ds, "Employee.CreateDate ");