Write datatable to SQL Server - add new, update existing, remove missing - sql-server

I have a SQL Server table that contains three columns:
PositionID, MeasureID, ModifiedDateTime
In my application I load this data into a DataTable and then add and remove rows to/from the DataTable. I would then like to write this back to the database.
The way I was doing it initially: delete all matching records for the PositionID and then loop and insert command with the all the data (or use Bulk Copy).
Is there a better way to do this more simply?

Try this one:
conn.Open();
SqlCommand cmd = new SqlCommand("Truncate Table SQLTableName", conn);
cmd.ExecuteNonQuery();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
bulkCopy.DestinationTableName = "SQLTableName";
bulkCopy.WriteToServer(datatableName);
"conn" is your connection to SQL-server.

Related

Copy records from one database to another (Teradata to SQL Server)

I have a project where I need to query a Teradata database and then copy the records returned to a SQL Server database. I can hit the Teradata db no problem and I can get the results into a DataTable. The SQL server db is already setup and has the same columns as the Teradata results (except for the auto id column). I am having trouble figuring out how to take the records in the DataTable and insert them into the SQL server db.
Here is what i have with some pseudo code where I didn't think the details were relevant:
Using cn As New TdConnection("User Id=XYZ12345;Password=XYZ12345;Data Source=teradataserver.company.com;Persist Security Info=False")
cn.Open()
Dim cmd As TdCommand = cn.CreateCommand()
'build the SELECT part of the command we will issue
cmd.CommandText = GetTeradataSqlString()
'setup the DataAdapter
Dim da As New TdDataAdapter(cmd)
' Provider specific types will be used in the data table
da.ReturnProviderSpecificTypes = False 'True=Use Teradata types, False=Use .NET types
' Adapter will determine how many statements will be batched
da.UpdateBatchSize = 0
Dim cb As New TdCommandBuilder(da)
'create a DataTable to hold our returned data
Dim dtCheck As New DataTable("TableCheck")
' Filling the data table with data retrieved from the select statement
da.Fill(dtCheck)
'create a DataSet to hold all of our tables
Dim dsMain As New DataSet("MainDataset")
'now we add the DataTable to our DataSet
dsMain.Tables.Add(dtCheck)
'at this point a cycle through the DataTable to the debug window shows we have the data we need from the Teradata db.
'now we will pump it into our SQL server database
Dim connSqlSvr As New System.Data.SqlClient.SqlConnection
connSqlSvr.ConnectionString = "Data Source=DestSqlServer;Initial Catalog=DestDb;Connect Timeout=15"
connSqlSvr.Open()
'now we create a SQL command to take the data in the Teradata DataTable and insert it into the SQL server table
Dim sqlCmd As New SqlCommand
With sqlCmd
.CommandType = CommandType.Text
Dim sbSqlCmd As New StringBuilder
sbSqlCmd.AppendLine("INSERT INTO [DestDb].[dbo].[Events] ([CityCode],[CarNum],[VIN],[Fleet],[EventItm])")
sbSqlCmd.AppendLine("SELECT City,CarNo,VIN,Fleet,EventDesc FROM #MyTable;")
.CommandText = sbSqlCmd.ToString
Dim sqlParam As New SqlParameter
sqlParam.ParameterName = "#MyTable"
sqlParam.SqlDbType = SqlDbType.Structured
sqlParam.Value = dtCheck
sqlParam.TypeName = "TableCheck"
.Parameters.Add(sqlParam)
.Connection = connSqlSvr
Dim rowsAffectedLoad As Integer = .ExecuteNonQuery()
debug.print(rowsAffectedLoad & " rows were loaded into the SQL server table.")
End With
'close and dispose the SQL server database connection
connSqlSvr.Close()
connSqlSvr.Dispose()
End Using
Running the code I get an exception:
"Column, parameter, or variable #MyTable. : Cannot find data type TableCheck."
I've looked for a method to insert a DataTable into a database and noticed many samples were using the INSERT INTO. I just dont think I am using the SqlParameter properly.
Your example appears to be using a Table Valued Parameter of type TableCheck but you have not defined that type within SQL Server. See http://msdn.microsoft.com/en-us/library/bb510489.aspx
CREATE TYPE LocationTableType AS TABLE
( LocationName VARCHAR(50)
, CostRate INT );
Although I can't guarantee that you can pass a TVP directly into a raw SQL statement.
I would actually suggest you use a different approach using SqlBulkCopy, http://msdn.microsoft.com/en-us/library/ex21zs8x(v=vs.110).aspx .

How to get the auto increment primary key in old mdb database?

I have to retrieve the Auto_Increment primary key (id) after inserting a new row in a mdb access database.
Having mdb file in the old Access 97 version too, I cannot use "SELECT ##Identity;", because it is not supported.
Besides, in a multi users environment I do not like the idea to use Max(ID).
Actually the only solution I have, it is to use the DAO as:
Dim db As Database 'Test Database
Dim rs As Recordset 'Test Table
...
rs.AddNew
id = rs!id
but I have to add a reference to the DAO COM DLL, while I would like to have a full managed .NET code.
Do you have any suggestion how to retrieve the Auto_Increment primary key (after inserting a new row) without using DAO or Max(ID)?
Here my code to add a new row:
Using oConn As New OleDbConnection(m_ConnString)
oConn.Open()
Using cmd As New OleDbCommand(sqlInsert, oConn)
cmd.ExecuteNonQuery()
End Using
End Using
Thank you.
If there is any other mandatory field in this table that has a unique index, you may query its value after your insertion with something like DFirst("ID", "tablename", "myfield = givenvalue"). If you're running in a transaction, use a recordset (perhaps the same as for the insertion) instead of DFirst.

Change datatable and tableadapter schema at runtime according to stored procedure. C#

I have DataSet and TableAdapter for the table in that DataSet. The problem is that TableAdapter uses a stored procedure to fill DataTable, but the amount of returned columns can be different.
How can I change the schema of DataTable at runtime to get all the columns from stored procedure?
In my opinion, drop the idea of using the DataAdapter, it's completely unnecessary. Do this to get the DataTable instead:
using(SqlConnection con = new SqlConnection("Conectionstring"))
{
con.Open();
using (SqlCommand commnand= new SqlCommand("StoredProcName",con))
{
command.CommandType=CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
DataTable table = new DataTable();
table.Load(reader);
return table;
}
}
Now it doesn't matter the # of columns you return from the proc, the datatable will contain all of them.
EDIT: Add the following section to your Web.config file below the <configuration> section replacing the actual connectionString for yours. You should be able to copy this from the Your Settings file.
And now create your SqlConnection as follows:
SqlConnection con
= new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
Notice that "ConnectionString" is the name attribute of the connection string in the Web.Config.
See another example here, in case is not very clear.

save DataTable to database

Hi, I am generating a DataTable from a webservice and i would like to save the whole DataTable into one database table.
DataTable ds = //get info from webservice
The DataTable is getting generated but What to do next .I am getting stuck .Show me some syntax.I dont really need the select statement there either, i just want to insert all the info from the DataTable into a blank db table.
Use bulkcopy,
this is the code. And make sure that the table has no foriegn key or primary key constraints.
SqlBulkCopy bulkcopy = new SqlBulkCopy(myConnection);
bulkcopy.DestinationTableName = table.TableName;
try
{
bulkcopy.WriteToServer(table);
}
catch(Exception e){messagebox.show(e.message);}
This is exactly what SqlBulkCopy is for. Check it out: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

Create DB table from dataset table

Is it possible (in Vb.Net 2005), without manually parsing the dataset table properties, to create the table and add it to the database?
We have old versions of our program on some machines, which obviously has our old database, and we are looking for a way to detect if there is a missing table and then generate the table based on the current status of the table in the dataset. We were re-scripting the table every time we released a new version (if new columns were added) but we would like to avoid this step if possible.
See this MSDN Forum Post: Creating a new Table in SQL Server from ADO.net DataTable.
Here the poster seems to be trying to do the same thing as you, and provides code that generates a Create Table statement using the schema contained in a DataTable.
Assuming this works as it should, you could then take that code, and submit it to the database through SqlCommand.ExecuteNonQuery() in order to create your table.
Here is the code:
SqlConnection con = new SqlConnection("Data Source=.;uid=sa;pwd=sa123;database=Example1");
con.Open();
string sql = "Create Table abcd (";
foreach (DataColumn column in dt.Columns)
{
sql += "[" + column.ColumnName + "] " + "nvarchar(50)" + ",";
}
sql = sql.TrimEnd(new char[] { ',' }) + ")";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
using (var adapter = new SqlDataAdapter("SELECT * FROM abcd", con))
using(var builder = new SqlCommandBuilder(adapter))
{
adapter.InsertCommand = builder.GetInsertCommand();
adapter.Update(dt);
}
con.Close();
I hope you got the problem solved.
Here dt is the name of the DataTable.
Alternatively you can replace:
adapter.update(dt);
with
//if you have a DataSet
adapter.Update(ds.Tables[0]);

Resources