Linq to SQL DBML Access across a Linked Server? - linq-to-sql

I have a DBML on a single database in a named instance. The instance has a linked server to another MSSQL database in another server/instance. Both instances and databases have the same dbo-level user. Both databases are MSSQL 2008.
I have some C# code (Framework 3.5) that uses the DBML that accesses the tables and views and sprocs on DatabaseA. I now need to access a table on DatabaseB. What is the smartest way for my code to get to the table/entity over the linked server connection?
Thanks.

One clean way of doing this is to create views inside DatabaseA that encapsulate enities on the other side. You will have to manually define the primary keys and relationships for these entities in your .dbml file. Once this is done they can work just like any other table with CRUD functionality as long as the DTC service is running on DatabaseA.

Try adding a linked server to your local:
EXEC sp_addlinkedserver
#server=N'SERVER',
#srvproduct=N'',
#provider=N'SQLNCLI',
#datasrc=N'SERVER';
SELECT * FROM sys.servers
EXEC sp_addlinkedsrvlogin '<SERVER>', 'false', '<DOMAIN>\<USERNAME>', '<USER>', '<PASSWORD>';
And access your local referring to the linked server:
SELECT * FROM SERVER.DB.SCHEMA.OBJECT

I had used SQL Synonyms in Entity Framework and LINQ-To-SQL, you can create a SQL Synonnym to point to a linked Server object, like this:
And then perform a SQL Query:
Northwnd db = new Northwnd(#"c:\northwnd.mdf");
IEnumerable<Customer> results = db.ExecuteQuery<Customer>
("SELECT contactname FROM customersSynonym WHERE city = {0}",
"London");
You can read the documentation here and also you can read another question like this one but using Entity Framework, which uses the same principle, using a SQL Synonym.

Related

SSIS - check if database and tables exist, if not - run sql to create

I am planning on importing data into Azure SQL database using SSIS package. I know I can do that with OLEDB Source and Destination but I also want to check if the database and tables exist and if not - create them. I am planning on using Execute SQL task to create database and tables, but how do I first check if they already exist?
So if database and tables exist, I will run data flow task to transfer the data, but if they do not exist - run Execute SQL task to create database and tables and then run data flow task.
How can I accomplish that?
Create an OLE DB connection manager to the server and master database on it. Apply this connection manager for the next two steps.
Create two SQL tasks in a container. The first SQL task will check to see if the database exists. You can pass the database name as a variable to it and apply it in the SQL example like that shown below. The "?" is the database name variable.
IF NOT EXISTS(SELECT * FROM sys.sysdatabases where name=?)
-- create database
Then for the second SQL task apply something like the following in which the database and table name are passed as variables. But, in difference to the previous SQL, you can apply an expression for defining the SQL.
"IF OBJECT_ID(N'" + <#DatabaseName> + "dbo." + <#tablename> + ", N'U') IS NULL
CREATE TABLE " + <#DatabaseName> + "dbo." + <#tablename> + "
(
Field1 VARCHAR(20) NOT NULL
,Field2 TINYINT NOT NULL
);"
I'm not very familiar with Azure or SSIS, but in SQL Server you can check to see if an object exists like this:
IF OBJECT_ID('dbo.UserTable', 'U') IS NULL
-- Doesn't Exist.
I hope this helps in some way.
SSDT (the database tool, not the SSDT-BI product suite) uses a declarative model to manage and deploy database schemas. You can get SSDT here.
Once you create a new SQL Server Database Project, you can import an existing database schema. In the database properties, you will define your target database as Azure. When you build the project, it creates a dacpac file as output. This file is an archive that contains the definition of your database.
SSDT comes with a utility called sqlpackage.exe, which you can learn about here. You can then pass in your dacpac file and target server and have the utility "publish" the database. If the database and schema are there are match up, it will do nothing. If it does not exist all objects will get created. If the schema is different than expected, it will update the schema according to how it is defined in the project.
SSDT has loads more benefits than this, but for your case, it will simplify the process and make it far more maintainable.
BTW, if the database does not exist, you may need to put delay validation on your connection managers and tasks in the rest of the package so it does not fail validation. Or better, create a master package that first executes the sqlpackage process task and then calls your loading package as a child package.

Joinning SQL Server table with *.mdb file

I'm trying to joinning SQLServer 2008 R2 tables with msaccess table (*.mdb).
I already tried "OPENDATASOURCE" and "Linked Server", but no one of them is work correctly.
in example, I've got the following message:
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server
"TestLinkServer" returned message "Cannot open database ''. It may
not be a database that your application recognizes, or the file may be
corrupt.".
the other error message:
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "MDBTest"
returned message "The Microsoft Jet database engine cannot open the
file '\10.55.56.34\Shared Folder\LBUS.mdb'. It is already opened
exclusively by another user, or you need permission to view its
data.".
and many more :D
can anyone give the working tutorial?
thanks in advance.. :)
The easiest way is to do the join inside ms-access.
Set up a table link in your access database that references the sql-server table you want to join.
Then build a query in access that joins that table with one or more tables in the access database.
If you want to join more than one sql-server table, first create a view in sql-server that combines all the relevant tables. Then set up your table link to reference the view.
If, for some reason, you must do the join inside SQL server, you will have to use a different technique, or use the table link feature to "push" data from the access table to a (previously defined) sql server table. Then, it's just an ordinary join.

What changes I need to do on the restored DB and how?

I have restored a DB from "mixed authentication" MS SQL Server into "windows authentication only" SQL Server.
My application is configured for windows authentication which uses 'dbo' for accessing DB.
But in the restored DB , all the tables, views are NOT owned by 'dbo'
Im not really sure if i understood the question, but i guess when you accessing database, you are/was creating the tables. Most possibly your statements was like:
Create Table TableName (...)
To create tables in specific schema, you should add schema prefix (or change the default schema of user you are using):
Create Table dbo.TableName (...)
To move tables to other schema, see Alter Schema.

Updating records in MYSQL with SSIS

I am writing an SSIS package that has a conditional split from a SQL Server source that splits records to either be updated or inserted into a MYSQL database.
The SQL Server connection has provider .NET Provider for OldDB\SQL Server Native Client 10.0.
The MYSQL connection is a MYSQL ODBC 5.1 ADO.NET connection.
I was thinking about using the OLE DB Command branching off of the conditional split to update records but I connect use this and connect to the MYSQL database.
Does anyone know how to accomplish this task?
I would write to a staging table for updates including the PK and columns to be updated and then execute an UPDATE SQL statement using that table and the table to be updated. The alternative is to use the command for every row and that just doesn't seem to perform that well in my experience - at least compared to a nice fat batch insert and a single update command.
For that matter, I guess you could do without the conditional split altogether, write everything to a staging table and then use an UPDATE and INSERT in SQL back to back.
Probably, the following MSDN blog link might help you. I haven't tried this.
How do I UPDATE and DELETE if I don’t have an OLEDB provider?
The post suggests the following three options.
Script Component
Store the data in a Recordset
Use a custom component (like Merge destination component)
The author also had posted two other articles about MySQL prior to posting the above article.
Connecting to MySQL from SSIS
Writing to a MySQL database from SSIS
Hope that points you in the right direction.

How can I export these Sql Server 2008 Tables from SqlServer A to SqlServer B?

I'm trying to export some tables from a sql database on SqlServer 1. In our intranet LAN (right next to me) is a temp Sql Server I made, called SqlServer 2.
I don't want to backup the entire db and then restore that : source DB is around 30Gig.
So I try and do an EXPORT task. But some of the tables have some GEOGRAPHY fields. So the Export Task isn't working. Is there any other way?
if you have administrator rights on SQLServer1, you could create a linked server object on SQLServer1 that points to SQLServer2. With that relationship, you can use a four-part name to reference the tables on SQLServer2 and write an INSERT statement to push the data over.
Check out Books Online for how to set up a linked server.