Loading DataTable into SP - c#

I have SQL Server 2008 and VS 2008 Pro. And I am coding in C#. I accept a text input file from the customer and I parse this data into a DataTable in my C# aspx.cs file. I know that this is working correctly. So I populated the DataTable. But now how do I load this data into an SP? Like can I use a dynamic table parameter? Or should I use XML instead?
The problem is that the number of required columns may vary depending on which table they want to insert into. The details are I let the user select which table they want to append data to. For simplicity, let's say:
TABLE_NAME NUM_COLS
A 2
B 3
C 4
And also let's assume that the first column in each of these is an INT primary key.
So if they choose Table B, then DataTable would look something like:
PK C1 C2 C3
1 'd' 'e' '3/10/99'
2 'g' 'h' '4/10/99'
So now I want to append this data above into Table B in my AdventureWorks DB. What is the easiest way to implement this both in the SP definition and also the C# code which calls this SP?
Thank you!

I think I understand what you're asking. I'm going to assume each row of your data import will map directly/cleanly to a table in the database. I am also going to assume your application logic can determine where each row of data shall be persisted.
This said, I suggest working with each row of the .NET DataTable individually rather than passing the data in bulk to SQL as a single stored procedure parameter and then depending on SQL to do any data parsing and table mapping.
Basically, loop through your DataTable, determine the type of data and execute the appropriate insert for each row. I hope this helps.

Related

bulk compare in sql

I have a csv file with one column. Each row in this column may have a match record in TestTable from database (sample table only). I would like to send the entire data via C# to the stored procedure one time and return the rows from the database if it has a match.
Input parameter (csv column):
Stringcolumn
apple
banana
copper
dig
....
Output (possibly dataset):
ID|StringColumn|AddedDate
2|apple|2021-01-02
35|copper|2021-01-02
The requirement is to send and receive the data via C#.
Right now, I'm thinking to use User-Defined Table Types in MS SQL to receive the data.
Thank you.
If its a big csv I would recommend dumping it in a temp table and then using a join to get the data. Alternatively you can use user defined table type to pass the data into a stored procedure

Best way to store program state variables in SQL Server

I'm writing an app in C# that connects to a SQL Server using Entity Framework. Every instance of the app shares the tables and some variables (ints, strings and bools).
What's the best way to share that variables (int string and bools) via tables in SQL Server?
Since tables have fixed type columns, one table would not do it without loosing type-safe in C#, because every type should be converted to string or boxed to object.
The two solutions I came up with are, one table with 3 columns (int, varchar, bool), with the data writing in the appropriately typed column, or 3 tables with one column.
Or maybe I am totally missing the point here..
The question would be: what's the most elegant way to accomplish saving typed data to a SQL Server?
You can use SQL_Variant for that purpose
https://learn.microsoft.com/en-us/sql/t-sql/data-types/sql-variant-transact-sql?view=sql-server-2017
CREATE TABLE tableA(colA sql_variant, colB int)
INSERT INTO tableA values ( cast (46279.1 as decimal(8,2)), 1689)
SELECT SQL_VARIANT_PROPERTY(colA,'BaseType') AS 'Base Type',
SQL_VARIANT_PROPERTY(colA,'Precision') AS 'Precision',
SQL_VARIANT_PROPERTY(colA,'Scale') AS 'Scale'
FROM tableA
WHERE colB = 1689

How do I change the connection string to SQL Server in Excel Power query programmatically?

I'm attempting to create an Excel pivot table based on data in a Microsoft Dynamics NAV database.
The trick is I need the Excel to access the data directly from the SQL Server database with power query - and furthermore it must be able to access the data from the same table in multiple databases with different names and table names.
Does anybody have any experience or advice regarding this issue?
Step 1. First you should make a function where you can pass a server name, database name and table to be queried. Something like
let getData =(servername,dbname,tablename)=>
let
Source = Sql.Database(Servername, dbname, [Query="select abc , def from" & tablename & " where condition etc etc"]),
#"CustomStep1" = some action on Source,
in
.
.
#"CustomStepn" = some action on Added CustomStepn-1
in
#"Added CustomStepn"
in
getData
You have a function ready which you can use in a table to create a custom column.
Step 2. Now use a parameter table approach. Create a table in normal excel area.Something like
Server Name|DatabaseName | Table_to_be_used
Use now use menu option fromtable in powerquery options (or Data tab in Excel 16) Add a custom column in this table in powerquery steps using function getdata created in previous Step. Perform any other "Expand" (By default first function is going to return a table if you are not doing any other transformation) , "summarize" , Rename Operation.
However powerquery formula firewall is going to give you hardtime as powerquery doesn't trust Native SQL queries and you will have to approve each and every native sql query. You may try to uncheck checkbox for "Require user approval for new native database query" in query option.
Hope you get the idea and it helps.
Perhaps it would be worth looking into creating Query objects and exposing them via oData which is something Excel can read from. The benefit here is that it can handle table relations natively and can expose Flow Fields which you cannot see in direct SQL queries to the table.
Aside from a stored procedure to manage the different table names, there's not a simple way to query specific tables without hard coding the names in some capacity.
The Company table will give you the prefix$ and the table names are static between companies. You could write some fancy Excel logic to loop through them.

send and insert Data-table to sqlserver table

I need to send a datatable to sqlserver2012 and, insert datatable into a specific table. How can i do that?
( i don't want do this work in C# i want to send datatable to sqlserver and do this work in sqlserver). I saw similar question, but didn't found my answer.
Edit:
into sqlserver i want insert datatable rows into a table.
Your point is not clear enough for what is your purpose, you can use the Update() method which would perform the operation of updating newly added records to your table, this would implicitly send the table to the sql server and update data
i think you want to save bulk data from DataTable in sqlserver. please peroform following steps
1) Create custom table value data type in sqlserver 2008 onword
2) create stored procedure with custom table value data type parameter.
3) use following code for set parameter value from code
SqlParameter param = cmd.Parameters.AddWithValue("#RECORD_TBL", Datatable);

What SQL DataTypes to look for?

I wrote a code using c#- Visual 2008- that takes:
1- an excel sheet.
2- Opens a connection to Excel
3- Reads the Column names using the OleDbDataReader object and GetTableSchema method
4- Added the Columns names in an array
5- Created a Table that has the same Column names as the EXcel sheet ( i used CREATE table
command)
6-Then once i have the table created in SQL , i loop over the excel rows an add the data
into sql using Insert command.
Now My problem is:
In the " create table" command , i have to specify the DATATYPE for the column !i.e.
CREATE TABLE "table_name"
("column 1" "data_type_for_column_1",
"column 2" "data_type_for_column_2",
... )
HOW should i solve this problem? Are the datatypes that excel columns can be, the same as the datatypes in SQL server? is there some kind of mapping ? please help me out.
thank you
OleDbDataReader.GetSchemaTable will give you what the underlying datatype is from the spreadsheet. This (quote):
Maps to the .NET Framework type of the column.
You then need to map that to the appropriate SqlDbType. Check out this other answer for ideas on how to do that.
The other point I wanted to raise as a side point, was to consider bulk loading the data from excel into SQL Server instead of "read a row / insert a row". I don't know what data volumes you're talking about, but that could speed the process up.
I believe you can use. sql_variant data type if you dont know what kind it is going to be?
i'd take a look at that.
http://msdn.microsoft.com/en-us/library/ms173829.aspx
You can figure out the data type of the column using GetTableSchema()...
DataTable schema = rdr.GetSchemaTable();
foreach (DataRow row in schema.Rows)
{
foreach (DataColumn col in schema.Columns)
Console.WriteLine(col.DataType);
}

Categories