Getting single row from TableAdapter.GetData() - asp.net

So I recently came across this solution in code and was wondering if it is considered an 'acceptable' way of retrieving a single row from a Strongly Typed dataset/table.
Basically what we have is something like this:
Dim fooAdapter As New FooDataSetTableAdapters.FooTableAdapter()
Dim fooRow As FooDataSet.FooRow = CType(fooAdapter.GetData().Select("SomeFooField=50")(0), FooDataSet.FooRow)
with fooRow
..
..
end with
Although the code above works my main concern is that calling fooAdapter.GetData() will actually grab all the data in the table before applying the Select() filter which could potentially slow things down over time... Is there a cleaner way to do this or is this way of doing things fine?
Edit:
The filter criteria is not the primarykey field so calling the table's FindByFooID method will not work...

Use the dataset editor tool to create a query GetUniqueFoo with the SQL that will filter based on SomeFooField. You can run it by using fooAdapter.GetUniqueFoo(SomeFooValue).

Related

Use "where in" for LiteDB

I enjoy working with LiteDB in Xamarin.Forms. What is the best practice for getting list of objects from a table using list of ids or indexes in condition?
Currently, it looks for me like this:
_db.GetCollection<T>().FindAll().Where(q => listValues.Contains(Convert.ToInt32(q.GetProperty(idColumnName))));
listValues - list of searched ids.
idColumnName - column with indexes.
But FindAll retrives all records from LiteDB. Is there any more efficint options without full scanning?
Use the query in the Find method instead which will only retrieve entries based on your query rather than the full data set. This, according to the LiteDB's guidance, is more efficient than Linq queries on the full data returned: https://github.com/mbdavid/LiteDB/wiki/Queries
So your query would look like as follows
_d.GetCollection<T>().Find(q => listValues.Contains(Convert.ToInt32(q.GetProperty(idColumnName))))
I think you can just use collection.Query().Where("Column", 1,2,3) for this

Using "Count" in PreparedStatement ... gives same result [duplicate]

I already used the search here (and other forums as well) but haven't found an answer exacty to what I'm trying to do.
I know that it can easily be done in some other way, and this is just a small sandbox-framework I'm coding for a University course... in a real environment I'd just take Spring, Hibernate etc.
So what I did was coding myself a small generic Data Access Layer with POJOs, working with generic methods to retrieve, check or insert data to the database (Oracle). Most of this is done through PreparedStatements.
This is working as long as I don't have joins... is it possible to put in a Column as parameter?
Example:
Table A has Attribute X + others
Table B has Attribute Y + others
PreparedStatement with query SELECT * FROM A,B WHERE "A"."X" = ?
And then fill in "B"."Y" as the parameter...
The database doesn't throw me an error or exception, but the ResultSet returned after executing the statement is empty. Is it just not possible to do, or am I just missing some escaping?
I'm using PreparedStatement.setString(int index, String value) to fill in the parameter... in lack of ideas which other setX method I could use...
Again, in a real project I'd never code that myself, but rather use something like Spring or Hibernate and not re-invent the wheel, but I see it as an interesting exercise to code such a generic small data access layer myself.
No, JDBC does not allow this. Only column values can be set. If you want to make dynamic changes to the sql statement you will have to do it before you create the PreparedStatement.

Getting out a single value from a sql database (asp.net)

I'm trying to get a single value from my table in a database. They are all given a unique id when stored. Here is the code I use to put the in:
With SqlDataSource1
.InsertParameters("page").DefaultValue = ViewState("TrueURL")
If My.User.IsAuthenticated Then
.InsertParameters("sender").DefaultValue = My.User.Name
Else
.InsertParameters("sender").DefaultValue = Request.UserHostAddress.ToString
End If
.InsertParameters("details").DefaultValue = PrepareText(TextBox2.Text)
.InsertParameters("date").DefaultValue = Now()
.Insert()
End With
That was so you could get an idea of what I was looking for, I'm not looking for sql statements, I don't know how to use them.
Just keep in mind, this is all vb.net/asp.net.
Thanks!
--EDIT--
I think I found something useful but I can't find how to use it. The Select function. It returns something and accepts parameters like the insert one I mentioned above... any ideas?
You cant extract it without first running an SQL query to extract it.
If you've got this far, read a little further in the MS help pages about how to run the select querys, this is where you would put your SQL statements.

LINQ "table" variable

I'm trying to turn a method I have right now into a more "generic" method that returns a string. Right now, the method uses a statement like this:
var app = (from d in testContext.DAPPs
where d.sserID == (Guid)user.ProviderUserKey
select d).ToList();
I process the results of "app", add extra text etc. The piece that changes (that I need to make more "generic") is the table name (DAPPs). Is there a way I can do that, or, a better way to go around this all together?
You'll have to compose a Dynamic Linq Query.
There are a couple of ways to do this. Have a look at the following:
Dynamic Expressions in Linq to Sql
http://www.west-wind.com/Weblog/posts/143814.aspx
Using the LINQ Dynamic Query Library
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Import schema from one datatable to another?

Are there any commands that make life easy with respect to this? I want to take the column schema of one datatable (.net datatable) and copy it to another new datatable.
I've seen something like:
SELECT * INTO [DestinationTable] FROM [SourceTable] WHERE (1=2);
Used in SqlServer.
I think this assumes that DestinationTable doesn't exist. It then creates the table and copies the schema from SourceTable the WHERE clause prevents any actual data from being copied.
I'm not really a database developer, so there's probably a much better way to do this.
I've found the answer. It is the .Clone method.

Resources