PDA

View Full Version : سوال: تیدیل کدهای اتصال به بانک access بهsql



mahan206
جمعه 25 مهر 1393, 01:41 صبح
سلام دوستان و اساتید گرامی من تو پروژم چنین کدهای دارم میخواستم همین کدها رو برای بانک sqlبنویسم اما بلد نیستم اگه کسی بلده لطف کنه و بگه کجاش باید تغییر کنه یا تغیرات رو اعمال کنه و کدش رو بزاره ممنون میشم کسی کمکم کنه

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;


public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// if (Session["userLogin"] != null)
// {
// lblStatus.Text = Session["userLogin"].ToString() + "خوش آمدید";
// lblStatus.Text += "<a href=\"logout.aspx\">خروج</a>";
// }
// else
// {
// Response.Redirect("Login2.aspx");
// }
string MyPage = System.IO.Path.GetFileName(Request.Path).ToLower() ;

using (OleDbConnection Con = new OleDbConnection(ConfigurationManager.ConnectionStr ings["ConnectionString1"].ConnectionString))
{
OleDbCommand cmd = new OleDbCommand("select * from tbl_pages where PageName=?", Con);
cmd.Parameters.AddWithValue("PageNam", MyPage);
Con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (Page.User.Identity.IsAuthenticated)
{
if (!Page.User.IsInRole(dr["PageRole"].ToString()))
{
Response.Redirect("MsgPage.aspx");
}
}
else
{
FormsAuthentication.RedirectToLoginPage();
}

}
Con.Close();
}


if (Page.User.Identity.IsAuthenticated)
{
Button1.Visible = true;
}
else
{
Button1.Visible = false;
}



}

protected void HiddenField1_ValueChanged(object sender, EventArgs e)
{


}


private string GetMemberRoleByID(string UID)
{
using (OleDbConnection Con = new OleDbConnection(ConfigurationManager.ConnectionStr ings["ConnectionString1"].ConnectionString))
{
OleDbCommand cmd = new OleDbCommand("select roles from tb_admin where uid=?", Con);
cmd.Parameters.AddWithValue("uid", UID);
Con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
string RetVal = "NA";
if (dr.Read())
{
RetVal = dr["Roles"].ToString();
}


Con.Close();
return RetVal;
}
}
private bool GetMemberAuthenticate(string UID, string PWD)
{
using (OleDbConnection Con = new OleDbConnection(ConfigurationManager.ConnectionStr ings["ConnectionString1"].ConnectionString))
{
bool RetVal=false;
OleDbCommand cmd = new OleDbCommand("select count(*) from tbl_members where (uid=? And Pwd=?)", Con);
cmd.Parameters.AddWithValue("uid", UID);
cmd.Parameters.AddWithValue("Pwd", PWD);
Con.Open();
RetVal= Convert.ToBoolean(cmd.ExecuteScalar());
Con.Close();
return RetVal;
}
}




}


اینم یکی دیگش همینه تبدیل بشه کارم راه میفته دوستان

using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private string GetMemberRoleByID(string UID)
{
using (OleDbConnection ConnectionString = new OleDbConnection(ConfigurationManager.ConnectionStr ings["ConnectionString"].ConnectionString))
{
OleDbCommand cmd = new OleDbCommand("select roles from tb_admin where uid=?", ConnectionString);
cmd.Parameters.AddWithValue("uid", UID);
ConnectionString.Open();
OleDbDataReader dr = cmd.ExecuteReader();
string RetVal = "NA";
if (dr.Read())
{
RetVal = dr["Roles"].ToString();
}


ConnectionString.Close();
return RetVal;
}
}
private bool GetMemberAuthenticate(string UID, string PWD)
{
using (OleDbConnection ConnectionString = new OleDbConnection(ConfigurationManager.ConnectionStr ings["ConnectionString"].ConnectionString))
{
bool RetVal=false;
OleDbCommand cmd = new OleDbCommand("select count(*) from tb_admin where (uid=? And Pwd=?)", ConnectionString);

cmd.Parameters.AddWithValue("uid", UID);
cmd.Parameters.AddWithValue("Pwd", PWD);
ConnectionString.Open();
RetVal= Convert.ToBoolean(cmd.ExecuteScalar());

ConnectionString.Close();
return RetVal;


}
}

protected void btn_submit_Click(object sender, EventArgs e)
{
if (GetMemberAuthenticate(txt_uid.Text, txt_pwd.Text))
{
string role = GetMemberRoleByID(txt_uid.Text);
FormsAuthenticationTicket AuthTicket = new FormsAuthenticationTicket(1, txt_uid.Text, DateTime.Now, DateTime.Now.AddHours(1), false, role);
string encryptedTicket = FormsAuthentication.Encrypt(AuthTicket);
HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(AuthCookie);

Response.Redirect(FormsAuthentication.GetRedirectU rl(txt_uid.Text, chk_member.Checked));





}
else
{
lbl.Visible = true;
}
}
}


ممنون دوستان

mahan206
جمعه 25 مهر 1393, 12:45 عصر
دوستان من خودم یکم روش کار کردم این جور شد.

mahan206
جمعه 25 مهر 1393, 12:47 عصر
دوستان من خودم یکم روش کار کردم این جور شد.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// if (Session["userLogin"] != null)
// {
// lblStatus.Text = Session["userLogin"].ToString() + "خوش آمدید";
// lblStatus.Text += "<a href=\"logout.aspx\">خروج</a>";
// }
// else
// {
// Response.Redirect("Login2.aspx");
// }
string MyPage = System.IO.Path.GetFileName(Request.Path).ToLower() ;

using (SqlConnection Con = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select * from tbl_pages where PageName=?", Con);
cmd.Parameters.AddWithValue("PageNam", MyPage);
Con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (Page.User.Identity.IsAuthenticated)
{
if (!Page.User.IsInRole(dr["PageRole"].ToString()))
{
Response.Redirect("MsgPage.aspx");
}
}
else
{
FormsAuthentication.RedirectToLoginPage();
}

}
Con.Close();
}


if (Page.User.Identity.IsAuthenticated)
{
Button1.Visible = true;
}
else
{
Button1.Visible = false;
}



}

protected void HiddenField1_ValueChanged(object sender, EventArgs e)
{


}


private string GetMemberRoleByID(string UID)
{
using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select roles from tb_admin where uid=?", Con);
cmd.Parameters.AddWithValue("uid", UID);
Con.Open();
SqlDataReader dr = cmd.ExecuteReader();
string RetVal = "NA";
if (dr.Read())
{
RetVal = dr["Roles"].ToString();
}


Con.Close();
return RetVal;
}
}
private bool GetMemberAuthenticate(string UID, string PWD)
{
using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConnectionString"].ConnectionString))
{
bool RetVal=false;
SqlCommand cmd = new SqlCommand("select count(*) from tbl_members where (uid=? And Pwd=?)", Con);
cmd.Parameters.AddWithValue("uid", UID);
cmd.Parameters.AddWithValue("Pwd", PWD);
Con.Open();
RetVal= Convert.ToBoolean(cmd.ExecuteScalar());
Con.Close();
return RetVal;
}
}




}
در قسمت بالا بهSqlDataReader dr = cmd.ExecuteReader();گیر میده.
و کد پایین به RetVal= Convert.ToBoolean(cmd.ExecuteScalar());گیر میده و کلا سایت متوقف میشه

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private string GetMemberRoleByID(string UID)
{
using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select roles from tb_admin where uid=?", Con);
cmd.Parameters.AddWithValue("uid", UID);
Con.Open();
SqlDataReader dr = cmd.ExecuteReader();
string RetVal = "NA";
if (dr.Read())
{
RetVal = dr["Roles"].ToString();
}


Con.Close();
return RetVal;
}
}
private bool GetMemberAuthenticate(string UID, string PWD)
{
using (SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["ConnectionString"].ConnectionString))
{
bool RetVal=false;
SqlCommand cmd = new SqlCommand("select count(*) from tb_admin where (uid=? And Pwd=?)", Con);

cmd.Parameters.AddWithValue("uid", UID);
cmd.Parameters.AddWithValue("Pwd", PWD);
Con.Open();
RetVal= Convert.ToBoolean(cmd.ExecuteScalar());

Con.Close();
return RetVal;


}
}

protected void btn_submit_Click(object sender, EventArgs e)
{
if (GetMemberAuthenticate(txt_uid.Text, txt_pwd.Text))
{
string role = GetMemberRoleByID(txt_uid.Text);
FormsAuthenticationTicket AuthTicket = new FormsAuthenticationTicket(1, txt_uid.Text, DateTime.Now, DateTime.Now.AddHours(1), false, role);
string encryptedTicket = FormsAuthentication.Encrypt(AuthTicket);
HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(AuthCookie);

Response.Redirect(FormsAuthentication.GetRedirectU rl(txt_uid.Text, chk_member.Checked));





}
else
{
lbl.Visible = true;
}
}
}

mahan206
شنبه 26 مهر 1393, 19:42 عصر
کسی نیست جواب مارو بده

forodo
یک شنبه 27 مهر 1393, 01:45 صبح
سلام
متن ارور رو بذارید ببینیم حرف حسابش چیه.

mahan206
یک شنبه 27 مهر 1393, 14:19 عصر
در قسمت بالا بهSqlDataReader dr = cmd.ExecuteReader();گیر میده.
متنشم{"Incorrect syntax near '?'."}
به جاش @pagenamهم گزاشتم که تو بانکمه اما فایده نداره و کلا ارور دیگه نمیده ولی خب کاری هم انجام نمیده.کلا میخوام بدونم کد اولی رو بخوای به sqlبنویسی چطوره

forodo
یک شنبه 27 مهر 1393, 14:32 عصر
اینطوری بنویسید ببینید درست میشه:
SqlCommand cmd = new SqlCommand("select roles from tb_admin where uid=@ali", Con);

cmd.Parameters.AddWithValue("@ali", مقدار مورد نظر);


ali@ باید در داخل ورودی بانکتون تعریف شده باشه

mahan206
یک شنبه 27 مهر 1393, 15:24 عصر
ممنون از اینکه جواب دادی
اینجور نوشتم

SqlCommand cmd = new SqlCommand("select * from tbl_pages where PageName=@PageName", Con);
cmd.Parameters.AddWithValue("@PageName", MyPage);
Con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (Page.User.Identity.IsAuthenticated)
{
if (!Page.User.IsInRole(dr["PageRole"].ToString()))
{
Response.Redirect("MsgPage.aspx");
}
}
else
تو بانکم صفحه ای که تعریف کردم serch.aspxهست اما خب سایته دیگه ارور نمیده و بالا میاد اما سیاست امنیتی رو انجام نمیده مثل اکسس

forodo
یک شنبه 27 مهر 1393, 15:32 عصر
Page.User.Identity.IsAuthenticated و !Page.User.IsInRole(dr["PageRole"].ToString()) چه کاری انجام می دن؟

mahan206
یک شنبه 27 مهر 1393, 16:29 عصر
اینا کلا کدهای اماده بود با بانکش من استفاده کردم برای امنیت سایت با اکسس الان میخوام برای sql بنویسمش.
این قسمتش هم چک میکنه که ایا صفحه تو بانک با صفحه تو کوکی برابره و ایا کاربری که وارد شده دسترسی بهش داده یا نه. میخوای کد ها رو با بانک اکسسش بهت بدم ببینی؟

forodo
یک شنبه 27 مهر 1393, 17:00 عصر
اگه قرار بدی ممنون می شم.

mahan206
دوشنبه 28 مهر 1393, 11:31 صبح
http://20p.20p.ir/do.php?filename=141379060900141.rar
اینم کدهای استفاده شده برای امنیت یکی تو مستر پیج یکی تو لوگین یکی هم global.aspx و یه قسمتم تو وب کانفیگ
تشکر