منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
[كود] [ليس جديد] صياغة Function لتستخدم متغيرات لاسماء الجداول او الحقول - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb)
+-- قسم : قسم لغة الفيجوال بيسك VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=182)
+--- قسم : قسم مكتبة كتب VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=186)
+--- الموضوع : [كود] [ليس جديد] صياغة Function لتستخدم متغيرات لاسماء الجداول او الحقول (/showthread.php?tid=45958)



[ليس جديد] صياغة Function لتستخدم متغيرات لاسماء الجداول او الحقول - سعود - 25-04-23

بسم الله الرحمن الرحيم
السلام عليكم ورحمة الله و بركاته
حبيت اكتب الطريقة ادري انها ليست جديدة لكن لعل احد يجد بها حل.

اول Function تزودها بوسائط لتعيد لك قيمة
PHP كود :
Public Function getPageTitle(table As Stringcolid As Stringcoltitle As Stringfid As Integer) As String
        Using cm 
As New SqlClient.SqlCommand(""con)
 
           cm.CommandText "select " coltitle " from " table " where " colid "=@" colid ""
 
           cm.Parameters.AddWithValue("@" colidfid)
 
           If con.State ConnectionState.Closed Then con.Open()
 
           Dim dr As SqlClient.SqlDataReader cm.ExecuteReader
            If dr
.Read Then
                Return dr
.Item(coltitle)
 
           End If
 
       End Using
    End 
Function 
التالية لمعرفة عدد صفوف شيء معين:
PHP كود :
Public Function rowsCountByid(table As Stringcolname As Stringcolid As Integer) As Integer
        Using cm 
As New SqlClient.SqlCommand(""con)
 
           cm.CommandText "select count(" colname ") from " table " where " colname "=@" colname ""
 
           cm.Parameters.AddWithValue("@" colnamecolid)
 
           If con.State ConnectionState.Closed Then con.Open()
 
           Return cm.ExecuteScalar
        End Using
    End 
Function 
اكيد الكود فيه قصور لكن الفكرة هي عدم تكرار صياغة Function  لكل هدف قد يكون مشابه.
مساء الخير