PDA

View Full Version : سوال: اتچ کردن بانک SQL و دیاتچ کردن آن



omid-vbAuto
پنج شنبه 19 اسفند 1389, 04:30 صبح
با سلام خدمت دوستان محترم

دوستان روش اتچ کردن با کد یک بانک SQL رو می دونیم.حالا من نمی دونم چطوری بانک اتچ شده رو با کد دیاتچ کنم.و اگر تونستیم دیاتچ کنیم از کجا بدانیم یا تشخیص بدیم که بانک دیاتچ شده؟؟؟

لطفا نمونه کد بدهید

hesam_hma
پنج شنبه 19 اسفند 1389, 09:06 صبح
این dettach

Private Sub btnDetach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetach.Click
Me.Cursor = Cursors.WaitCursor
Dim cmd As New SqlClient.SqlCommand
Dim txt As String
con = New SqlConnection("data source=localhost;initial catalog=master;Persist Security Info=True;uid=sa;pwd=;")
con.Open()
txt = "EXEC sp_detach_db '" + txtDBName.Text + "', 'true'"
cmd.CommandText = txt
Try
cmd.Connection = Con
cmd.ExecuteNonQuery()
Con.Close()
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر با موفقیت حذف شد", "حذف کردن بانک", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Me.Cursor = Cursors.Default
Catch ex As Exception
Me.Cursor = Cursors.Default
con.Close()
If ex.Message.ToString = "The database 'taxi_parsian' does not exist. Use sp_helpdb to show available databases." Then
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر در حال حاضر وجود ندارد", "خطا", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Else
msg.ShowMessage(1, ex.Message, "خطا", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End If
End Try
End Sub


اینم تست وجود دیتا بیس :

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Try
Con = New SqlConnection("data source=localhost;initial catalog=Taxi_Parsian;Persist Security Info=True;uid=sa;pwd=;")
Con.Open()
con.Close()
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر موجود می باشد", "تست بانک", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Catch ex As Exception
Me.Cursor = Cursors.Default
con.Close()
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر موجود نمی باشد", "تست بانک", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Try
End Sub

parselearn
پنج شنبه 19 اسفند 1389, 09:43 صبح
http://www.mssqlcity.com/Articles/Adm/attach_database.htm

omid-vbAuto
پنج شنبه 19 اسفند 1389, 15:28 عصر
این dettach

Private Sub btnDetach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetach.Click
Me.Cursor = Cursors.WaitCursor
Dim cmd As New SqlClient.SqlCommand
Dim txt As String
con = New SqlConnection("data source=localhost;initial catalog=master;Persist Security Info=True;uid=sa;pwd=;")
con.Open()
txt = "EXEC sp_detach_db '" + txtDBName.Text + "', 'true'"
cmd.CommandText = txt
Try
cmd.Connection = Con
cmd.ExecuteNonQuery()
Con.Close()
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر با موفقیت حذف شد", "حذف کردن بانک", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Me.Cursor = Cursors.Default
Catch ex As Exception
Me.Cursor = Cursors.Default
con.Close()
If ex.Message.ToString = "The database 'taxi_parsian' does not exist. Use sp_helpdb to show available databases." Then
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر در حال حاضر وجود ندارد", "خطا", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Else
msg.ShowMessage(1, ex.Message, "خطا", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End If
End Try
End Sub


اینم تست وجود دیتا بیس :

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Try
Con = New SqlConnection("data source=localhost;initial catalog=Taxi_Parsian;Persist Security Info=True;uid=sa;pwd=;")
Con.Open()
con.Close()
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر موجود می باشد", "تست بانک", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
Catch ex As Exception
Me.Cursor = Cursors.Default
con.Close()
msg.ShowMessage(1, "بانک اطلاعاتی مورد نظر موجود نمی باشد", "تست بانک", "تایید", "", "", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
End Try
End Sub




مهندس برنامتون رو نتونستم اجرا کنم کلی ایراد میگره!!!

ممکنه نمونه برنامه بدین که:اتچ کنه - دیاتچ کنه - تست کنه.

omid-vbAuto
پنج شنبه 19 اسفند 1389, 15:29 عصر
http://www.mssqlcity.com/Articles/Adm/attach_database.htm


این رو دیده بدم ولی نتونستم استفاده کنم.