ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - php3 - 28-08-14
السلام عليكم
اخواني قبل كل شي بحثت في المنتدى عن هذه الطريقه ولم اجدها
وبعض المواضيع متشابهه وبعضها تتعلق بقاعدة بيانات mysql و mysql server
انا رفعت قاعدة بيانات على موقع مجاني من نوع mysql
واستخدمت عدة اكواد على الفيجوال بيسك دوت نت بدون فائدة
وهذا هو الكود الذي استخدمه في الفيجوال بيسك
PHP كود :
Imports System.Data Imports MySql.Data.MySqlClient Public Class Manage_user Dim con As New MySqlConnection Dim result As Integer 'MySqlCommand It represents a SQL statement to execute against a MySQL Database Dim cmd As New MySqlCommand 'Represents a set of data commands and a database connection that 'are used to fill a dataset and update a MySQL database. This class cannot be inherited. Dim da As New MySqlDataAdapter
Private Sub btncreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncreate.Click 'this line of is simply we copy this one from our form con.ConnectionString = ("server=localhost;user id=root;password=;database=androidhive")
Try 'we open Connection con.Open()
With cmd .Connection = con .CommandText = "INSERT INTO `androidhive`.`products` (`pid`, `name`, `price`, `description`) " & _ "VALUES (NULL, '" & txtfullname.Text & "', '" & txtusername.Text & "', '" & txtpassword.Text & "');" 'in this line it Executes a transact-SQL statements against the connection and returns the number of rows affected result = cmd.ExecuteNonQuery 'if the result is equal to zero it means that no rows is inserted or somethings wrong during the execution If result = 0 Then MsgBox("Data has been Inserted!") Else MsgBox("Successfully saved!")
End If End With Catch ex As Exception MsgBox(ex.Message) End Try con.Close() End Sub
Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click 'again we do this because we do not yet declare this as global, but we do it for now beacause in later tutorial 'we will do the moduling and more on refactoring of codes con.ConnectionString = ("server=localhost;user id=root;password=;database=androidhive") Try con.Open() With cmd .Connection = con 'in this query it does simply selecting or getting all the user found in the database. .CommandText = "Select * from products" End With
Catch ex As Exception MsgBox(ex.Message)
End Try con.Close() da.Dispose() filltable(dtguser)
End Sub ' this our sub procedure that well always used 'this beacause it always catch all the result query 'and it will display to any kind of visual basic containers like datagridview,listbox,listview, etc... Public Sub filltable(ByVal dtgrd As Object) 'declare a variable as new datatable Dim publictable As New DataTable Try 'Gets or sets an SQL statement or stored procedure used to select records in the database. da.SelectCommand = cmd da.Fill(publictable) dtgrd.DataSource = publictable dtgrd.Columns(0).Visible = False
da.Dispose()
Catch ex As Exception MsgBox(ex.Message)
End Try
End Sub
Private Sub Manage_user_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub End Class
لم اجد الطريقة في بعض مواقع الانترنت واتمنى ان اجدها هنا
علما بأن قاعدة البيانات مستخدمه في موقع مجاني اسمه
hostinger.ae
شكرا لكم
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - الشاكي لله - 28-08-14
انتظرني قريبا سأعمل فيديو عن هذا الموضوع
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - php3 - 28-08-14
بانتظارك اخي الكريم
وشكرا على التواصل
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - administrator - 28-08-14
هل الموق يدعم اتصال البيانات عن بُعد ؟
تأكد من ذلك
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - vbnet - 28-08-14
.....
بعد الاطلاع على هذا الموضوع وموضوع آخر لك (ماهي المشكلة في مشروعي . ربط قاعدة بيانات mysql)
منقول مع تعديل بسيط ليناسب طلبك
كود :
Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As New MySqlConnection(" Server = db4free.net; Port = 3306; Database = vbnet; " _
& " User id = vbnet; Password = 10241024; ")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' إعدادات خصائص بعض الأدوات يمكن الإستغناء عن هذه الأسطر بتعيينها من خلال نافذة الخصائص
Me.btnInsert.Text = "إضافة"
Me.btnUpdate.Text = "تحديث"
Me.btnDelete.Text = "حذف"
Me.btnInsert.Enabled = False
Me.btnUpdate.Enabled = False
Me.btnDelete.Enabled = False
Me.txtPID.Enabled = False
'---
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.ReadOnly = True
Me.DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.MultiSelect = False
'------------------------
' إستعجال ظهور النافذة
Me.Show()
Application.DoEvents()
'------------------------
' إستدعاء تعبئة البيانات
If DataGridView1Fill() Then
Me.btnInsert.Enabled = True
End If
End Sub
' إضافة
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
If Me.btnInsert.Text = "جديد" Then
' إستدعاء الوضع الإفتراضي للأدوات
Reset()
Exit Sub
End If
If (Me.txtName.Text.Trim = String.Empty) Or (Not IsNumeric(Me.txtPrice.Text)) Then
MsgBox("تأكد من البيانات")
Exit Sub
End If
Try
Dim sql As String = " INSERT INTO `products` ( `name`, `price`, `description`, `expiredate` ) " & _
" Values ( @name, @price, @description, @expiredate ) "
Using cm As New MySqlCommand(sql, con)
cm.Parameters.AddWithValue("@name", Me.txtName.Text.Trim)
cm.Parameters.AddWithValue("@price", Me.txtPrice.Text.Trim)
cm.Parameters.AddWithValue("@description", Me.txtDescription.Text.Trim)
'cm.Parameters.AddWithValue("@expiredate", Me.DateTimePicker1.Value.Date) '' إذا كان الحقل تاريخ فقط بدون وقت
cm.Parameters.AddWithValue("@expiredate", Me.DateTimePicker1.Value) '' هنا التاريخ مع الوقت
con.Open()
cm.ExecuteNonQuery()
con.Close()
End Using
' إستدعاء الوضع الإفتراضي للأدوات
Reset()
MsgBox("تمت الإضافة بنجاح")
DataGridView1Fill()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
' تحديث
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
If MsgBox("هل أنت متأكد من البيانات", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading + MsgBoxStyle.YesNo + MsgBoxStyle.Question) <> MsgBoxResult.Yes Then
Exit Sub
End If
If (Val(Me.txtPrice.Text) < 0) _
Or (Me.txtName.Text.Trim = String.Empty) _
Or (Not IsNumeric(Me.txtPrice.Text)) _
Then
MsgBox("تأكد من البيانات")
Exit Sub
End If
Try
Dim sql As String = " UPDATE `products` " & _
" SET `name` = @name " & _
" , `price` = @price " & _
" , `description` = @description " & _
" , `expiredate` = @expiredate " & _
" WHERE `pid` = @pid "
Using cm As New MySqlCommand(sql, con)
cm.Parameters.AddWithValue("@name", Me.txtName.Text.Trim)
cm.Parameters.AddWithValue("@price", Me.txtPrice.Text.Trim)
cm.Parameters.AddWithValue("@description", Me.txtDescription.Text.Trim)
cm.Parameters.AddWithValue("@expiredate", Me.DateTimePicker1.Value)
cm.Parameters.AddWithValue("@pid", Val(Me.txtPID.Text))
con.Open()
cm.ExecuteNonQuery()
con.Close()
End Using
' إستدعاء الوضع الإفتراضي للأدوات
Reset()
MsgBox("تم التحديث بنجاح")
DataGridView1Fill()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
' حذف
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MsgBox("هل أنت متأكد من حذف البيانات", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Exclamation) <> MsgBoxResult.Yes Then
If MsgBox("هل أنت فعلاً متأكد من حذف البيانات", MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.Exclamation) <> MsgBoxResult.Yes Then
Exit Sub
End If
End If
Try
Dim sql As String = " Delete From `products` " & _
" WHERE `pid` = @pid "
Using cm As New MySqlCommand(sql, con)
cm.Parameters.AddWithValue("@pid", Val(Me.txtPID.Text))
con.Open()
cm.ExecuteNonQuery()
con.Close()
End Using
' إستدعاء الوضع الإفتراضي للأدوات
Reset()
MsgBox("تم الحذف بنجاح")
DataGridView1Fill()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
' تعبئة أداة الجدول بالبيانات
Public Function DataGridView1Fill() As Boolean
Try
Using da As New MySqlDataAdapter("Select * from `products`", con)
Using dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
If DataGridView1.Rows.Count > 0 Then
DataGridView1.Columns(0).HeaderText = "ID"
DataGridView1.Columns(0).Width = 50
DataGridView1.Columns(1).HeaderText = "الإسم"
DataGridView1.Columns(1).Width = 150
DataGridView1.Columns(2).HeaderText = "السعر"
DataGridView1.Columns(2).Width = 70
DataGridView1.Columns(3).HeaderText = "الوصف"
DataGridView1.Columns(3).Width = 200
DataGridView1.Columns(4).HeaderText = "تاريخ الصلاحية"
DataGridView1.Columns(4).Width = 110
End If
End Using
End Using
Return True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
' الاختيار بالنقر المزدوج
' عند النقر المزدوج رأس السطر
Private Sub DataGridView1_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseDoubleClick
RowSelect(e.RowIndex)
End Sub
' عند النقر المزدوج على أي خلية في سطر
Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
RowSelect(e.RowIndex)
End Sub
' قراءة بيانات السطر الذي تم اختياره بالنقر المزدوج
Private Sub RowSelect(ByVal rowIndex As Integer)
Me.txtPID.Text = DataGridView1.Item(0, rowIndex).Value
Me.txtName.Text = DataGridView1.Item(1, rowIndex).Value
Me.txtPrice.Text = DataGridView1.Item(2, rowIndex).Value
Me.txtDescription.Text = DataGridView1.Item(3, rowIndex).Value
Me.DateTimePicker1.Text = DataGridView1.Item(4, rowIndex).Value
Me.btnInsert.Text = "جديد"
Me.btnUpdate.Enabled = True
Me.btnDelete.Enabled = True
Me.DataGridView1.Enabled = False
End Sub
' إعادة الأدوات للوضع الإفتراضي
Private Sub Reset()
Me.txtPID.Text = ""
Me.txtName.Text = ""
Me.txtPrice.Text = ""
Me.txtDescription.Text = ""
Me.DateTimePicker1.Text = Now
Me.btnInsert.Text = "إضافة"
Me.btnUpdate.Enabled = False
Me.btnDelete.Enabled = False
Me.DataGridView1.Enabled = True
End Sub
End Class
.....
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - php3 - 28-08-14
اشكرك اخي الكريم vbnet
سأقوم بتجربته حالا
واذا كان لديك المشروع يمكنك اضافته هنا لربما يستفيد منه غيري
شكرا مرة ثانية
RE: ماهي طريقة الاتصال بقاعدة البيانات الموجودة في موقع الانترنت - php3 - 28-08-14
اخي الكريم vbnet
للاسف الكود لا يعمل
واتمنى ان ارسل لك تفاصيل موقعي حتى تتمكن من رؤية المشكلة
شكرا لك
|