Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vodkasoda
Starting Member
15 Posts |
Posted - 2012-02-01 : 07:28:36
|
Can somebody please explain, in nice clear & concise English, what exactly the SQLdataadapter is and how it works ? My course uses it but doesn't explain why and I can't get my head around it !!! This is the code I have at the moment ... public partial class KAELCForm1 : Form { //Connect to SQL Server database SqlConnection cs = new SqlConnection(@"Data Source=MEDESKTOP;AttachDbFilename=J:\Users\Gary\Documents\Visual Studio 2010\Projects\KA_Email_League_Cup\KA_Email_League_Cup\KAELC_DB.mdf;Initial Catalog=myKADB;Integrated Security=True"); public KAELCForm1() { InitializeComponent(); //Set up SQL statement SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM LEAGUES", cs); //Create Dataset DataTable dt = new DataTable(); //Fill Dataset da.Fill(dt); //Fill ComboBox from Dataset for (int i = 0; i < dt.Rows.Count; i++) { KAELCcomboBox1.Items.Add(dt.Rows[i]["LeagueName"]); } KAELCcomboBox1.Text = "Select From Options Below ..."; }... which works, but just seems long-winded & complicated. I look at a lot of code via Google and it seems that sometimes it is used and other times it is not, is there an easier / better way to access a SQL database ? |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-01 : 07:59:46
|
| The name is almost descriptive. According to Microsoft, a DataAdapter "Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database". That is as precise a definition as one can get. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter(v=vs.100).aspxIn ADO.Net, to make a connection, you need a connection object (SqlConnection for SQL Server) and a command object (SqlCommand for SQL Server). Then, depending on what you need to do, you could use DataAdapter or DataReader. (there is also ExecuteScalar and ExecuteNonQuery). Usually you use DataAdapter when you want to work with DataTables on the .Net side. DataAdapters are a little heavier than readers, so lot of people prefer readers.Look for some tutorials or basics of ADO.Net. The concept is really simple, and once you do it once or twice, it is like telling the same story again and again, it almost becomes second nature.Here is one link - I haven't read through it in detail, but seems decent: http://www.sqlserver2005tutorial.com/Tutorial-Learn-Basics-of-ADO-NET.html |
 |
|
|
|
|
|
|
|