I've to retrieve information of an existing system which is using MS Access( :'( ).
So I added an existing Item(the mdb) to the project, and it created me a DataSet corresponding to all tables I've.
Once I've done this, I try to access to these data:
ClsDataSet dataSet = new ClsDataSet();
foreach (ClsDataSet.DOCDOCUMENTSRow docdocumentsRow in dataSet.DOCDOCUMENTS)
{
System.Console.WriteLine(docdocumentsRow.nom_document+"-->"+docdocumentsRow.nom_fichier);
}
System.Console.ReadLine();
Only to test...
But it doesn't enter in the foreach, it seems it thinks that the table is empty? I directly step on the ReadLine();
So did I missed something? I don't have any exception..
Should I load the table or something like this?
Thank you!
In the snippet you have posted there isn't any TableAdapter that fills your schemas in the dataset you have created. DataSet is just a representation of what you have on db-side, so it is only a structure of db tables and doesn't directly contain data.
To fill your schema with database records you have to call Fill method of the associated TableAdapter. Usually it is created automatically by Visual Studio Designer when you drag-n-drop some database table in a DataSet schema (.xsd).
Just creating the data set using the constructor does not yet retrieve the data. You need code to fill (=> select the rows from the MDB file and store them within your data set instance) the data set first.
The easiest would be to create an ODBC connection to the MDB file and use OdbcConnection and respective OdbsCommand instances to create the actual connection and "SELECT" statements.
EDIT
You may be able to connect to the MDB file directly using the Table Adapter designer. You would then not need to create an ODBC connection.
Here is a tutorial about connecting to an Access DB - http://www.homeandlearn.co.uk/csharp/csharp_s12p4.html. There is a link at the bottom to another page on that site on how to retrieve data.
Related
I have created a database and i have linked it with a Windows Form Application
in Visual Studio and it is written in Visual C#. I am curious to know whether or not the buttons that i have added in the windows form application will correspond with
the statements that are written in SQL .
Will I need to implement code in the backend of SQL or C# or just one of the two.
You could try using something called Entity Framework, which can be installed if you right-click the project name and choose to Manage NuGet packages.
This will set up a framework for you so that you don't have to write any C# code connecting your form with your database. It is some what easy to use because it creates Table Adapters which makes the connection from a table in your database to your code, an example would be:
var myTableAdapter = new myDatabase_TableAdapters.myTableTableAdapter();
System.Data.DataTable myDataTable = myTableAdapter.GetData();
Now you have the data from your table in the SQL database in a DataTable and you have your TableAdapter as a connection between the two.
Say you want to take input from your users, then for our example consider the data comes from your text boxes then you could do something like:
string vehicleRegNum = vehicleRegNumTextBox.Text,
make = makeTextBox.Text,
engineSize = engineSizeTextBox.Text,
dateReg = dateRegTextBox.Text,
rentPerDay = rentPerDayTextBox.Text;
bool avail = availCheckBox.Checked;
myTableAdapter.Insert(vehicleRegNum, make, engineSize, dateReg, rentPerDay, avail);
This .Insert will add this data to your database (Here I am assuming all the text boxes go to one table in your database and that the order is as I have given them).
So in general,
Get Entity Framework setup in NuGet
Setup a table adapter from a table in your database
Update it with user input
A side note: I always like to add a user and date column to these kinds of tables so you can use:
string usr = System.Environment.UserName
var entryDate = System.DateTime.Now;
Clarification: It was pointed out to me by #DanRayson in the comments that I should be more clear about myDatabase_TableAdapters. It is not exactly a TableAdapter, it is however a TableAdapter which EntityFramework creates in the background for the user.
I'm using a DataSet with the DataSet designer on xsd file (System.Data v 4 in VS 2015). I've been searching for how to get the ID for a newly created record. I see on the designer advanced options there is an option for reading back the record for insert and updates. I also see the SELECT included in the INSERT INTO statement. However the designer generated code runs ExecuteNonQuery and returns the number of records affected. How do we get access to that record that was read back in?
Most solutions I've read involve creating your own query or sp that is set to ExecuteScalar and selects the ##ROWIDENTITY. Either that or timestamp the new record and get it back that way. But if the read-back is already being done then I should be able to get to it, right? What' the trick? What happens to that data that's been read back in?
TIA,
Mike
I just can't understand this thing: I have an access DB I want to query using c#. I read there are this useful tableadapters that make most of the job for you.
Here is what I have done:
Added new data source to the project though the wizard
Added a table adapter via the toolbox. The table adapter has a query named GetData that I want to use to retrieve results.
I add these lines to the code to make my textbox show my first result
Database_CeciDataSetTableAdapters.BaseTableAdapter tableadapter = new Database_CeciDataSetTableAdapters.BaseTableAdapter();
textBox1.Text = tableadapter.GetData()[0].ToString();
Here is what I get in the textbox:
WordAddIn5.Database_CeciDataSet+BaseRow
Can anyone tell what am I doing wrong?
I'm consuming a 3rd-party web service that outputs a Dataset (from XML). I'd like to create a new table in my local MS Access database that contains the DataSet data.
Is there a simple way to hand-off the DataSet to a .net object and ask it to "create a table from this"?
I know that we can use different parts of ADO to extract schema, build commands, insert rows, etc. I figured there has to be a simpler way.
The only two ways I know of are to
Walk through the DataSet field by
field and generate a DDL instruction
(which is valid for MS-Access)
Add a reference to ADOX, create a new table (with columns) and append the new table to the ADOX catalog. More info here. But again you are walking throught the dataset table field by field.
I haven't provided much detail on either of these approaches since I don't think they match what you've specified.
It seems you are looking for a quicker way than either of those so I guess the answer to your question is no.
I am working on a feather that export some tables(~50) to a disk file and import the file back to database. Export is quite easy, serialize dataset to a file stream. But when importing: table structure need to be determined dynamically.What I am doing now :
foreach table in dataset
(compare table schemas that in db and imported dataset)
define a batch command
foreach row in table
contruct a single insert sqlcommand,add it to batch command
execute batch insert command
this is very inefficient and I also I meet some problem to convert datatype in dataset datatable to database datatable. So I want to know is there some good method to do so?
Edit:
In fact, import and export is 2 functions(button) in program, On UI, there is a grid that list lots of tables, what I need to implement is to export selected tables's data to a disk file and import data back to database later
Why not use SQL Server's native Backup and Restore functionality? You can do incremental Restores on the data, and it's by far the fastest way to export and then import data again.
There are a lot of very advanced options to take into account some fringe cases, but at it's heart, it's two commands: Backup Database and Restore Database.
backup database mydb to disk = 'c:\my\path\to\backup.bak'
restore database mydb from disk = 'c:\my\path\to\backup.bak'
When doing this against TB-sized databases, it takes about 45 minutes to an hour in my experience. Much faster than trying to go through every row!
I'm guessing you are using SQL server? if so I would
a) make sure the table names are showing up in the export
b) look into the BulkCopy command. that will allow you to push an entire table in. so you can loop through the datatables and bulk copy each one in.
using (SqlBulkCopy copy = new SqlBulkCopy(MySQLExpConn))
{
copy.ColumnMappings.Add(0, 0);
copy.ColumnMappings.Add(1, 1);
copy.ColumnMappings.Add(2, 2);
copy.ColumnMappings.Add(3, 3);
copy.ColumnMappings.Add(4, 4);
copy.ColumnMappings.Add(5, 5);
copy.ColumnMappings.Add(6, 6);
copy.DestinationTableName = ds.Tables[i].TableName;
copy.WriteToServer(ds.Tables[i]);
}
You can use XML serializatin but you will need good ORML tool like NHibernation etc to help you with it. XML Serialization will maintain its data type and will work flowlessly.
You can read entire table and serialize all values into xml file, and you can read entire xml file back into list of objects and you can store them into database. Using good ORML tool you will not need to write any SQL. And I think they can work on different database servers as well.
I finally choose SqlCommandBuilder to build insert command automatically
See
SqlCommandBuilder Class