2-Tier SQL Server database example in VB.net - vb.net

I'm currently working on a project that requires connection to a SQL Server 2008 database over a LAN network. I intend using one computer as a server where the database will be on, while the others will connect to the server. I've done a thorough search but couldn't find any material on this. All the examples and materials I've seen seem to skip the topic of database connection over a LAN. Does anyone know how I can do this? I would like to see a simple code showing how this is done (in VB.net since that's the language that I'm using).

Connecting to SQL Server over the network is no different to connecting to SQL Server on your local machine. Simply...
Using Conn As New SqlConnection(connstring)
//Do something here
End Using
For information about your connection string have a look at connectionstrings.com
There are a few gotchas though that have more to do with networking and firewalls than any code you will have to write so before you start make sure you can connect to your server with SQL Server Management Studio or similar. Make sure you SQL Server is configured to allow incoming connections.
Depending on the nature of your application and the number of users this may not be the best archictecture. Make sure you are using windows authentication as you really don't want to be storing SQL Connection Strings with username and passwords in config files on the client machines.

Related

What is the difference between Microsoft SQL Server Connection and Generic DB connection provided in DB connector from Mule 4.3?

In the current project we are already using generic db connection and now we found that there is Sql Serve connection available but we have never used that so want to explore that option.
Is it going to give better performance than generic connection?
One observation from my experience is Generic Connection is working for Windows Authentication where as SqlServer Connection is not working for the same and working for Sql Account.
The Microsoft SQL Server Connection provides some database specific configurations to simplify the configuration of the connection. For example an instance name. With the Generic Connection you can only use generic JDBC configurations. There should not be a difference in performance. Depending on how did you configure Windows Authentication in the generic connection it may or not be possible to replicate it in the Microsoft SQL Server Connection. If you are happy with your current connection configuration there is no need to change it.

Creating a Database Server for home use in SQL Server 2005 Express

I am looking to create a database server that can be used to store data using a VB.net application. When I initially installed SQL Server 2005 Express edition, I believe I installed this using the LocalDB option.
As it stands my application is a simple tool which utilises the Process.Start command under certain situations, depending on user input, to launch Game Servers for use at a gaming LAN I assist in running.
I wish to progress the application by being able to update, delete and query a database created in SQL Server 2005 Express. I understand the use of connection strings etc, however I am wondering if the installation using LocalDB will mean that I cannot connect to the server to process the data I require.
Can I continue to use the LocalDB option and create a Database Server for specific use using my application or is a different installation option required?
Secondly, can someone point me in the right direction of how to create a new Server for this purpose? All of my searches so far have provided results for creating a database only, and not the server.
I have a couple questions about what you are trying to do.
First, why are you installing SQL Server 2005? It was released almost a decade ago.
I would go with 2012 express edition.
http://www.microsoft.com/en-us/download/details.aspx?id=29062
Second, install the management tools. SSMS is a nice GUI to do work in.
Third, If you are spinning up anything other than express, there are licensing costs which are quite high. 9K for standard and 25K for enterprise per socket or such.
Here are the versions and features list from microsoft.
http://technet.microsoft.com/en-us/library/ms144275.aspx
Last but not least, having the express edition on another computer is fine. However, you will have to use a network protocol such as TCP/IP instead of shared memory.
It will take longer to send Tabular Data Stream (TDS) to the other computer versus talking to memory on the same computer.
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c4f06669-41fd-42e6-b4a9-564cf04ca9f7/how-to-choose-between-shared-memory-named-pipes-tcpip-via?forum=sqlgetstarted
Good luck with your project.
You can use a local instance of SQL Server just fine, if you want to set up a separate server for some reason then you'll need either a 2nd machine on which to install SQL Server, or to spin up a virtual machine. If running Windows 8 (and barring hardware limitations) you have Hyper-V at your fingertips.
This is not a complete answer for you but may give you some things to think about.
First you might want to think about your choice of database. If you don't have a compelling reason for using 2005 you will probably at least want to move to SQL Express 2008.
Second, to answer the part about creating a server... A server is something that serves data. In a general sense a computer configured to be contacted by external machines and respond with data is a server. A web server runs a program like IIS or Apache to respond with web pages. A database server contains a database and allows connections to that database. So as long as you install the database on a computer and configure it to allow external connections, you have your database server.
In MS SQL there are several things that need to be set up to allow a database to accept external connections. One is that the SQL Server Browser service will need to be active. Another is that the database itself will need to be configured to allow external connections (SQL Authentication type probably).
Hope this helps.
Addition:
SQL Remote Connection Configuration
Disclaimer, I don't have SQL Express 2005 installed but I think the settings are found in the same place in 2008. If the info here is not exact to SQL Express 2005, the general terminology used here should be enough to get you headed in the right direction to find the specifics.
While viewing the database in Server Management Studio or Enterprise Manager, right click the database server instance name (the root of the tree) and select properties. There should be a section title Connections and within this section there should be an option "Allow remote connections to this server". Make sure it is checked. The other setting you need in this properties menu is under the security area. There is a radio button for "Windows Authentication Mode" and "SQL Server and Windows Authentication Mode", you want the second that allows both.
The next step you have may be to create a new user, add a password, and connect the user to the database. That will give you the credentials you will use in your database connection string while programming. I usually add new users through the security section of the database then set the User Roles for each database that I need to connect to. It is good practice to limit the permissions to those needed by the application. Typically this is read and write, but sometimes you can get away with just read. The less the better.

Check if external database is online

I know that this sentence tells me that 'MyDatabase' database is online:
select databasepropertyex('MyDatabase', 'Status')
What if I need to check a database from another server? Suppose that I already have a link server, the call should be like this:
select databasepropertyex('[192.168.111.33].MyOtherDatabase', 'Status')
but it doesn't work, anyone knows how to check this?
Thank you.
Might have to run as a pass through query, e.g.
OPENQUERY ( [192.168.111.33] ,'select databasepropertyex(MuOtherDatabase, ''Status'')' )
(assuming your already set up linked server is called [192.168.111.33], as implied above - seems like a dubious naming convention to use IPs?)
Normally, you would just try to connect to the database, and trap the error if the connection fails. But depending on your network setup and security, it might not be possible to connect directly to the database server. Exactly how you do that depends on what language and provider you use.
Some online examples of connection strings>
To see whether a SQL Server 2008 database can accept connections, look at the Collation property instead. I know how strange that sounds, but that's the documented way.
The ONLINE status may be returned while the database is being opened
and is not yet recovered. To identify when a database can accept
connections, query the Collation property of DATABASEPROPERTYEX.
For example, many database servers are set up to deny connections from all IP addresses except one. This is especially common in web-based setups; it's a fundamental security measure. In that case connecting like this (immediately below) would work. Code on the application server or web server tries to connect to the database server, and returns an error if it fails.
Your server --> Application server --> Database server
or workstation or web server
But connecting like this won't, because the database server is configured to accept connections only from the application server or web server.
Your server ----------------------------> Database server
or workstation
Application server
or web server

Can SQL Server Express LocalDB be connected to remotely?

I am looking into using the new SQL Server Express LocalDB (I think it is code named "Denali") for a desktop application.
It is currently running with SQL Compact, but the user is wanting to share the database between multiple PCs on a network. Unfortunately this is not something that SQL Compact can do, so I am investigating other solutions.
The client requires the ability to send database files easily to other sites or to back them up to a flash disk, so I am avoiding going to SQL Express because there is quite a bit of "administrator" knowledge required to backup and restore.
So, my questions is, does the new SQL Express LocalDB support remote connections to the database over a network and/or through a shared network folder with the mdf file in it?
LocalDB does support supplying a path for an attached local DB in it's connect string (AttachDbFileName) hence the shared network folder option.
NOTE: This question pertains to "LocalDB" the new version of SQL Express 'Denali' and not to SQL Server Express 2008 or prior.
See article here announcing LocalDB's release: http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
No, SQL Server Express LocalDB doesn't accept remote connections.
The idea with shared network folder might work, but only if you are able to make sure the LocalDB instance is shutdown before you try to copy the file. Also keep in mind that only one LocalDB instance can have any given database file open at the same time. and don't forget about the log files!
Additional security warning: unlike SQL Server Compact databases, SQL Server Express databases (including LocalDB ones) are not designed as secure data exchange format. For instance, they can contain malicious code in .NET assemblies embedded in them. So you should never open databases from untrusted source.
Maybe providing the customer with a simple tool that automates the backup process would be a better idea?
This isn't a fresh thread, but I would like to share my experience with SQL Server Express database LocalDB.
I have a WPF C# project using SQL database with LocalDb Engine. It is working fine no problem, I can use the database with the WPF app. I wanted this app to work on network with more PCs.
On the network another PC can use the database from my PC using UNC path in the connection string.
It seemed to me the remote connection is working. However when the remote PC is connected, I am not able to use the database with my local WPF app. If I run my app first the remote PC could not connect. So this tells me that the remote connection is working, but the multiple connection is not allowed.
OK, I didn't give up and I run the app from my PC twice and I saw it is working which tells me that the same SQL LocalDB engine can handle multiple connections locally only.
I hope this experience will help someone. Thanks.
In short, yes it can. Here is a tutorial on how to configure it.
Also, here is another post with a potential issue that might occur.
Both explain how to configure SQL Server Express to accept Remote Connections.

How to connect to a remote SQL Server using servername, username & password

How to connect to a remote SQL Server using servername, username & password in SQL Server 2005 ?
I'm going to assume that you're talking exactly about what your question asks, how to connect to a remote MSSQL instance inside SQL Server, rather than through SQL Server Management Studio because 1) you don't mention SSMS and 2) it's pretty obvious how to connect with SSMS (I mean, the connect box is right there when you start it).
To connect to one MSSQL instance from another you can use linked servers. You can query data from linked servers, and if they are configured for RPC Out you can also execute SQL against them.
If the server is configured for integrated security, there is no way of doing it without changing the security option.
Check out the ConnectionStrings website - it shows you how to build a connection string that will allow you to connect from your machine to a remote SQL Server instance.
That connection string will typically look something like:
server=YourServerHere;database=YourDatabase;User ID=YourUser;Password=Top$ecret
But there are lots of options and additional things you can specify - the ConnectionStrings.com web site shows and explains them all!