-3

我想从学生数据库中检索一个字段,比如电子邮件,然后我想打印从数据库中检索到的信息,在 Windows 窗体中的文本框中(在 c# 中)......是否可以这样做.....

4

1 回答 1

1

您从什么类型的数据库中检索?我假设 SQL Server 7+?如果是这样的话,那么像这样使用 SqlConnection :

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NameOfConnectionInAppConfigFile"].ConnectionString);

从这里你可以像这样构建你的命令:

SqlCommand command = new SqlCommand("Select [email] from [DBname]",connection);

您现在需要执行命令以将数据转换为可用格式。我会在这里使用 SqlDataAdapter 将所有信息放入 DataTable 中(您也可以使用 SqlDataReader,具体取决于您要执行的操作)。它看起来像这样:

DataTable dt = new DataTable();
SqlDataAdapter adpt = new SqlDataAdapter(command);
adpt.Fill(dt);

现在,您可以通过 DataColumn 和 DataRow 访问数据。从那里您将参考您要查找的内容并填充文本框。它看起来像这样:

textBox.Text = dt.Rows[0]["email"].ToString();

还指出,您需要考虑到您可能会返回多封电子邮件。在这种情况下,您需要执行以下操作:

foreach (DataRow row in dt.Rows)
{
     //Logic Here
}

我希望这有帮助。

于 2013-03-26T17:53:37.530 回答