تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.NET] استفسار بخصوص التاكد من وجود التيبل الاضافي
#1
السلام عليكم  ورحمة الله

عندي قاعدة بيانات رئيسية 

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

كمثال

القاعدة الرئيسية هيه
كود :
Data Source= {0}\DBT\DB.s3db", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))


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

بعد البحث حصلت هذا الكود
كود :
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

' in this example the table name comes from a textbox

Dim tableName As String = txtTable.Text

' define new db connection

Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = D:\suranda\Visual Studio Projects\testSolution\testDB.mdb")

' get database schema

con.Open()

Dim dbSchema As DataTable = con.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, tableName, "TABLE"})

con.Close()

' if the table exists the count will be 1...

If dbSchema.Rows.Count > 0 Then

' ... do whatever you want to do if the table exists

Else

' ... do whatever you want to do if the table does not exist

' eg create a table

Dim cmd As New OleDb.OleDbCommand("CREATE TABLE [" + tableName + "] ([Field1] TEXT(10), [Field2] TEXT(10))", con)

con.Open()

cmd.ExecuteNonQuery()

con.Close()

End If

End Sub


وحاولت اغير لاني هذا الكود بنظام   OleDb  وانا اعمل على  SQLite

وحاولت اغيرة بس ما نفع معاي 

بارك الله فيكم وشكرا 
الرد }}}
تم الشكر بواسطة:
#2
PHP كود :
Private Sub btnCreate_Click(ByVal sender As ObjectByVal e As EventArgsHandles btnCreate.Click
    If IsExistTable
(txtTable.Text.TrimThen
        MsgBox
("Exists"MsgBoxStyle.Exclamation)
 
   Else
        If CreateTable
(txtTable.Text.TrimThen
            MsgBox
("Created"MsgBoxStyle.Information)
 
       Else
            MsgBox
("Fails"MsgBoxStyle.Exclamation)
 
       End If
 
   End If
End Sub

Function IsExistTable(ByVal tblName As String) As Boolean
    Dim sql 
As String _
        
"SELECT COUNT(*)            " _
        
"FROM   [sqlite_master]     " _
        
"WHERE  [type]='table'      " _
        
"       AND [name]=@tName   "
 
   Dim cmd As New SQLiteCommand(Sqlcon)
 
   cmd.Parameters.AddWithValue("@tName"tblName)
 
   If con.State <> ConnectionState.Open Then con.Open()
 
   Dim ret As Integer cmd.ExecuteScalar
    con
.Close()
 
   Return If(ret 0TrueFalse)
End Function

Function 
CreateTable(ByVal tblName As String)
 
   Try
        Dim sql 
As String _
            
"CREATE TABLE   [" tblName "] (     " _
            
"               [Field1] TEXT(10),      " _
            
"               [Field2] TEXT(10)       " _
            
"           )                           "
 
       Dim cmd As New SQLiteCommand(sqlcon)
 
       If con.State <> ConnectionState.Open Then con.Open()
 
       Dim ret As Integer cmd.ExecuteNonQuery
        con
.Close()
 
       Return True
    Catch ex 
As Exception
        Return False
    End 
Try
End Function 
الرد }}}
تم الشكر بواسطة: dubai.eig , سعود الشامان , سعود الشامان
#3
(17-10-17, 09:52 AM)a.ahmed كتب :
PHP كود :
Private Sub btnCreate_Click(ByVal sender As ObjectByVal e As EventArgsHandles btnCreate.Click
    If IsExistTable
(txtTable.Text.TrimThen
        MsgBox
("Exists"MsgBoxStyle.Exclamation)
 
   Else
        If CreateTable
(txtTable.Text.TrimThen
            MsgBox
("Created"MsgBoxStyle.Information)
 
       Else
            MsgBox
("Fails"MsgBoxStyle.Exclamation)
 
       End If
 
   End If
End Sub

Function IsExistTable(ByVal tblName As String) As Boolean
    Dim sql 
As String _
        
"SELECT COUNT(*)            " _
        
"FROM   [sqlite_master]     " _
        
"WHERE  [type]='table'      " _
        
"       AND [name]=@tName   "
 
   Dim cmd As New SQLiteCommand(Sqlcon)
 
   cmd.Parameters.AddWithValue("@tName"tblName)
 
   If con.State <> ConnectionState.Open Then con.Open()
 
   Dim ret As Integer cmd.ExecuteScalar
    con
.Close()
 
   Return If(ret 0TrueFalse)
End Function

Function 
CreateTable(ByVal tblName As String)
 
   Try
        Dim sql 
As String _
            
"CREATE TABLE   [" tblName "] (     " _
            
"               [Field1] TEXT(10),      " _
            
"               [Field2] TEXT(10)       " _
            
"           )                           "
 
       Dim cmd As New SQLiteCommand(sqlcon)
 
       If con.State <> ConnectionState.Open Then con.Open()
 
       Dim ret As Integer cmd.ExecuteNonQuery
        con
.Close()
 
       Return True
    Catch ex 
As Exception
        Return False
    End 
Try
End Function 


تم احسنت يا بطل من جديد تبهرنا بمساعدتك للشباب

شكرا وفي ميزان حسناتك

Rolleyes
الرد }}}
تم الشكر بواسطة: سعود الشامان



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم