DataSet.Table Question

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

The SQL string is "select * from Customer where 0 > 1", and the DataSet
should be null.
Why MyDataSet.Tables.Count is 1, not 0? What kind of DataTable in this
place?
Thanks for help.

Jason
 
Because you're actually creating an empty DataTable. It will have the
structure of the Customer table but it will have no rows. Therefore.
MyDataSet.Tables[0].Rows.Count will return 0.

Pete
 
Hello Jason,

If I get your question right.....
The reason why you will get a DataSet with one DataTable is you asked for
a table. The result is therefor a table, only this time without rows.

also:
- If you ask the MyDataSet.Tables[yourTable].Rows.Count it will be 0
- If you ask the MyDataSet.Tables[yourTable].Columns.Count you will see that
it has as many columns as your table Customer in your database.

Hope this answered your question

Cheers
Christiaan
 
Thanks Chris!
This is my function FillSqlDataAdapter code:

public void FillSqlDataAdapter(string strFillQry,string DSname)
{
gConn=new SqlConnection();
gConn.ConnectionString=gstrConn;
gDA =new SqlDataAdapter(strFillQry,gConn.ConnectionString);
gDS=new DataSet();
gDA.Fill(gDS,DSname);
gDT=new DataTable();
gDT=gDS.Tables[0];
}// FillSqlDataAdapter

where the gVariable stands for GlobalVariable.

Chris van Bergen said:
Hello Jason,

If I get your question right.....
The reason why you will get a DataSet with one DataTable is you asked for
a table. The result is therefor a table, only this time without rows.

also:
- If you ask the MyDataSet.Tables[yourTable].Rows.Count it will be 0
- If you ask the MyDataSet.Tables[yourTable].Columns.Count you will see
that it has as many columns as your table Customer in your database.

Hope this answered your question

Cheers
Christiaan
Hi,

The SQL string is "select * from Customer where 0 > 1", and the
DataSet
should be null.
Why MyDataSet.Tables.Count is 1, not 0? What kind of DataTable in
this
place?
Thanks for help.
Jason
 
Hi Jason,

I notice that you use your dataadapter as a 'global'. Isn't that scope a bit
too big? Do you really need the adapter to live beyond the method
FillSqlDataAdapter?
Also, as soon as your connection ( gConn ) is created, why not reuse it?
First check if the gConn is null, if so then create a new connection.
And again, do really need those big scoped variables? Do you use them in
other methods or properties?

Chris

Jason Huang said:
Thanks Chris!
This is my function FillSqlDataAdapter code:

public void FillSqlDataAdapter(string strFillQry,string DSname)
{
gConn=new SqlConnection();
gConn.ConnectionString=gstrConn;
gDA =new SqlDataAdapter(strFillQry,gConn.ConnectionString);
gDS=new DataSet();
gDA.Fill(gDS,DSname);
gDT=new DataTable();
gDT=gDS.Tables[0];
}// FillSqlDataAdapter

where the gVariable stands for GlobalVariable.

Chris van Bergen said:
Hello Jason,

If I get your question right.....
The reason why you will get a DataSet with one DataTable is you asked for
a table. The result is therefor a table, only this time without rows.

also:
- If you ask the MyDataSet.Tables[yourTable].Rows.Count it will be 0
- If you ask the MyDataSet.Tables[yourTable].Columns.Count you will see
that it has as many columns as your table Customer in your database.

Hope this answered your question

Cheers
Christiaan
Hi,

The SQL string is "select * from Customer where 0 > 1", and the
DataSet
should be null.
Why MyDataSet.Tables.Count is 1, not 0? What kind of DataTable in
this
place?
Thanks for help.
Jason
 
Thanks Chris!
Honestly speaking, when I made up those codes the other days,
I was in a hurry to test how SQL can work in the .Net.
I think the reusing gConn connection is a good idea!
The reason I have the SqlDataAdapter as global is I think it will be more
convient.
every form can use the FillSqlDataAdapter.

Jason


Christiaan van Bergen said:
Hi Jason,

I notice that you use your dataadapter as a 'global'. Isn't that scope a
bit too big? Do you really need the adapter to live beyond the method
FillSqlDataAdapter?
Also, as soon as your connection ( gConn ) is created, why not reuse it?
First check if the gConn is null, if so then create a new connection.
And again, do really need those big scoped variables? Do you use them in
other methods or properties?

Chris

Jason Huang said:
Thanks Chris!
This is my function FillSqlDataAdapter code:

public void FillSqlDataAdapter(string strFillQry,string DSname)
{
gConn=new SqlConnection();
gConn.ConnectionString=gstrConn;
gDA =new SqlDataAdapter(strFillQry,gConn.ConnectionString);
gDS=new DataSet();
gDA.Fill(gDS,DSname);
gDT=new DataTable();
gDT=gDS.Tables[0];
}// FillSqlDataAdapter

where the gVariable stands for GlobalVariable.

Chris van Bergen said:
Hello Jason,

If I get your question right.....
The reason why you will get a DataSet with one DataTable is you asked
for a table. The result is therefor a table, only this time without
rows.

also:
- If you ask the MyDataSet.Tables[yourTable].Rows.Count it will be 0
- If you ask the MyDataSet.Tables[yourTable].Columns.Count you will see
that it has as many columns as your table Customer in your database.

Hope this answered your question

Cheers
Christiaan

Hi,

The SQL string is "select * from Customer where 0 > 1", and the
DataSet
should be null.
Why MyDataSet.Tables.Count is 1, not 0? What kind of DataTable in
this
place?
Thanks for help.
Jason
 
Hi Jason,

Well, making a method public can sure be useful this way. But be very
careful with a too big a scope of the variables/objects used inside that
method. Like I mentioned before, the scope of the adapater is very big. And
given your code, you do not release the instance of the adapter. So, this
adapter object will keep on living even after you leave the method
FillSqlDataAdapter. So if you do not need the adapter gDA outside
FillSqlDataAdapter, make it a local object.

Something like this:

public void FillSqlDataAdapter(string strFillQry,string DSname)
{
if (gConn==null)
{
gConn=new SqlConnection();
gConn.ConnectionString = gstrConn;
}
SqlDataAdapter DA =new SqlDataAdapter(strFillQry,gConn);
gDS=new DataSet();
DA.Fill(gDS,DSname);
if (gDS.Tables.Count>0)
gDT=gDS.Tables[0];
else
gDT=null;
}

In the code above, the dataadapter will be set ready for garbage collecting
after the method has been completed.
Now you could even place the code "gDS = null;" in the last if-statement so
you can reset the global dataset to null. And that is what you asked for at
first.

Cheers,
Christiaan



Jason Huang said:
Thanks Chris!
Honestly speaking, when I made up those codes the other days,
I was in a hurry to test how SQL can work in the .Net.
I think the reusing gConn connection is a good idea!
The reason I have the SqlDataAdapter as global is I think it will be more
convient.
every form can use the FillSqlDataAdapter.

Jason


Christiaan van Bergen said:
Hi Jason,

I notice that you use your dataadapter as a 'global'. Isn't that scope a
bit too big? Do you really need the adapter to live beyond the method
FillSqlDataAdapter?
Also, as soon as your connection ( gConn ) is created, why not reuse it?
First check if the gConn is null, if so then create a new connection.
And again, do really need those big scoped variables? Do you use them in
other methods or properties?

Chris

Jason Huang said:
Thanks Chris!
This is my function FillSqlDataAdapter code:

public void FillSqlDataAdapter(string strFillQry,string DSname)
{
gConn=new SqlConnection();
gConn.ConnectionString=gstrConn;
gDA =new SqlDataAdapter(strFillQry,gConn.ConnectionString);
gDS=new DataSet();
gDA.Fill(gDS,DSname);
gDT=new DataTable();
gDT=gDS.Tables[0];
}// FillSqlDataAdapter

where the gVariable stands for GlobalVariable.

"Chris van Bergen" <[email protected]>
???????:[email protected]...
Hello Jason,

If I get your question right.....
The reason why you will get a DataSet with one DataTable is you asked
for a table. The result is therefor a table, only this time without
rows.

also:
- If you ask the MyDataSet.Tables[yourTable].Rows.Count it will be 0
- If you ask the MyDataSet.Tables[yourTable].Columns.Count you will see
that it has as many columns as your table Customer in your database.

Hope this answered your question

Cheers
Christiaan

Hi,

The SQL string is "select * from Customer where 0 > 1", and the
DataSet
should be null.
Why MyDataSet.Tables.Count is 1, not 0? What kind of DataTable in
this
place?
Thanks for help.
Jason
 
Back
Top