How to address the TableAdapter of a Dataset (made by wizard) - sql

I created a new datasource with 4 tables of a sql database. In my first form i created a to_table_1 _bounded gridview via drag and drop. This created table Adapter and table adapter manager for example, which a bound to the form. Now I have a module1 where I want to perform an Insert Statement from the Table2Tableadapter. But I dont know how i can talk to the adapter, if I not create a gridview anywhere. I want to talk to the Datset.xsd table2 Adapter. Ther I already set up the sql Statements and the Insert statement is nevertheless Standard implemented in the adapter.
I know this must be so easy for someone who worked more then one time with databases. Hope You can help!
Cheers Steven

If you want to do your data access in a module then don't create the table adapter in the form; create it in the module. The Data Source wizard generates classes and you use those classes the same way you would any other. Do you have a problem using a String in a module? Of course not., If you want a String then you create a String. The same goes for a table adapter.
When you drag items onto a form from the Data Sources window it will generate some items automatically. There's no requirement that you keep all those items. Just delete the table adapter and manager and any code that uses them, then write your own code to populate a DataTable in your module. You've still got the DataSet in the form so you simply pass that, or a specific DataTable it contains, to your module to populate it.
You could go further and remove the DataSet from the form too and then use the GetData method of your table adapter to create a new DataTable and return that to the form. It's up to you. Don't limit yourself by what's generated for you. That is there to help but it's not the only way to do things. If you want to create something then create it.

Related

Automation to pull data into excel from SQL

I have a report that I generate on a weekly basis. I have the code written in SQL and I then pull all the data into excel's data model.
I then create pivot tables and dashboards in excel from that particular data.
The SQL code creates new table of the same name everytime and deletes the older version of the table. There isn't any way for me to just append the new data as the report is run from the very start and not just on the new data.
I wish to automate this process of refreshing my dashboard from the data I produce in SQL. Is there a way to do so?
Currently I create a new table in SQL, import data into the excel's data model and then recreate the dashboard.
I am not even sure if this is possible. Any help would be greatly appreciated!
Solved!
After some digging, I was able to find a feature that Excel's data model supports.
Instead of making a connection directly to a SQL Server Table, you can create a connection by writing a SQL Query.
This way, even if you delete the table for updating it, as far as the name remains the same, Excel's data model would be able to pull data from the table just by you hitting refresh!

Can't find class TableAdapterManager in visual studio express 2010

I tried to use this code
Dim Manager1 As New TableAdapterManager()
But it's seem to can't find TableAdapterManager.
Is this class not supported for visual studio express 2010?
That's not how you use a table adapter manager. It is part of a typed DataSet and, just like everything else in a typed DataSet, it gets generated by VS when you create a Data Source.
Use the Data Sources window in VS to create a new Data Source and specify that you want to create a DataSet for your database. That will generate a typed DataSet including a typed DataTable for each table in the database. There will also be a table adapter for each DataTable and a table adapter manager that allows you to save changes using multiple table adapters at once.
You can drag those items from the Toolbox to your form in the designer or you can create them in code. If you create them in code, there will be a namespace dedicated to the table adapters of your DataSet so you must either qualify the type names with that namespace or else import it, just like any other namespace.

updating a database using a table adapter and data table

I am currently working on a VB program connected to an Access database. I have fill a data table using a query and data adapter. At a later stage in the program, i want to go through and make permanent changes to the database using the adapter and table. I tried this:
For Each row As DataRow In db.DBDT.Rows
row("fldsentda") = "Y"
row("flddasenddate") = Date.Today
Next row
'db.DBDT.AcceptChanges()
db.DBDA.Update(db.DBDT)
*db is a class file, dbda is the data adapter, and dbdt is the data table
but i realized these changed are only effected the data table and not the actual database. How can I get it to where it will effect only the database rows that are inside of the data table filled using the query?
Update: I'm thinking my update function isn't written. I don't know if this should be a separate question or not, but how do I write the update function to only change fields in the database that has been changed on the data table??
Do not call
db.DBDT.AcceptChanges()
before
db.DBDA.Update(db.DBDT)
Doing so marks everything in the datatable as not changed. See here especially in remarks section.
Just call the update method and the acceptchanges should be called automatically for you.

ComoboBox Insert New Item to database automatically

I am using a combobox in VB .net to show customer's name. They also can type new names if they wish. Can I somehow save these new names in my sql database dynamically?
What I mean is if the name is new and does not exist in my database, can it be automatically saved to the database?
My combobox is bound to my name table.
Thanks.
If your combobox is bound to a datatable, then you should be able to use a DataAdapter to update the database table based on your datatable.
You could possibly also make this automatic by tying it into the combobox Validated event.

Get column names when you load from an access file

I load 10 tables from an ACCESS 2007 file database. IS their a way I can get the column names into the dataset or do I have to rename each column? I am using Visual Studio 2008 in VB.NET.
I would like to reference the columns in the code by the name and not have to use an index
I just went ahead and added the following code for each of the tables:
Table1.column(0).name = "NAME"
Table1.column(1).name = "ADDRESS"
Table1.column(2).name = "CITY"
Table1.column(3).name = "STATE"
But I do this for 10 tables so it can add up fast on the lines of code. But if their is a way for me to read it from the file I would like to replace all the lines of code with one line of code.
When you create the dataset, if you provide a connection string locally, when you generate the xsd file it should automatically type out any tables you add to the set. As long as you're using the tableadapters built into .Net, you can change the connection string of any tableadapter you use for your local config when you deploy.
When you right-click in the blank space of the new dataset you create (in the designer), you can add->tableadapter. It'll ask you for the connetion string to the database (provide it), and then you can submit a query. Start with the a general query for the basic table you're looking for. You can then add subsequent queries as you need them to the same table or create custom tables (and names) based on joins/other queries.
EDIT: I'm assuming some version of .Net is being used here.