PDA

مشاهده نسخه کامل : مشکل در c#



aka1
01-10-10, 11:46
سلام دوستان من میخوتم یک login بسازم که علاوه بر گرفتن user &pass اگر شخص expire date یا تاریخ انقضاش نگذشته بود اجازه ی ورود داشته باشه
من این قسمت تاریخش رو مشکل دارم



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace neww
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DateTime dteDate;
DateTime dtedate2;
dteDate = DateTime.Now;

if (txtusername.Text == "" || txtpassword.Text == "")
{
MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "Data Source=localhost;Initial Catalog=new;Integrated Security=True";
myConnection.Open();
SqlCommand cmd = new SqlCommand("select * from users ", myConnection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
while (dr.Read())
{
string x = dr["username"].ToString();
string y = (dr["passwrod"].ToString());
dtedate2=dr["exp_date"];
if (x == txtusername.Text && y == txtpassword.Text&& dteDate=>dtedate2 )
{
mainform frm = new mainform();
frm.Show();
this.Hide();
}
if (x == txtusername.Text && y == txtpassword.Text&& dteDate<dtedate2 )
{
MessageBox.Show("your time is over","expired!");
}
if
(x != txtusername.Text)
{
MessageBox.Show("enter right user & password", "wrong identity", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if
(x == txtusername.Text && y != txtpassword.Text)
{
MessageBox.Show("enter right password", "wrong password", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

dr.Close();
myConnection.Close();
}
}
}
}
}

وقتی میخوام زمان رو از db بگیرم نمیتونم اون را در محیط c# با زمان حال مقایسه کنم یهنی نمیدونم باید تبدیل کرد چیزی رو یا نه ممنون میشم اگه کنک کنید
در ضمن از c# و sqlserver استفاده میکنم

germ
01-10-10, 15:09
شما یا باید تاریخی که از دیتابیس میخونید رو به dateTime تدیل کنید یا به استرینگ تبدیل کنید و با هم مقایسه کنید

اگر فرض کنیم تاریخ به فرمت yyyy/mm/dd باشه :




if ( dteDate.ToString("yyyy/MM/dd").compareTo(dr["exp_date"].ToString()) > 0)

aka1
02-10-10, 10:44
آقا دمت گرم ممنون

یک سواله دیگه!! چجور میشه یک متغیر تعریف کرد که مثلا یک رشته بگیره و در یک windows form دیگه از اون مقدار استفاده کرد؟!

aka1
04-10-10, 16:59
کسییییییی نبود؟!!