How to convert Select (Transact-SQL) syntax to VB .NET - sql

I am using VB .NET and I have a huge variable (Table A) in DataTable format. Table A has many columns.
I need to create a subset of table (class DataTable) from Table A by checking the field value in each Column from Table A.
There is a function in SQL, which matches the requirement above. It is called as SELECT (Transact-SQL). I have found the SQL description in the websites below:
http://msdn.microsoft.com/en-us/library/ms189499.aspx and
http://www.w3schools.com/sql/sql_where.asp.
My question:
What is the equivalent command in VB .NET which performs same task on DataTable?
I have checked various sources on converting the SQL command above to VB .NET. The Select Case in VB .NET works as an if condition checking.
Thank you.

Your best approach, if you need the result to also be in a DataTable, is likely to be creating a DataView:
Dim sourceTable = new DataTable()
....
Dim myView = new DataView(sourceTable, "selected = 1", "",
DataViewRowState.Current)
Dim newTable = myView.ToTable()
The second parameter is the where clause part of a SQL SELECT statement, whilst the third allows you to add sorting to the result. The final parameter allows you to select a subset of the original rows if required.

You can try the DataTable.Select method.

Related

Usings filtering with "WHERE" clause in Data Studio SQL datasource to reference a function

Please how can I reference a filter function in Data Studio from my SQL Query Data Source? For example
select * from [table] where date between ["Date Range Control" function]
If you edit your data source, you'll see a tickbox to enable date range parameters. Once ticked you can refer to the two new data range parameters it creates in your custom query. These two new parameters will be called:
DS_START_DATE
DS_END_DATE

Is it possible to specify a function in a bind variable using Oracle?

When inserting data into an Oracle table from .NET is it (and how) possible to specify a function as a bind variable instead of a value?
The example below is simplistic on purpose.
For example;
dim oraCommand as OracleCommand = new OracleCommand("insert into dummy(dummy_string) values :value_to_insert")
oraCommand.Parameters.Add("value_to_insert", OracleDbType.VarChar, 200)
oraCommand.Parameters("value_to_insert").Value = "MY_USER_DEFINED_FUNCTION(23,98,"TEST")
Where "MY_USER_DEFINED_FUNCTION" is a function that returns a string. I have values which can be used by an existing Oracle function to provide the value to be inserted in the DB. Since these values are time critical, I need to call the function as I am inserting the data rather than call Oracle get the data back to the client and then pass it back to Oracle.
EDIT:
I have an existing generic VB.NET function which builds insert statements from an object which contains column name, type & value for each column in the table. Igor's comment below cleared my cobwebs, and I think I've painted myself into a corner.

Need list of columns (fields) in a SQlite table within Visual Basic

I'm building a query application in VS 2012 in which the user drills down by sequentially selecting the Database, then Table, then Column for the search. Database and Table selection work fine. Column selection not-so-much.
Table selection is the following SQL query Command String:
Dim ThisSQLcs As String = "SELECT `name` FROM sqlite_master WHERE type = 'table';"
Similar syntax for getting Column data does not work (type = "column") because there are no column/field types in sqlite_master. I have seen suggestions in other posts that work from the CLI (.schema tablename) or something about PROGMA table_info('table_name'), but neither of those work in this VS / VB context.
Any suggestions would be appreciated.
-larry

SELECT INTO statement not working in VB.NET, but works when ran directly in Access

I am converting old VB6 code to VB.NET with ADO.NET (OleDB). This is my query that will create a blank table when ran in VB.NET, but then works when ran directly in Access. This code presumably also works in VB6, as I am using the same SQL:
SELECT qryAsOf.name, qryAsOf.type, 0 as opt, 0 as swap
INTO qryCon
FROM qryAsOf
LEFT JOIN qryLinked on qryAsOf.c = qryLinked.lc
I feel like this has something to do with the left join and select into being together, but like I said it is only VB that has an issue with it, Access handles it perfectly.
Thanks :)
Edit: more details --
Without the INTO line, this query returns all 600+ rows.
This DOES NOT WORK:
cm.CommandText = "CREATE TABLE qryCon (etc...)"
cm.ExecuteNonQuery()
cm.CommandText = "INSERT INTO qryCon SELECT ..." '(rest of query above without INTO line)
cm.ExecuteNonQuery
This DOES WORK:
cm.CommandText = "CREATE PROC qryCon AS SELECT ..." '(same select as above without INTO, again)
cm.ExecuteNonQuery
The CREATE PROC that does work is fine, except I need to insert data into it later, so I get errors about how I need an updatable table. I really want the end qryCon to be a table, but I can't seem to get that to work :(
*However, when I do something like this (using the stored proc (renamed) above which, if viewed in Access, is full of data)
cm.CommandText = "SELECT * FROM storedProc"
dr = cm.ExecuteReader
while dr.Read
cm.CommandText = "INSERT INTO qryCon VALUES (dr.GetValue(0), dr.GetValue(1), dr.GetValue(2), dr.GetValue(3))
cm.ExecuteNonQuery
end while
This DOES NOT WORK! By the way, I removed the concatenation in the query for readability. It is correct in the actual project.
Your VB6/Access syntax functions where you would iterate through the query object itself when parsing the results.
For .Net, you want to leverage the newer ADO.Net library and use a DataReader (or DataAdapter if you need to work with the results more than once in your code without having to make a second trip to the database) object in order to process your query. You can find a nice overview of how to work with them on the MSDN site.
EDIT:
After your question was updated, you want your SQL query to be this:
SELECT qryAsOf.name, qryAsOf.type, 0 as opt, 0 as swap
FROM qryAsOf
LEFT JOIN qryLinked on qryAsOf.c = qryLinked.lc
and then you'll iterate through the results and parse the values through each loop (as found in the MSDN link with something like
If reader.HasRows Then
Do While reader.Read()
Console.WriteLine(reader.GetInt32(0) & vbTab & reader.GetString(1))
Loop
Else
Console.WriteLine("No rows found.")
End If
Select into syntax may not be supported in the middleware, which tends to be somewhat generic. The statement might have to be executed in"passthrough" i.e. native-syntax mode.
Since my earlier comment about pass-through mode appears to have been downvoted, perhaps by #onedaywhen, I include this link:
http://msdn.microsoft.com/en-us/library/ms681754(v=vs.85).aspx
Jet OLEDB:ODBC Pass-Through Statement
(DBPROP_JETOLEDB_ODBCPASSTHROUGH)
Indicates that Jet should pass the SQL
text in a Command object to the back
end unaltered.
The documentation seems to suggest that the SQL text can be altered by the middleware unless this feature is invoked. So I repeat, you might want to make sure that oleDB supports SELECT INTO syntax. I'm not saying that SELECT INTO is not supported -- just that it is an issue worthy of the OP's attention.
P.S. When talking to a database via a middle layer that is designed to support a variety of back-ends, it is a good idea, I think, to use pass-through mode when the statement contains a function like IIF() or a UDF, or any syntax, such as SELECT INTO, which is not universally supported.
P.P.S. Here's some additional info that might be relevant:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

How to use parameterized queries in vb.net?

How to use parameterized queries in vb.net?
Because I always wanted to make a program that could read an ms access database and display results based on your query.
Create the desired query using question marks in the place of your parameters
Dim sql = "SELECT * FROM Students WHERE Name = ?"
Then create a OleDbCommand object and set the appropriate values (CommandText and so on). Then add Parameters to the command object.
Finally, execute it how you need. Eg as ExecuteReader.