I'm writing an C# application that watch for newly created xml files, and I want to insert some of the data in these XML files to a SQL database. One file is one row in the database.
What is the best and easiest way to do this?
What I have done so far is to defined a DataTable with the columns I want from the XML. After that I use ReadXML on the DataSet. This give me a DataSet with one table and one row, and the columns I want. So far it's perfect. But I can't find a good way to ONLY insert this new DataRow into my MSSQL database. I don't have any unique ID for that row yet. Would this make it easier? I don't want to map my dataset with the database, I'm only doing an insert...
You can do something like this to get the ID:
string query = "INSERT INTO Employees (EmployeeID, Name, Phone) "+
"OUTPUT INSERTED.EmployeeID "+
"VALUES (NEWID(), 'John Kris', '99-99999')";
Guid lastId = (Guid)command.ExecuteScalar();
The rest of the things you do sound ok for me.
I used LINQ to SQL to solve this. I set it up somewhat like described here:
LINQ to SQL.
On the data context I used InsertOnSubmit.
Related
I am trying to implement a database in one of my project. The table thats giving me a problem is structured as follows.
Table Savegame:
SaveID(autonumber),
Username(Long text),
GameTitle(long text),
Savestate(short number)
I am trying to insert into the table using the following code
cmd.CommantText - "INSERT INTO Savegame(Username,GameTitle,Savestate) Values('"+username+"','"+game+"','1')";
Everytime I run the code I'm told that I cannot leave the SaveID field empty since its NOT NULL.
Any help would be much appreciated.
At least, supply a number for SaveState:
cmd.CommantText - "INSERT INTO Savegame(Username,GameTitle,Savestate) Values('"+username+"','"+game+"',1)";
Also, are you sure, that the other fields are Long Text, not Short Text?
I have HTML source and want to import specific tags from the HTML source to database-table " MySql " by using C#.
any suggestion would be great.
Your question is very abroad and we need some enlightment on your question, first off:
Do you have a database yet?
Do you have a connection with that database?
Have you set up a way to fire queries at that database?
Have you created a user that is allowed to fire queries at the database?
Why not save every unique column as a column in the database? pasting plain HTML into a database isn't really good practise.
Create a table named test_table with the column ID with an auto increment and a column data with the type longtext.
for the query:
INSERT INTO `test_table` (`data`) VALUES ('<a>someHTML code</a>')
I have created one DataTable in C# dynamically. I want to insert this whole DataTable in MySql database table.
I am using MySql hence SqlBulkCopy is not an option for me.
I have tried MySqlBulkLoader but it seems it only insert data from file.
I can do it via iterating through every datarow of datatable and insert it to database.
Is there any another way I can achieve multiple inserts into database?
Thanks for any help!
If the number of rows are not much you can simply create an insert statement
INSERT INTO table VALUES (1,2,3), (4,5,6), (7,8,9);
Another article at http://www.codeproject.com/Articles/19727/ADO-NET-Generic-Copy-Table-Data-Function may help.
Though, if the number of rows are high, you can probably create a temporary text file and then use BulkLoader !
I'm trying to store a DataTable into a single column in a SQL Server table. The idea behind this is a user runs a SQL command and the returned output is stored into a datatable, then I want that datatable to be stored into a SQL Server logging table. Later on I want to be able to retrieve that entire datatable back for displaying on a logging aspx page.
Currently I'm just storing it as a big string but that doesn't give me column headers and the formatting is kinda funky as well as being inefficient.
TIA
I would probably convert the datatable to XML and store it into an XML field type if I was going to do what you are trying to do.
Hello you can try with WriteXml
this link give you sample interessant : http://msdn.microsoft.com/fr-fr/library/system.data.datatable.writexml.aspx
Another Idea is to create two tables in your database. It is slightly complex.
One Table contains two columns, Let name the table PTable.
Columns:
ID and ColumnID
ID is the primary key and ColumnID contains the name of your column in datatable
After creating this table create another table. It will consists of three fields. Let name it STable. This table stores the columns of you datatable.
Columns:
stblID, PtblID and PtColumnID
StbID is the primary key in this table, PtblID is the Primary key of PTable and PtColumnID is the ColumnID of PTable. This table stores the rows of table. Now store the data in this table and receive the data when you need it.
and the simplest idea is to create a table in your datbabase having an xml column and store your datatable as an xml.
i need create query dynamically in my .cs page. Actually what happens in my application is user can pass the column name and Table names from the page. by using those parameters(columns and tables) we need build Select query . we need retrieve those values from database and show in text format to user.there may multiple table and single table have multiple columns also.
please try yo help how to do this ny using asp.net3.5 and c3.net
So you need to have a dynamic query that populates its columns as per users requirement. It is not achievable but I have a nearby solution of it.
You can specify from which tables you want data and then using system functions create dynamic query of yours and get it done.
If nothing else, just create the query string dynamically and then pass it to the ADO.NET routines.
string qry = String.Format("SELECT {0} FROM Table", colName);