الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

عمل attatch للداتابيس

مغلق
بدأه amr15 في 16 أبريل 2006 · 5 رد · 725 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم :

لو سمحتم انا كتبت الكود التالي حتى اعمل attach :

Try

Dim Path As String = Application.StartupPath & "\registration_Data.MDF"

File.Copy(Path, "c:\registration_Data.MDF", True)

Dim con As New SqlConnection("Server=localhost;" & "DataBase=master;" & "Integrated Security=SSPI")

If con.State = ConnectionState.Closed Then

con.Open()

End If

File.SetAttributes("c:\registration_Data.MDF", FileAttributes.Normal)

Dim cmd As New SqlCommand("sp_attach_db registration , @filename1 ='registration_Data.MDF'", con)

cmd.ExecuteNonQuery()

If con.State = ConnectionState.Open Then

con.Close()

End If

Catch eUpdate As System.Exception

MessageBox.Show(eUpdate.Message)

End Try

بيحمل الملف mdf على ال c بس بعدين بيعطي الخطأالتالي :

device activation error.the physical file name 'registration_data.mdf'

maybe incorrect

ليش

#2

وينكم يا اخواني المبرمجين ارجو المساعدة بسرعه

#3

اتمنى ان ينفع هذا الحل الذي جلبته من احد المواقع

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
    <System.Diagnostics.DebuggerStepThrough()> 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

اللهم صل و سلم على سيدنا محمد

كاف و نون خلقهم و فنائهم كاف و نون

عالمي شاشة و لغتي صفر و واحد

Just Smile ............You can do it

When you stop learning ... You stop leading

لساني بنطقي صامت عنه عادل............وقلبي بصمتي ضاحك منه هازل

وأتـعـب من نـاداك من لا تـجيبـه............وأغـيظ من عـاداك من لا تـشاكـل

مدونتي الشخصية : Tammam Koujan

آخر مواضيع المدونة :

#4

كل الكود علشان يعمل attach لل database!!

الكود ده يا باشا شغال عندي :

sqlConnection1.Open 
Try 
 Dim Command As String = " EXEC sp_attach_db @dbname = N'Dentistry'," + "@filename1 = N'" + Application.StartupPath + "\Dentistry_Data.MDF'," + "@filename2 = N'" + Application.StartupPath + "\Dentistry_Log.LDF' " 
 Dim da_attach As SqlDataAdapter = New SqlDataAdapter(Command, sqlConnection1) 
 Dim ds_attach As DataSet = New DataSet 
 da_attach.Fill(ds_attach) 
Catch 
End Try 
sqlConnection1.Close

ارجو الدعاء

Enjoy

#5

أنا آسف يا باشا مقرأتش كود الأخ Tammam Koujan ... أنا شفت حاجة كبيرة كده لكن طلع هو حاطط كل كود ال form

الكود اللي أنا حطيته بيعمل attach لل database لو حطيتها جنب ال exe ... فلو عايز تحط ال database في مكان تاني غير ال path في ال command

#6

حصل خير

لكن أتمنى من جميع اعضاء المنتديات الا

يتعجلوا بالرد قبل ان يتبينوا ما قدمه الطرف الاخر و يفهموا فكرته

حيث ان اكثر المشاحنات التي تحصل في الموقع تقع من هكذا امور

على كل مشاركتك اخي C#please جميلة و ما في زعل يا باشا :lol:

اللهم صل و سلم على سيدنا محمد

كاف و نون خلقهم و فنائهم كاف و نون

عالمي شاشة و لغتي صفر و واحد

Just Smile ............You can do it

When you stop learning ... You stop leading

لساني بنطقي صامت عنه عادل............وقلبي بصمتي ضاحك منه هازل

وأتـعـب من نـاداك من لا تـجيبـه............وأغـيظ من عـاداك من لا تـشاكـل

مدونتي الشخصية : Tammam Koujan

آخر مواضيع المدونة :

هذا الموضوع مغلق.

مواضيع مشابهة