从 SQL 数据库填充文本框
我有七个字段需要填充在七个文本框中。数据来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用索引或列名来获取实际数据,如下所示:
然后,对
TextBox
进行简单的赋值。否则,您也可以直接将数据插入到TextBox
中,但在大多数情况下不应在此之前完成验证。如果您需要更多帮助,请查看此处:
MSDN - SqlCeDataReader
You can either use the index or the column name to get the actual data, as follows:
After which, it's simple assignment to the
TextBox
. Otherwise you could also directly insert the Data into theTextBox
, but rather not as validation should be done before this in most cases.If you need more help, have a look here:
MSDN - SqlCeDataReader