从 SQL 数据库填充文本框

发布于 2024-08-03 16:06:54 字数 870 浏览 2 评论 0原文

我有七个字段需要填充在七个文本框中。数据来自 SQL Compact DB...

到目前为止,这是我的代码,但我陷入困境。我需要做什么来填充表单加载上的文本框...非常感谢。

伍迪 <代码>

private void mcContactSubmit_Load(object sender, EventArgs e)
{
    // Setup our SQL connection.
    SqlCeConnection dataSource = new SqlCeConnection(
                 @"Data Source=|DataDirectory|\..\..\ContactInformation.sdf;
               Persist Security Info=False");
        SqlCeDataReader myReader = null;

    // Create our command text.
    string sqlQuery = String.Format(@"SELECT TOP (1) FirstName, LastName, Title, 
    Department, Category, Phone, Comments FROM ContactInformation 
    ORDER BY FirstName DESC");

    // Open the SQL connection.
    dataSource.Open();

    SqlCeCommand myCommand = new SqlCeCommand(sqlQuery, dataSource);
    myReader = myCommand.ExecuteReader();
}

I have seven fields that need to be populated in seven text boxes. The data is coming from a SQL Compact DB...

Here's my code so far, but I'm stuck. What do I need to do to populate the text boxes on Form Load... thanks much.

Woody

private void mcContactSubmit_Load(object sender, EventArgs e)
{
    // Setup our SQL connection.
    SqlCeConnection dataSource = new SqlCeConnection(
                 @"Data Source=|DataDirectory|\..\..\ContactInformation.sdf;
               Persist Security Info=False");
        SqlCeDataReader myReader = null;

    // Create our command text.
    string sqlQuery = String.Format(@"SELECT TOP (1) FirstName, LastName, Title, 
    Department, Category, Phone, Comments FROM ContactInformation 
    ORDER BY FirstName DESC");

    // Open the SQL connection.
    dataSource.Open();

    SqlCeCommand myCommand = new SqlCeCommand(sqlQuery, dataSource);
    myReader = myCommand.ExecuteReader();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

司马昭之心 2024-08-10 16:06:54

您可以使用索引或列名来获取实际数据,如下所示:

myReader = cmd.ExecuteReader();

// Run through the results
while (myReader.Read())
{
    string fname = myReader.GetString(0);

    // or alternatively:

    string fname2 = myReader["FirstName"];

    // Either of these should work
}

然后,对 TextBox 进行简单的赋值。否则,您也可以直接将数据插入到 TextBox 中,但在大多数情况下不应在此之前完成验证。

如果您需要更多帮助,请查看此处:

MSDN - SqlCeDataReader

You can either use the index or the column name to get the actual data, as follows:

myReader = cmd.ExecuteReader();

// Run through the results
while (myReader.Read())
{
    string fname = myReader.GetString(0);

    // or alternatively:

    string fname2 = myReader["FirstName"];

    // Either of these should work
}

After which, it's simple assignment to the TextBox. Otherwise you could also directly insert the Data into the TextBox, but rather not as validation should be done before this in most cases.

If you need more help, have a look here:

MSDN - SqlCeDataReader

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文