SqlConnection conn = new SqlConnection(strcon); DataSet dstable = new DataSet(); string sqlStrSelect = "select * from TUser where UserName='" + txtuser.Text.Trim() + "'"; try { SqlDataAdapter adapter = new SqlDataAdapter(sqlStrSelect, conn); dstable = new DataSet(); adapter.Fill(dstable, "用户"); UserName = Convert.ToString(dstable.Tables["用户"].Rows[0]["Username"]); } catch (Exception ex) { MessageBox.Show(ex.Message); return ; } finally { conn.Close(); } //判断用户是否存在 if (dstable.Tables["用户"].Rows.Count == 0) { MessageBox.Show("不存在该用户,请重新输入!"); txtuser.Focus(); return; }