I have made a dataset in a Visual Studio project. It has the default queries, that being Fill and GetData, but I will be adding more in the future
I cannot find any resource or information regarding how to use these queries in my code. I have configured the functions with the default values, and I have searched up any relevant search term. However, I still have not found any way to cajole these queries into being useable in my code
How do I use these dataset queries?
When you add a new query to a table adapter in the DataSet designer, you are prompted to provide the method names by which the query will be invoked. The default methods are Fill, which populates an existing DataTable, and GetData, which returns a new DataTable. When prompted, the names used are FillBy and GetDataBy and the idea is that you add whatever column(s) you are filtering by. For instance, if you add the query "SELECT * FROM SomeTable WHERE SomeColumn = ?" then you should provide the method names FillBySomeColumn and GetDataBySomeColumn. You would then, for example, call FillBySomeColumn instead of Fill and provide the filter value as an argument, e.g. instead of this:
exampleTableAdapter.Fill(exampleDataSet.SomeTable)
you would use this:
exampleTableAdapter.FillBySomeColumn(exampleDataSet.SomeTable, someValue)
Related
I need a little help. I have a one Dataset, you can see on image and I want to copy some rows in Other dataset to use it further. For that we can use Dataset2 = Dataset1.clone() function to get the dataset structure but how can we copy the rows? enter image description here
The simplest option is to use LINQ to DataSet, e.g.
Dim newTable = oldTable.Select(Function(dr) dr.Field(Of Integer)("Count") > 10).CopyToDataTable()
That will create a new DataTable with the same schema as the old one and copy all the rows where the Count column is greater than 10. You can use whatever condition(s) you want in that lambda expression. Note that you will have to have referenced the appropriate assembly and imported the appropriate namespace. The namespace (System.Data) is probably covered automatically but the assembly/package may or may not be. For WinForms and .NET Framework, it likely will be. For .NET 5, likely not, so you'd need to add the appropriate package.
I want to implement a user defined type (table) in SQL Server (2008 R2) that is passed from a VB.NET application. This will be a generic type used in place of passing comma-delimited lists (at least initially it will be exclusively ID values).
If it is decided later that we need to modify the UDT, will we also need to change everywhere in the application code that we attempt to use the UDT? Or can we get around having to do such a thing by making a new column in the UDT nullable?
The concern is that if we want to add a new column to the table we will need to go back and change all the places this UDT is used in the application, whether those places will need the new column or not.
You should be able to add columns to the UDT as long as they DO allow NULL values.
As far as the application code, it will depend on what you are calling. If you want to add data to the new column, you will need to change the code where you are inserting rows. If you are retrieving the data, you will need to change the code to get the data from the new column.
Are you asking if changing the UDT on the server side will require immediate changes to your code? You should not need to. SELECT queries in VB require you to access each column specifically after each query so additional columns should not affect the code. Same with inserting rows: if you are using cmd.CommandText = "INSERT INTO table (field1, [field2, ... ]) VALUES (value1, [value2, ...])" then each column is specifically labeled and additional columns should not affect the application unless the no not allow NULL.
I'm trying to make a generic reporting system so that I can add reports to my program on the fly, rather than issuing a new version of the program every time I have to add in a report.
Currently, I have it partly working;
My custom report SQL is stored in a table, along with the report name. The report name is used for the buttons for the user to click, when they click the button, I want the SQL to execute and be bound to a gridview, so it's as generic as possible.
This seems to be where the troubles come though. Getting the SQL and executing it is fine, but binding it to a gridview seems impossible, I only get one column, called Column, that displays 1 row with a value of System.Object.
I think the problem is because I'm returning data from multiple tables, i.e.
SELECT c.CertificateName, e.EntityName,
ce.CertificateDate, ce.CertificateExpiry, ce.CertificateNumber
FROM FCERTSTest.dbo.CertificateEntries ce
INNER JOIN FCERTSTest.dbo.Certificates c
ON c.CertificateID = ce.Certificate_ID
INNER JOIN FCERTSTest.dbo.Entities e
ON e.EntityID = ce.Entity_ID
WHERE FirstNotificationSent = '1'
I'm currently using
using (DBContainer db = new DBContainer())
{
grid.DataSource = db.Database.SqlQuery<dynamic>(SQLCommand).ToList();
}
I've tried replacing dynamic with object and get the same problem...
I understand that the best solution would be to have a custom class and replace dynamic with that custom class, but then I lose the benefits of it being generic as a different report may not return the same results and I would need a new custom class...
Use SqlDataAdapter and DataSet. EF doesn't work with "dynamic queries" where you don't have special type for result set.
I used the DataSet Designer to create FTWDataSet which holds the AlarmText table from a SQLExpress database. This far my form contains ONLY Datagridview1. The code below successfully shows the contents of the AlarmText table plus the one added checkbox column (which I will populate with display-only data, and is not an issue here).
Dim ta As New FTWDataSetTableAdapters.AlarmTextTableAdapter
Dim dt As New FTWDataSet.AlarmTextDataTable
ta.Fill(dt)
DataGridView1.DataSource = dt
'create a new Bool column in the datatable
dt.Columns.Add("NewCol", (New Boolean).GetType)
What else do I need to do to use the DataGridView to edit and save values in the AlarmText table?
Here's a brief MSDN walkthrough on this topic.
Some notes:
You shouldn't need a binding source to persist changes back to the database.
To make the table adapter accessible to other procedures in your form, make it a form-scoped (a.k.a. member) variable instead of method-scoped, as in your example.
If you created your Dataset using the Dataset Designer, and you're fetching your original data from anything more than a simple table or view, then your adapter won't know how to update anything in the original database. You have to manually configure the UPDATE command in this situation. For reference, see the TableAdapter Update Commands section in the above link.
I should also mention that I avoid TableAdapters in ADO.Net like the plague. In theory they're very convenient and powerful. In practice, many of them (especially the one for the Oracle provider) are buggy, and if they don't work exactly right you're totally screwed.
If you're pulling from just one underlying table, the adapter should work fine (so ignore my nihilistic advice for the time being). It may be that adding an additional column in code will breaks the adapter (since there's no corresponding column in the database table).
Can a DevExpress XtraGrid be filled using a a Dynamic SELECT statement string?
i.e.
SELECT * FROM Employee
or
SELECT * FROM Dependents
To fill our XtraGrids currently, we use a ORM that creates entities. To do this it takes a lot of steps and time. What is driving me to ask this question is we do a lot of client aquistions where we get hundreds of files. I do not want to create entities for all of these then have to create an XtraGrid for each entity. Ideally if I can just feed it a SELECT statement and the XtraGrid could render it then I could use the XtraGrid very nice data minipulation features (Filter, Group By, etc).
If you have any other ideas or suggestions please do not hesitate to post them.
Revised:
Per Brendon's responce below to create a static method that returns a DataTable.
Here is a link to a page that actually does that. http://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-retrieve-a-datatable-using-a-sql-statement.aspx
The XtraGrid doesn't have any built-in mechanisms to create a data source from a SQL statement. For me, the easiest way to accomplish this is to simply create a static method that builds and returns a DataTable/DataSet from a SQL statement. I can then set the XtraGrid's data source to the results of this method.