2009-08-25 15 views
0

7つのフィールドが7つのテキストボックスに表示されます。データは、SQLコンパクトDBから来ています...SQLデータベースのテキストボックスにデータを入力する

ここまでは私のコードですが、私は立ち往生しています。フォームロード時にテキストボックスにデータを設定するにはどうすればよいですか...ありがとうございます。

ウッディー

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(); 
} 

答えて

3

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。それ以外の場合は、TextBoxに直接データを挿入することもできますが、ほとんどの場合、この前に検証を行う必要があります。

MSDN - SqlCeDataReader

+0

おかげでバディ、簡単:

さらにヘルプが必要な場合

は、ここを見てください。 – Woody

+0

偉大なもの、それは助けてうれしい。 –

+0

Doh。私はあまりにも早く話しました。テキストボックスに入力されていない...デバッグモードで表示されている内容は次のとおりです。 メッセージ\t "SQL Server Compactでは、基になるカーソルがスクロール可能でない場合、HasRowsプロパティへの呼び出しをサポートしていません。 これは何を意味しますか? LOL ... – Woody

関連する問題