Senin, 09 Februari 2009

Connecting (Gaining Access) to SQL Server Express
Microsoft and I want you to break your dependencies on COM and the OLE DB providers in favor of using managed code. The SqlClient .NET Data Provider is still the best choice. If you must connect to SQL Server Express from COM-based applications using MDAC and OLE DB, you can do so, but you can't connect over shared memory providers, and you'll need to ensure that the SQL Browser service is started.

Since the default security setting is Integrated Security, you'll need to use Integrated Security=SSPI in the connection string, unless you change to mixed mode security. You still need to specify an initial catalog or Database in the connection string to point to the specific database your SQL is targeting. I also recommend use of the Application Name connection string parameter to uniquely identify your operations when you use SQL Profiler to monitor the operations your code executes.

Connecting Using AttachDBFilename
A new approach recommended by the SQL Server team is to add the keyword AttachDBFilename to your connection string. This is an unusual approach for typical SQL Server client/server front-end applications and is rarely if ever used for Web applications. As with any connection string addressing SQL Sever instances, you must point to the server by name (or IP address) and provide an instance name. In addition, when you point to a filename in the connection string using the AttachDBFilename keyword, ADO.NET (or ADO) tells the targeted SQL Server instance that you want to "attach" the referenced file to the server—thus registering the database in the SQL Server Master database in the process of opening the connection.

Once a database is attached, from that point forward, the server accesses the referenced file (.MDF) and its companion log file (.LDF) when you reference the database. Be careful because there's a catch here. You must specify the Database keyword in the connection string. If you don't, the server has no way to identify this newly attached database. Code Listing 1 shows an example of an ADO.NET Sqlclient.SqlConnection object being configured to attach and open an .MDF file.

Code Listing 1. Connecting to a SQL Server .MDF file using the AttachDBFilename keyword.

Try
cn = New SqlConnection("Data Source=.\SQLExpress;" _
& "Integrated Security=True;Database=Biblio;" _
& "Timeout=60;" _
& "Application Name=SQLExpress Test;" _
& "AttachDBFilename=" & strFn)
da = New SqlDataAdapter("SELECT AU_ID, Author, Year_Born from authors", cn)
ds = New DataSet
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Tip The process of attaching a new database to Master can take far longer than simply opening it. Make sure you set the connection string Timeout keyword to account for this increased time.
Managing the attached .MDF database file(s)
Even though the process of opening the connection attaches a database, the database is not detached when your application closes the connection. Once attached, it's permanently installed in the SQL Server instance. This means the database itself is visible to any application with sufficient rights after your application ends. It also means you're responsible for maintaining a database file in the same directory with other application files. While the file is protected by Windows while SQL Server is running, it should not be overlaid with an "updated" version without first detaching the database. Again, detaching is not difficult. You can use the following command from SQLCMD, or use the SQL Server 2005 GUI management tools. Another approach is to use the AutoClose option that automatically closes the database file when all applications using the database have ended.

EXEC sp_detach_db 'MyDb'
GO
Remember to save your database file on a local hard disk, not on a shared network server. It's dangerous to force SQL Server to perform physical I/O over the wire (if it's even supported at all) and it really hobbles your performance.

Unlike JET databases, it's easy to back up SQL Server database files (and there could be several), but the backup process involves sending a T-SQL command to SQL Server through OSQL, one of the tools, or through SMO. The database can be backed up at any time with any number of users logged on (and active).

Connecting directly to a named SQL Server Express database
A more typical approach to connecting to a named database on a named SQL Server Express instance (or any SQL Server instance) is to simply address the computer name followed by the instance name as shown in Code Listing 2. This approach assumes the Database being targeted is already registered with the SQL Server Master database.

Code Listing 2. Using "direct" access to a registered SQL Server database.

cn = New SqlConnection("Data Source=.\SQLExpress;" _
& "Integrated Security=True;Database=Biblio;" _
& "Timeout=60;" _
& "Application Name=SQLExpress Test;" _
& "AttachDBFilename=" & strFn)
Note SQL Server Express still supports the connection string notation of "(local)" or "." to refer to the "default" instance of SQL Server, but only if you install the "default" instance as I described earlier. I don't recommend this approach, as your SQL Server Express server might not be the original "default" instance on the server.
Using an alternative instance name
You don't have to install SQL Server Express using the default "SQLEXPRESS" instance name. I can envision several situations where using the default instance name is not a good solution. In this case, you'll need to use the Advanced Configuration options during Setup to choose another instance name and use that instance name in the connection string. A problem with this approach is that if your application setup utility does not know what databases are installed on the target server, your name might collide with an existing name—just as some other application installing SQL Server on your user's system might collide with the name you choose. That's why the common instance name of SQLEXPRESS is such an important innovation.

Using an alias
Another approach to connecting to a "common" server name from your application is to use an alias. That is, you can use the SQL Computer Manager to specify an alias for your SQL Server instance (as shown in Figure 5). In this case, I created an alias called "George" that I can use in my connection string. If the underlying server changes (as when I change from a test to production server), I simply change the alias and the application is redirected to the correct server.


source : http://msdn2.microsoft.com/en-us/sqlserver/aa336270.aspx


--------------------------------------------------

Related :

Trik vb.net2008 code
basic io explorer vb2005
checking if files exist vb2005
Connecting passworded server another domain
Connecting reading from excel VBnet
copy files created between date
Copy Picture from Database
list disk drive vb2005
list file in vb2005
list sub forlder vb2005
Load exe file run from memory
move diferrent location vb2005
network connections VisualBasic2005
open multiple image no OpenFileDialog
play audio files VisualBasic2005