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

please check my code

بدأه Eng_Ranya في 3 أبريل 2011 · 8 رد · 1,423 مشاهدة · في ASP.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
                string sql = "select DISTINCT id,MAX(enddate) as enddate from table1 group by id";
                SqlCommand cmd = new SqlCommand(sql, con);
                string d1 = DateTime.Now.ToString("dd/m/yyyy");
                DateTime datenow = new DateTime();
                datenow = Convert.ToDateTime(d1);

                DateTime enddate = new DateTime();
                enddate = Convert.ToDateTime(cmd.ExecuteScalar());

                if (DateTime.Compare(datenow, enddate) > 0)
                {

                    string sql1 = "update users set type='Suspended Users' where users.id in(select DISTINCT id from table1 where enddate ='" + enddate + "')";
                    SqlCommand cmd1 = new SqlCommand(sql1, con);
                    cmd1.ExecuteNonQuery();
                    for (int i = 1; i < 5; i++)
                    {
                        int period = 1;
                        string sql2 = "update table3 set period='" + period + "'";
                        SqlCommand cmd2 = new SqlCommand(sql2, con);
                        cmd2.ExecuteNonQuery();
                        period++;
                    }

my tables are

table1:

id|enddate

______________

user table:

tableid|type|id

___________________

table3:

pid|period|id

______________

1 |1 |1

2 |2 |1

3 |1 |2

4 |3 |1

5 |2 |2

#2

الأخت / Eng. Ranya

السلام عليكم ورحمة الله وبركاته

إذا كان هناك خطأ في الـ Runtime فأنا أعتقد إنه في الجزء المتعلق بالـ SQL Parameter في السطر التالي

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
                string sql = "select DISTINCT id,MAX(enddate) as enddate from table1 group by id";
                SqlCommand cmd = new SqlCommand(sql, con);
                string d1 = DateTime.Now.ToString("dd/m/yyyy"); // 'm' inside the format string would result to excperiotn as it would be representing 'minutes'. Minutes could be greater than 12 or 31. Use 'M' instead of 'm' if you want to get the months

أتمنى لكي التوفيق والنجاح

تم تعديل هذه المشاركة بواسطة Robatic في 3 أبريل 2011 في 16:37

#3

ظبطت التاريخ ولكن لا يحدث اى تغيير

#4

مرحبا الرجاء ارسال مسج الerror على الايميل

dhananny2@hotmail.com

لمعرفة ما هو الاerror

#5

الخطأ في نص الإتصال بقاعدة البيانات

هذا الكود

إذا لم ينجح وضحي أسماء الجداول في قاعدة البيانات وإسم القاعدة

يجب أن يكون هكذا

     string con;

        con = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = con;


        string sql = "select DISTINCT id,MAX(enddate) as enddate from table1 group by id";
        SqlCommand cmd = new SqlCommand(sql, sqlcon);
        string d1 = DateTime.Now.ToString("dd/M/yyyy");
        DateTime datenow = new DateTime();
        datenow = Convert.ToDateTime(d1);

        DateTime enddate = new DateTime();
        enddate = Convert.ToDateTime(cmd.ExecuteScalar());

        if (DateTime.Compare(datenow, enddate) > 0)
        {

            string sql1 = "update users set type='Suspended Users' where users.id in(select DISTINCT id from table1 where enddate ='" + enddate + "')";
            SqlCommand cmd1 = new SqlCommand(sql1, sqlcon);
            cmd1.ExecuteNonQuery();
            for (int i = 1; i < 5; i++)
            {
                int period = 1;
                string sql2 = "update table3 set period='" + period + "'";
                SqlCommand cmd2 = new SqlCommand(sql2, sqlcon);
                cmd2.ExecuteNonQuery();
                period++;
            }
        }
    }

تم تعديل هذه المشاركة بواسطة KV2010 في 3 أبريل 2011 في 19:52

الله أكبر الله أكبر الله أكبر لآ إله إلا الله الله أكبر الله أكبر ولله الحمد

#6
Eng_Ranya كتب:

ظبطت التاريخ ولكن لا يحدث اى تغيير

أختي العزيزة / Eng_Ranya

إذا ضبط الكود ولم يكن هناك أي Runtime Error، يبقى إحتمال أن الشرط في جملة الـ SQL الخاصة بالتعديل لا يتحقق. الرجاء قومي يتعديل جملة الـ SQL بما يتناسب مع الشكل التالي

SqlCommand updateCommand = new SqlCommand("UPDATE ..... WHERE enddate = @enddate", your_sqlConnectionObject);

// The following is a C# 3.0 new intializer feature

updateCommand.Parameters.Add(
	new SqlParameter()
	{
		ParameterName = "enddate",
		SqlDbType = SqlDbType.DateTime,		// Important as DateTime is a little bit tricky, 'AddWithValue' will result in an error
		Value = your_where_clause_datetime_value
	}
);

updateCommand.ExecuteNonQuery();

رجاء أختي العزيزة لا تقومي ببناء جمل SQL عن طريق دمجها بمتغيرات كما هو موجود حاليا، صدقيني بهذه الطريقة أنك تفتحين مجال للتعدي على بياناتك

string commandText = "UPDATE ..... WHERE X = " + x + " AND Y = " + y ........    // This is not a recomended approach, it is prone to SQL Injection

الـ SQL Injection هو عبارة عن حقن جملة الـ SQL بحيث تكون دائما صائبة بالتالي يتم الحصول على البيانات أو حذفها أو تعديلها، خذي المثال التالي

string commandText= "DELETE FROM ImportantDataTable WHERE Password = '" + password + "'";

// Imagine a malicious person typed something like this as password
// Malicious Person typed : I will hack your system' OR '1'='1
// The final outcome would be

// DELETE FROM ImportantDataTable WHERE Password = 'I will hack your system' OR '1'='1'"

// Which is always true
// Programming is an ugly world!

أتمنى للجميع التوفيق والنجاح

تم تعديل هذه المشاركة بواسطة Robatic في 3 أبريل 2011 في 21:06

#7

اخى Robatic

شكرا على التوضيح ولكن انا تعبت من الكود

ظبطته باكثر من طريقة برمجية ولكن مفيش فايدة

هاقول حضرتك عايزة اعمل ايه وتكتبلى الكود لو سمحت

table 1:

table1id| enddate | id

_________________

1 | 27/10/2010 | 1

2 | 28/11/2011 | 2

3 | 5/12/2011 | 1

4 | 8/9/2010 | 2

________________

table 2 :

id| type

_________

1 | active

2 | active

________

عايزة اجيب اخر تاريخ تم ادخاله لكل يوزر من الجدول الاول واذا كان التاريخ اكبر من تاريخ اليوم يغير فى الجدول التانى حالة اليوزر اللى تاريخه طلع اكبر من تاريخ اليوم

المشكلة فى جملة update لانها مش عارفة توصل ل id لليوزر المفروض يتعدل حالته :unsure: :unsure:

#8
Eng_Ranya كتب:

اخى Robatic

شكرا على التوضيح ولكن انا تعبت من الكود

ظبطته باكثر من طريقة برمجية ولكن مفيش فايدة

هاقول حضرتك عايزة اعمل ايه وتكتبلى الكود لو سمحت

table 1:

table1id| enddate | id

_________________

1 | 27/10/2010 | 1

2 | 28/11/2011 | 2

3 | 5/12/2011 | 1

4 | 8/9/2010 | 2

________________

table 2 :

id| type

_________

1 | active

2 | active

________

عايزة اجيب اخر تاريخ تم ادخاله لكل يوزر من الجدول الاول واذا كان التاريخ اكبر من تاريخ اليوم يغير فى الجدول التانى حالة اليوزر اللى تاريخه طلع اكبر من تاريخ اليوم

المشكلة فى جملة update لانها مش عارفة توصل ل id لليوزر المفروض يتعدل حالته :unsure: :unsure:

أختي العزيزة / Eng.Ranya

على فرض إنه table1.id هو Foreign Key للـ table2.id، وأن table2.id هو الـ id الخاص بالـ Users، وعلى فرض إنه أنا أساسا فهمت الفكرة بشكل صحيح، إن شاء الله الكود التالي يخدم هدفك

using (SqlConnection con = new SqlConnection("Your Connection String")) {

	SqlCommand cmd = new SqlCommand(
		"SELECT id, MAX(enddate) FROM table1 GROUP BY id",
		con
	);

	DateTime today = DateTime.NOW;

	con.Open();

	// Execute Scalar will return the first column from the first row of the resulting data set, no need for scalars a loop is required

	SqlCommand updateCommand = new SqlCommand(
		"UPDATE table2 SET [type] = 'Suspended' WHERE id=@id",
		con);
	SqlParameter idParam = updateCommand.Parameters.Add();
	idParam.ParameterName = "id";


	using(SqlDataReader reader = cmd.ExecuteReader()){
		while(reder.Read()){
			if(today > DateTime.Parse(reader["enddate"].ToString())) {
				idParam.Value = reader["id"];
				updateCommand.ExecuteNonQuery();
			}

		}
	}
}

أتمنى للجميع التوفيق والنجاح

تم تعديل هذه المشاركة بواسطة Robatic في 4 أبريل 2011 في 01:11

#9

اخى

تم تعديل فى هذا الكود

SqlParameter idParam = updateCommand.Parameters.Add("@id",SqlDbType.Int);

ولكن لم يحدث اى تغيير :unsure: :unsure:

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