fill Method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use this code to select a particular record using the fill
method. is there a way to do this?? Please help me someone.

SqlCommand cmd = new SqlCommand("SELECT * " + "FROM [animal-info]", cnKennel);

da = new SqlDataAdapter(cmd);

cbd = new SqlCommandBuilder(da);
dsPictures = new DataSet();
da.Fill(dsPictures);

lstPictures.DataSource = dsPictures.Tables[0];
lstPictures.DisplayMember = "filename";
 
The Fill method can be used to return a specific row but you'll need a WHERE
clause to limit the rowset to just the desired row(s).
SELECT col, col1, col2 FROM Table WHERE SomeStrcol = 'some text value'
or

SELECT col, col1, col2 FROM Table WHERE SomeNumcol = somevalue

Ideally this should be setup as a parameter query.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
www.sqlreportingservices.net
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Norm,

First a few general comments about your code -

You are using string concatenation to create the command, that is probably
not a great idea. You should use parameterized queries, and stringbuilder to
concatenate (if need be).
Okay secondly, your query is "Select * ... ", it's probably a better idea to
specify the column names.

Finally, answering your question :),
I am trying to use this code to select a particular record using the fill
method. is there a way to do this?? Please help me someone.

There are numerous ways to do this, and the specific way you pick depends on
your specific situation,

a) You can limit the specific row you need in the database, using a where
clause in front of your SQL Query.
b) You can sue SqlDataReader, which is the return value of
SqlCommand.ExecuteReader, remember to close the data reader and connection
when you are done.
c) You can use a dataset/datatable and then limit the specific row you want
using the DataTable.Select method
d) You can use a dataset/datatable, and then create a dataview on the
datatable, and limit your results to the specific row/s you are interested
in, using the dataview.

Each of these have their own plusses and minuses, and the specific approach
you may take, depends on your situation/usage.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx





nbohana said:
I am trying to use this code to select a particular record using the fill
method. is there a way to do this?? Please help me someone.

SqlCommand cmd = new SqlCommand("SELECT * " + "FROM [animal-info]",
cnKennel);

da = new SqlDataAdapter(cmd);

cbd = new SqlCommandBuilder(da);
dsPictures = new DataSet();
da.Fill(dsPictures);

lstPictures.DataSource = dsPictures.Tables[0];
lstPictures.DisplayMember = "filename";
 
nbohana,

I am lazy so I do often what you do, I am also a big fan of the dataadapter.

But the dataadapter in my opinion not the best method to get one database
row.

I would go in your situation, (if it is not one row or something in a
complete process) for a datareader (and if updates needed the
executenonquery) to get one row and create for that my own class/object (or
if it is only reading and using in one methods variables).

http://msdn.microsoft.com/library/d...emdatasqlclientsqldatareaderclasstopic.aspFor the SQL string you can look to the message from William Vaughn and hisadvice about parameters.If you are a VB Net user and need to know how to use parameters, than youcan visit our website where there are more samples about that.http://www.vb-tips.com/default.aspxI surely would not go for methods by reading a complete datatable and use byinstance a dataview or a select. This will give in a real productionenvironment only less speed and most probably harder to resolve concurrencyproblems.I hope this helps,Cor"nbohana" <[email protected]> schreef in berichtnews:[email protected]...>I am trying to use this code to select a particular record using the fill> method. is there a way to do this?? Please help me someone.>> SqlCommand cmd = new SqlCommand("SELECT * " + "FROM [animal-info]",cnKennel);>> da = new SqlDataAdapter(cmd);>> cbd = new SqlCommandBuilder(da);> dsPictures = new DataSet();> da.Fill(dsPictures);>> lstPictures.DataSource = dsPictures.Tables[0];> lstPictures.DisplayMember = "filename";>> --> Norm Bohana
 
nbohana,

Sometimes this happens my message is automatically converted to s**t, I did
nothing to add that to do that myself.

:-)

Here the message as it was (hoping this goes now).

..............................

I am lazy so I do often what you do, I am also a big fan of the dataadapter.

But the dataadapter in my opinion not the best method to get one database
row.

I would go in your situation, (if it is not one row or something in a
complete process) for a datareader (and if updates needed the
executenonquery) to get one row and create for that my own class/object (or
if it is only reading and using in one methods variables).

http://msdn.microsoft.com/library/d...emdatasqlclientsqldatareaderclasstopic.aspFor the SQL string you can look to the message from William Vaughn and hisadvice about parameters.If you are a VB Net user and need to know how to use parameters, than youcan visit our website where there are more samples about that.http://www.vb-tips.com/default.aspxI surely would not go for methods by reading a complete datatable and use byinstance a dataview or a select.This will give in a real production environment only less speed and mostprobably harder to resolve concurrency problems.I hope this helps,Cor
 
Last try,

I am lazy so I do often what you do, I am also a big fan of the dataadapter.

But the dataadapter in my opinion not the best method to get one database
row.

I would go in your situation, (if it is not one row or something in a
complete process) for a datareader (and if updates needed the
executenonquery) to get one row and create for that my own class/object (or
if it is only reading and using in one methods variables).

http://msdn.microsoft.com/library/d...ystemdatasqlclientsqldatareaderclasstopic.asp

.....................................

About the SQL string you can look to the message from William Vaughn and his
advice about parameters.

If you are a VB Net user and need to know how to use parameters, than you
can visit our website where there are more samples about that.

http://www.vb-tips.com/default.aspx

I surely would not go for methods by reading a complete datatable and use
byinstance a dataview or a select.

This will give in a real production environment only less speed and most
probably harder to resolve concurrency problems.

I hope this helps,

Cor
 
Back
Top