Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Dynamic Attach MSSQL2000 database through VB.NET
Author:
vive kulshreshtha
E-mail:
Click to e-mail author
Submitted:
6/5/2005
Version:
VB.NET 2003
Compatibility:
VB.NET 2003, VB.NET 2005
Category:
Databases
Views:
21374
Dynamically Attach MSSQL2000 database through VB.net using the sp_attach_database.
Declarations:
'This code is helps to attach database through .mdf and .ldf file. 'Just copy and paste into the code window its working absolutely fine.
Code:
Imports System Imports System.Data Imports System.Data.SqlClient 'This Snippet i wrote to solve the various issues for attaching the full database databse through VB.net 'Add Anydatabse_data.mdf and anydatabase_log.ldf file in to the c:\database\ 'Add a instance of button and paste the below code. Public Class Form1 Inherits System.Windows.Forms.Form Dim constatus As Boolean = False Dim con As SqlConnection #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button
Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(192, 72) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(56, 64) Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ConOpen() Dim cmd As New SqlCommand() Dim txt As String txt = "sp_attach_db @dbname = N'Test', " & vbCrLf & _ "@filename1 = N'C:\Database\test_data.MDF', " & vbCrLf & _ "@filename2 = N'C:\Database\test_log.LDF'" cmd.CommandText = txt cmd.Connection = con cmd.ExecuteNonQuery() MsgBox("Database created successfully") End Sub Public Sub ConOpen() Try ConStatus = True con = New SqlConnection("workstation id=" & "(Local)" & ";packet size=4096;user id=sa;data source=" & "(Local)" & ";persist security info=True;initial catalog=master;password=") Con.Open() Catch ex As Exception ConStatus = False End Try End Sub End Class
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2023 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement