Search a database for a record (Visual Studio 2010)?

SUPERMAN89

Expert Member
Joined
Jan 14, 2008
Messages
1,385
Reaction score
4
Location
South Africa
Anyone know a simply way to just enter a name into a textbox and search a database for that name and return in a msgbox if that name is in the database. the internet and msdn just doesnt have what im looking for!:cry:
 
Pseudo code:

SQLCommand command = new SQLCommand("SELECT [Name] FROM
WHERE [Name] LIKE '%" & TextBox1.Text & "%'", new SQLConnection("SQL Connection String"));
SQLDataReader reader;
reader = command.ExecuteReader();

if (reader.HasRows()){ //Single Record
reader.Read();
MessageBox.Show(reader[0].ToString());
}

//Or records >= 1
While (reader.Read()){
MessageBox.Show(reader[0].ToString());
}

reader.Close();
reader.Dispose();
command.Dispose();
 
Sure, make use of using statement if you love try..catch..finally in your actual code. I don't therefore I don't use them.

Effectively I see this alot:

try{
using (...){
...
}
}catch (Exception ex){

}

effectively your code now read like this in the compiler
try{
try{
...
}
catch(){

}
finally{

}
}
}catch (Exception ex){

}

se excuse me for trying to optimise my Psuedo code.
 
Sure, make use of using statement if you love try..catch..finally in your actual code. I don't therefore I don't use them.

Effectively I see this alot:

try{
using (...){
...
}
}catch (Exception ex){

}

effectively your code now read like this in the compiler
try{
try{
...
}
catch(){

}
finally{

}
}
}catch (Exception ex){

}

se excuse me for trying to optimise my Psuedo code.

With you on that one. Also, as far as I remember, SqlCommand gets automatically disposed if you close SqlConnection.
 
@iblade: so can I interpret your reply as you promoting this:
Code:
try
{
  //Do
  //Dispose
}catch(...)
{
  //Handle
}

as opposed to


Code:
try
{
  //Do  
}
catch(...)
{
  //Handle
}
finally
{
  //Dispose
}

If so, in your scenario what happens to unmanaged resources when an exception is thrown in the "do" section?
 
Top
Sign up to the MyBroadband newsletter
X