Insert data into SQl Server Database using ADO.Net Dataset - ado.net

I have a small database with a table with 2 columns (ID and Name)
I have an ASP.Net form that i want to input an ID and a Name and upon clicking the button, it will insert rows into the database
I want to do this using a dataset. Meaning, I want to add the rows first into a ado.net dataset. Once I have the dataset with its table and rows, I want to insert data into the SQL Server 2008 database using SQLDataAdpater.
I can manually create a DS and add rows in it
How do I accomplish inserting data to database with the dataset I created?

Maybe this will help you
HOW TO: Implement a DataSet SELECT INTO Helper Class in Visual C# .NET
http://support.microsoft.com/kb/326009/en

Related

Multiple database with the different (database) structure asp.net mvc

I am using MVC 5 with multiple existing database using Entity Framework 6.
The structure is almost the same in each one, except in some case there could be an additional column in a table
Example:
Database_A has table Table_A with Col_1 and Col_2.
Database_B has a newer version of data base structure and has Table_A with Col_1, Col_2 and Col_3
I know how to switch from one database to another but my problem is: How I can make my model that covers both (or more) databases structure?
If you are using entity framework and are writing code that only deals with the columns common to both databases you should have no problem. When you create your db context, just pass it the connection string for the appropriate database and the code should work fine.
Gotchas to be aware of:
1. Creating new records where Col_3 is required. Make sure to have a default value.
2. Using migrations so Entity Framework will try to keep the database matching the model.

Tableau - Blend Multiple Tableau Server Data Sources

I have three Tableau Server data sources in a workbook that represent three tables in a database. Table A is joined to Table B and table B is joined to Table C. There is no foreign key relationship between Table A and Table C, but the implicit relationship exists through Table B (junction table). When using Table A as the primary data source on a report, I receive an error message when attempting to add data from Table C.
In order to use fields from Table C, a relationship needs to be created with Table A. Select Data > Edit Relationships to open the Relationships dialog box.
Is there any way to join multiple Tableau Server data sources to accomplish the described scenario?
Tableau has the concept of Primary and Secondary data sources, and every secondary needs to have a key into the primary to be considered. You can't link a secondary to a secondary data source.
My suggestion here would be creating another data source already joined/blended. This can be done by connecting directly to the database and joining the tables using Tableau wizard (see image below):
Or, if you feel comfortable, you can use the New Custom SQL button and use your own query (see image below):

entity framework map one model to many table

I store data in table named by month,such as data201201,data201202 and so on,table is created in the first day of month,all tables have same struct.I design this because each month ten million rows increase and I have only sqlserver standard version so Partition table can not be used.
Before use entity framework,i used sql string to query data,so I can change tablename in sql string by query condition.
Is these any way that I can query related table in Entity Framework?
I have two idea:
1.another table design that can support such big data and minor cost?
2.Intercept ef's query and change table name by query condition,and continue this query,but how and where i can do so?

PostgreSQL: How to delete dynamically created table using SQL

I am developing a windows application and using Postgres as backend database. At some point in my application i am dynamically creating table e.g Table1, then Table2 and so on. In this way i have many dynamic table in my database. Now i provide a button "Clean Database", so i need to remove all those dynamic tables using SQL query. Should some one guide me how to write SQL Query that automatically delete all such tables?
You should just be able to say
DROP TABLE {tablename}
for each dynamically created table. Try that and see if it works.

Most straightforward way to add a row to an SQL Server table in ADO.NET without hardcoded SQL?

I am wondering what the best / most efficient / common way is to add a row to an SQL Server table using C# and ADO.NET. I know of course that I can just create an SQL statement for that, but first, the destination table schema might vary, so I want to keep this flexible, and second, there are so much columns that I do not want to code and maintain this manually. So I currently use a SqlCommandBuilder that is automatically creating the proper insert statement for me, together with an SQLDataAdapter, like this:
var dataAdapter = new SqlDataAdapter("select * from sometable", _databaseConnection);
new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(dataTable);
// ... add row to dataTable, fill fields from some external file that
// ... includes column names as well,
//.... add some more field values not from the file, etc. ...
dataAdapter.Update(dataTable);
This seems pretty inefficient though to first grab all the records from the table even though I do not need them for anything (especially considering that there might even already be a million records in there). Using some select statement like select * from sometable where 1=2 would work, but it does not seem like a very clean approach. I imagine there is some different solution for this that I am just not aware of.
Thanks,
Timo
I think the best way to insert rows is by using Stored Procedures through the ADO.NET command object.
If you are inserting massive amounts of data and are using SQL Server 2008 you can pass DataTable objects to a stored procedure by using a User-Defined Table Types.
In SQL:
CREATE TYPE SAMPLE_TABLE_TYPE --
AS
field1 VARCHAR(255)
field2 VARCHAR(255)
CREATE STORED PROCEDURE insert_data
AS
#data Sample_TABLE_TYPE
BEGIN
INSERT INTO table1 (field1, field1)
SELECT username, password FROM #data;
In .NET:
DataTable myTable = new DataTable();
myTable.Columns.Add(new DataColumn("field1", typeof(string));
myTable.Columns.Add(new DataColumn("field1", typeof(string));
SqlCommand command = new SqlCommand(conn, CommandType.StoredProcedure);
command.Parameters.Add("#data", myTable);
command.ExecuteNonQuery();
If you data also contains updates you can use the new MERGE function used in SQL Server 2008 to efficiently perform both inserts and updates in the same procedure.
However, if creating User-Defined Table Types and creating stored procedures is too much work, and you need a complete dynamic solution I would stick with what you have, with the recommendation of using the
Where 1 = 0
appended to your SQL text.
You also can use "SELECT TOP(0) * FROM SOMETABLE;" query.