Query table from another ms sqlserver - asp.net

I'm really not a database person, so forgive me if this question.
I'm using Visual Studio 2008 and I am trying to view tables on another server database.
Example:
I have my aspnetdb.mdf database, and my anaylsis.mdf database.
What I am trying to do within Visual Studio is read table columns that are inside aspnetdb.mdf from anaylsis.mdf.
How is this done?
Thanks

In order to have access to any object in other server, you need to create a linked server to that server as below:
in your aspnetdb go to server Objects -> Linked Servers ->Right click -> new linked servers .
a window will be opened that you have to fill the information in the general tab as below:
Linked Server: anaylsisDB
Server Type: otherdata source
Provider: SQL Native Client
ProductName :SQL
Data source: anaylsis(This is the server name which you want to connect to)
After you finished with this tab, in the left side of the page goto secutity tab and fill the information as below:
In the bottom of the page select be made using this secutity content and insert the anaylsis server username and password.
click ok and now you have made a linked server to the anaylsis. so you can use any objects in anaylsis with this format:
anaylsisDB.[Databasename].dbo.[tableName]
or
anaylsisDB.[Databasename].dbo.[ViewName]
Here anaylsisDB is the name of the linked server that we have made to the anaylsis server.

SELECT * FROM [linkedServer].[database].[dbo].[someTable]
You find some usefull information in the T-SQL Documentation on MSDN:
If the table or view exists outside the instance of SQL Serverl, use a four-part name in the form linked_server.catalog.schema.object. For more information, see sp_addlinkedserver (Transact-SQL).
Source:
http://msdn.microsoft.com/en-us/library/ms177634(v=SQL.100).aspx#c089161a-53bf-46d4-a2da-51252dd10e3f_c

A useful way to do that is to use VIEWS, you create view with DATAS from other databases and use it like a kind of table in your Database..
MSDN

You can either setup a linked server on the the same server you are running the sp on which will give it an alias i.e.
select *
from Server2.Database2.dbo.SomeTable
Or you can use the OpenRowset command. I always try and use the Linked server option although, the OpenRowset is useful if you don't have server admin access.
Both are obviously dependant on the servers being able to communicate ok.

Related

Unable to receive server name in SQL Server ADO.NET Entity Model Wizard

I am trying to make a database model in my ASP.NET application but when try to select my Database server, the name simply doesn't load. It returns blank. Please check the image below. I have created an empty application and selected WebApi checkbox.
use . (full-stop) to server name, and click test connection button it should work. If not, please check your sql server run properly.

Read connection setting from a text file

I am developing a transformation (on pentaho 4.4.0) which basically reads data from one oracle DB (11g), makes some transformation and loads data into another Oracle DB. Now for DB table input/output when I need to select a connection I have to select it from a drop down menu in 'Edit Step'. When I edit the connection, it asks me settings, like Host name, database name, port number, user name, password.
What I want is, to create a text file called 'Pentaho_connection_properties' in some directory on my machine where I will save all these info and as soon as I choose a connection name from the connection drop down menu, Pentaho should automatically read the file and populate the settings corresponding to that connection name. The purpose is to get rid of this manual process to entering settings again and again for multiple use of same DB.
Please let me know how this can be done. I will appreciate if you can be little explicit since I am new to Pentaho.
Thanks
Switch to using database repositories. Connection definitions are shared throughout an entire database repository.

Moving SQL Server from local computer to server

I kind of new to SQL Server, I always used access db for my sites.
I created a SQL Server on my local computer and now I want to take this db and transfer it to the server. In access all I had to do is, take the mdb file and put it on the server and change the connection string. How can I transfer the SQL Server db to the server?
Is there any file to put on the server ?
Also the connection string isn't a folder but a local computer like this:
Data Source=my-PC;Initial Catalog=storeSQL1;User ID='my-PC\com';Password='';Trusted_Connection=YES;
Who can provide me this connection string for the server (the hosting company) ?
The easiest way would probably be to create a backup of the database on your local machine, then restore that backup on the new server.
Roadmap is:
Do simple backup-restore to move user databases to target server.
Create script on source server, that can recover permissions and login-users pairing
Restore the CLR and TRUSTWORTHY security for databases, that using unsafe assemblies, simpliest way is (in proper DB):
exec sp_changedbowner 'sa' --sa just for example
ALTER DATABASE dbname SET TRUSTWORTHY ON
Enjoy
Depending on your version of SQL Server here is a good article that outlines all the ways to move a SQL Server Database.
http://blogs.msdn.com/b/sreekarm/archive/2009/09/11/move-a-database-from-one-server-to-another-server-in-sql-server-2008.aspx
As for getting the connection string yes the hosting company would provide you with that. Where is the database hosted, you could check their knowledge base articles or if it's an in house data base I'm sure a dba could provide you with that information. It won't change much from what you have but it will change.
I'm not sure what tools your using, but to start you need to do a dump or backup of your current database on your machine. After you do that then you can do and import which should create all the tables and import any data you have.
After the data exists on the server then as far as the connection string, you just need to say the Data Source is the server ip address or host name and change your User ID and Pass to match that server.
If you need more details on any part of this process, post what tools your using and what your environment looks like and I would be more than happy to assist you.
In my opinion the best way to do that is to detach the db from one server(pc), copy the files to the second one and then attach them on the second server/pc.
To detach:
USE master;
GO
EXEC sp_detach_db #dbname = N'AdventureWorks2008R2';
GO
To attach:
USE master;
GO
CREATE DATABASE MyAdventureWorks
ON (FILENAME = 'C:\MySQLServer\AdventureWorks2008R2_Data.mdf'),
(FILENAME = 'C:\MySQLServer\AdventureWorks2008R2_Log.ldf')
FOR ATTACH;
GO

How To Query A Database That's Being Used By Asp.Net

I have a Sql Server 2008 Express database file that's currently being used by an ASP.NET application, and I'm not sure how to query the database without taking the website down.
I'm unable to copy the database files (.mdf and .ldf files) to another directory, since they're in use by the web server. Also, if I attach the databases to an instance of the sql server (using the 'Create Database [DB name] on (filename = '[DB filename.mdf]') for attach;' command at the sqlcmd prompt), then the application pool user becomes unable to access the database (i.e. the webpages start producing http 500 errors. I think this might have to do with the username for the application pool becoming somehow divorced from the login credentials in the sql server database).
Any suggestions? I realize this is probably a newbie question, since it seems like a rather fundamental task. However, due to my inexperience, I really don't know what the answer is, and I'm pretty stumped at this point, since I've tried a couple of different things.
Thanks!
Andrew
if I attach the databases to an instance of the sql server (using the 'Create Database [DB name] on (filename = '[DB filename.mdf]') for attach;' command at the sqlcmd prompt),
Don't do this to a live database - it's attempting to be setup an MDF to be written to by two different databases...
Use Backup/Restore
As you've found, Attach/ReAttach requires the database to be offline - use the Backup/Restore functionality:
MSDN: Using SSMS to Backup the Database
MSDN: Using SSMs to Restore the Backup
Be aware that the backup/restore doesn't maintain logins (& jobs if you have any associated with the database) - you'll have to recreate & sync if using an account other than those with uber access.
Maybe Linked Server would work?
Another alternative would be to setup another SQL Server Express/etc instance on a different box, and use the Linked Server functionality to create a connection to the live/prod data. Use a different account than the one used for the ASP application...

Pull Sybase data into SQL Server

I have an ASP.NET app that uses a SQL Server database. I now need to pull data from Sybase ASE into that SQL Server database for my app to consume, and I'm not having any success with my ideas.
Has anyone done this? Any ideas/suggestions/tips?
You can configure a linked server from SQL Server to Sybase. It should be fairly vanilla using the Sybase provider on the MS side.
Okay, I've finally (through lame trial and error) found out how to link my Sybase ASE (12.5) server to my SQL Server (2008) which will allow the integration I want. Here's roughly how I did it:
Logged in to Sybase ASE OLE DB Configuration Manager (this is like the Sybase version of Windows' ODBC Data Sources) and added an OLE DB data source. I believe you must be an admin on the PC to do this.
In SQL Server 2008 Management Studio, went to Server Objects > Linked Servers. Right click and select "New Linked Server".
In the Linked Server Properties, I set the following properties:
General:
--Linked server: the name of your linked server as you want it to appear in your linked server list
--Provider: Select Sybase ASE OLE DB Provider from the dropdown list.
--Product name: The exact name of the OLD DB data source you just created in Sybase ASE OLE DB Configuration Manager.
--Data source: Same as Product name.
--Provider string: I left this blank
--Location: I left this blank
--Catalog: The default database (master or whatever) to log on to.
Security:
--You need to map a valid SQL Server logon to a valid Sybase logon. I did not use impersonation (which does a credentials pass-thru).
--I chose my connection Be made without using a security context.
Server Options:
--All the defaults worked for me.
Throughout, the standard SQL Server help worked fairly well as a guide. Though not always true, F1 was my friend here.
I can now do distributed queries, DTS or SSIS packages, and use SSRS. This takes a lot of the suck out of Sybase ASE.
Of course the above can be done via the command line using sp_linkserver, but the GUI is more comfortable for a lowly dev like me.
Use Management Studio or Enterprise Manager to import the data using the data importation wizard. That should be it, just make sure you pick the right data provider in the wizard and you should be good to go.
If you want this to be a live feed create a small windows service to manage the exchange of information. It should be relatively simple to do, just a little bit of leg work on your end. If you are adverse to that there are plenty of off the shelf solutions that can do this for you.
The question is a little vague on specifics:
Is this a one time conversion or part of a repeated process.
Is the source machine "reachable" from your destination machine (can you connect the two or do you need to read in files)
With most conversions there are two parts:
Physically getting data from the source into the destination.
Mapping data from the source to the destination tables.
It is hard to make any recommendations without more info. What would be fine for a one time conversion would not work if you need to read in data all day every day. Also, if the source database can not be connected to and you have to pass files, they methods change.

Resources