Using WCF Data Services to copy one database table to another - c#

I am little stuck in using LINQ to insert one database table to another database table located on another server as a WCF Data Service.
Suppose I have an Item class on the local database and the same Item class on the the remote server, and I want to copy all the records across.
Is there a possibility to do this from: -
private Uri svcUri = new Uri("someurl/WcfDataService.svc");
Entities = new Entities(svcUri);
.....
I know that LINQ to SQL is mostly a 1-1 mapping between classes and the database, but I heard it is possible.

You just need to construct a connectionstring pointing at the right database. That database has to have the tables the EF expects to have though. (The code would look like yours, but I've never used a Uri for a connection string.)
However, it would be more efficient to do via a stored procedure on your source database, via a Linked Database to y=the Target. This because if you do it via WCF the data has to travel twice: SourceDB -> WCF service -> TargetDB, whereas with a SP it only has one hop: SourceDB -> TargetDB. Also in the SP it will operate pretty much as a set operation whereas the WCF service will have to process one row at a time.
EDIT - Apologies: I didn't notibe the MySQL tag. I don't know whether MySQL supports Linked Databases, so feel free to ignore this if it doesn't.

Related

How to resolve or skip the share issue of SQLXMLBulkLoad (transaction mode)

I need to import xml file into Microsoft SQL Server database. And I have a xml schema file (.xsd) that contains the mapping info for the xml file to be imported. Each valid data line in xml file need to be imported into a database table row with several columns.
Originally I was using SQLXMLBulkLoad (Non Transaction Mode - specify a database connection string) to do the job and it works just fine, however, there is a new requirement that need me to execute a stored procedure on the same database connection that SQLXMLBulkLoad uses. The purpose of this stored procedure is to bind the connection with a specific ID for the current user because there might be multiple users importing xml file at the same time. So I have to use transaction mode instead (- specify a database connection rather than a connection string for SQLXMLBulkLoad) and which introduce the well known share issue under remote environment. If the xml file is local and the db server is remote, I need to create a shared folder for SQLXMLBulkLoad to work. This is bad but can be done.
The problem is the share folder thing is not accepted by our clients, so I need a better solution that could satisfy the new requirement without the share issue.
I've Googled a while and found one solution that might work - SqlBulkCopy, but I have trouble with parsing the schema file (.xsd) because it contains many xml-db:table mapping information.
Any idea would be appreciated.

C# entity framework for loop dynamic connection string

I am a DBA and have decided as a project to help me learn c# / ef to build an app that monitors sql servers, the idea is to develop a windows service that runs to collect all the stats from various sql instances using scheduled jobs in .net quartz. i.e. connected users and details from various dynamic management views.
to store the configuration data i.e. what servers to monitor I am using an sql database so it just contains a table of servers to monitor you will be able to add move via a web ui.
now the issue I have is how to loop through the servers "table" in EF and obtain the connection strings column and use this to connect to the various sql instances to get stats. (these will later be written back to the database for analysis by a front end)
e.g.configuration table data:
servername: test server
connectionstring: testserver\inst1
hope that all makes sense, thanks for your time
You can create a list of all your connection string keys like this-
List<String> DataVaseKeys = new List<String>(); DataVaseKeys.Add("testserver\inst1");
DataVaseKeys.Add("testserver\inst2");
foreach (var key in DataVaseKeys) {
string currentConString=System.Configuration.ConfigurationManager.
ConnectionStrings[key].ConnectionString;
//access to the data base with your connection string here
}

Add column to Azure SQL Database and display using Azure Mobile Services

I'm a newbie to dealing with Azure SQL Databases and Mobile Services. Does anyone know if it is possible to add a column to Azure SQL Database without having to go through the publish process in Visual Studio? When I download the mobile services project from Azure, I can publish the service and it will run with a new ToDoItems table. So I open up a SQL query in Visual Studio and add a column and add some random data to the new column 'Individual':
ALTER TABLE Numboo.TodoItems
ADD Individual nvarchar(max)
INSERT INTO Numboo.TodoItems (Text,Complete,Deleted,Individual)
VALUES ('90987834',1,0,'test data xxxxx')
But when I request the data from the table using mobile services, the data in the new column is null when I query the table
private MobileServiceCollection<TodoItem, TodoItem> demo;
private IMobileServiceTable<TodoItem> demoTable = App.MobileService.GetTable<TodoItem>();
demo = await demoTable.ToCollectionAsync();
//Individual is null
instead of "test data xxxxx" which I inserted via a sql query. Does anyone know if this is even possible to do it this way (add a column via script and access it using mobile services)? Or point me in the right direction or to an answer on SO if there is one? I'm not sure what I would be searching for. Thanks.
NB: I have updated the model/entity to reflect the new column in the database but that didn't make any difference:
You do need to deploy your service with an updated model that matches the table schema/the model you want your API to expose. It's not enough to just change the client model.
Now, your question is specifically about publishing from VS, and although a deployment is required, you have other deployment options, including continuous deployment, which you can learn more about here:
https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-store-code-source-control/

Inserting different data simultaneously from different clients

I created a windows forms application in C #, and a database MS SQL server 2008 Express, and I use LINQ-to-SQL query to insert and edit data.
The database is housed on a server with Windows Server 2008 R2 (standard edition). Right now I have the application running on five different computers, and users are authenticated through active directory.
One complaint reported to me was that sometimes when different data is entered and submitted, the same data do not appear in the listing that contains the application. I use try catch block to send the errors but errors do not appear in the application; but the data simply disappear.
The id of the table records is an integer auto-increment. As I have to tell them the registration number that was entered I use the following piece of code:
try{
ConectionDataContext db = new ConectionDataContext();
Table_Registers tr = new Table_Registers();
tr.Name=textbox1.text;
tr.sector=textbox2.text;
db.Table_Registers.InsertOnSubmit(tr);
db.SubmitChanges();
int numberRegister=tr.NumberRegister;
MessageBox.Show(tr.ToString());
}
catch{Exception e}
I wonder if I'm doing something wrong or if you know of any article on the web that speaks how to insert data from different clients in MSSQL Server databases, please let me know.
Thanks.
That's what a database server DOES: "insert data simultaneously from different clients".
One thing you can do is to consider "transactions":
http://www.sqlteam.com/article/introduction-to-transactions
Another thing you can (and should!) do is to insure as much work as possible is done on the server, by using "stored procedures":
http://www.sql-server-performance.com/2003/stored-procedures-basics/
You should also check the SQL Server error logs, especially for potential deadlocks. You can see these in your SSMS GUI, or in the "logs" directory under your SQL Server installation.
But the FIRST thing you need to do is to determine exactly what's going on. Since you've only got MSSQL Express (which is not a good choice for production use!), perhaps the easiest approach is to create a "log" table: insert an entry in your "log" every time you insert a row in the real table, and see if stuff is "missing" (i.e. you have more entires in the log table than the data table).

updating changes from one database to another database in the same server

I have a copy of client database say 'DBCopy' which already contains modified data. The copy of the client database (DBCopy) is attached to the SQL Server where the Central Database (DBCentral) exists. Then I want to update whatever
changes already present in DBCopy to DBCentral. Both DBCopy and DBCentral have same schema. How can i do it programatically using C#.NET maybe with a button click. Can you give me an example code as how to do it?. I am using SQL Server 2005 Standard Edition and VS 2008 SP1.
In the actual scenario there are about 7 client database all with same schema as the central database. I am bringing copy of each client database and attach it to Central Server where the central database resides and try to update changes present in each copy of the client database to central database one by one programatically using C# .NET . The clients and the central server are physically seperate machines present in different places. They are not interconnected.
I need to only update and insert new data. I am not bothered about deletion of data.
Thanks and regards
Pavan
Go check out Sync Framework. Otherwise, go create some SSIS packages and run them.
What you are describing sounds an awful lot like Database Mirroring.
If this is a one-time or infrequent sync, then I'd use a third-party tool such as Red-Gate's SQL Data Compare. If this is meant as an ongoing sync, then I'd recommend replication or the Synchronization Framework.
i think this is wht u want:-----
USE DB1
UPDATE DB1.dbo.MyTable
SET
Field1 = d2.Field1,
Field2 = d2.Field2,
Field3 = d2.Field3
FROM DB2.dbo.MyTable d2
WHERE d2.MyKey = MyTable.MyKey

Categories

Resources