PDA

View Full Version : سوال: مشکل جستجو در بان اطلاعاتی



jeson_park
چهارشنبه 08 خرداد 1392, 23:22 عصر
سلام
کد زیر رو برای جستجوی رکورد های بانک اطلاعاتی نوشتم
اما کار نمی کنه
با وجود اینکه مقدار مورد نظر در بانک اطلاعاتی هست اما پیداشت نمی کنه و پیام not Found رو چاپ میکنه



string ConStr = "server=(local);database=mydatabase;Trusted_Connect ion=yes;";
SqlConnection conn = new SqlConnection(ConStr);

String strCommand = "select * from tblStudent where Name = '" + txtName.Text + "' AND Family = '" + txtFamily.Text + "' AND Shenasnameh = "+ txtShenasname.Text + " AND BirthYear = " + txtBirthYear.Text ;

// string.Format(strCommand,txtName.Text,txtFamily.Te xt,Convert.ToInt64(txtShenasname.Text), Convert.ToInt16(txtBirthYear.Text));

conn.Open();

SqlCommand cmd = new SqlCommand(strCommand, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
Response.Write("Search Compelete!");
}
else
{
Response.Write("Not Found");
}
conn.Close();

مشکل از چیه؟؟:متفکر:

parvizwpf
چهارشنبه 08 خرداد 1392, 23:28 عصر
بابا پارامترم چیز خوبیه دیگه این جور کد نویسی رو بی خیال شو. همیشه امکان بوجود آمدن مشکل توی این نوع کوئری نویسی موجوده. بهتره از پارهمترها استفده کنی.

tooraj_azizi_1035
چهارشنبه 08 خرداد 1392, 23:58 عصر
ExecuteNonQuery برای دستورات تغییر دهنده داده ها تعداد ردیف های تاثیر یافته را بر می گرداند اما
برای دستور SELECT مقدار -1 را بر میگرداند. شما باید با ExecuteReader کار کنید.

omidrajabitiz
پنج شنبه 09 خرداد 1392, 09:13 صبح
سلام
دستور String strCommand به صورت زیر تغییر بده! انشاله که درست میشه!


String strCommand = "select * from tblStudent where Name = '" + txtName.Text.Trim() + "' AND Family = '" + txtFamily.Text.Trim() + "' AND Shenasnameh = " + txtShenasname.Text.Trim() + " AND BirthYear = " + txtBirthYear.Text.Trim();

jeson_park
پنج شنبه 09 خرداد 1392, 10:33 صبح
سلام
دستور String strCommand به صورت زیر تغییر بده! انشاله که درست میشه!


String strCommand = "select * from tblStudent where Name = '" + txtName.Text.Trim() + "' AND Family = '" + txtFamily.Text.Trim() + "' AND Shenasnameh = " + txtShenasname.Text.Trim() + " AND BirthYear = " + txtBirthYear.Text.Trim();


trim برای حذف white space استفاده می شه ربطی نداره!!!
در ExecuteReader چه طوری باید چک کنم که آیا نتیجه درست هست یا نه؟؟
کسی کد کامل نداره برام بزاره؟؟

jeson_park
پنج شنبه 09 خرداد 1392, 10:50 صبح
با دستور String.format هم نوشتم


String strCommand = "select * from tblStudent where Name = '{0}' AND Family = '{1}' AND Shenasnameh = '{2}' AND BirthYear = '{3}'";
string.Format(strCommand,txtName.Text,txtFamily.Te xt,txtShenasname.Text, txtBirthYear.Text);

خطا می ده
Error converting data type varchar to bigint

pcseven
پنج شنبه 09 خرداد 1392, 11:16 صبح
یکی از ستونها در دیتابیس نوع داده bigint داره ولی شما داری string یا همون varchar ارسال می کنی!

ضمناً از پارامتر استفاده کنید تا امکان خطا و sql injection تا حدودی کمتر بشه

tooraj_azizi_1035
پنج شنبه 09 خرداد 1392, 11:29 صبح
در مورد خطا مثل اینکه فیلد Shenasnameh نباید داخل '' باشه.
چک کردن حاصل SELECT:


cmd.CommandText = "SELECT COUNT(*) FROM dbo.region";
Int32 count = (Int32) cmd.ExecuteScalar();

jeson_park
پنج شنبه 09 خرداد 1392, 12:07 عصر
سلام
مشکلم حل شد
کد رو می زارم تا اگه کسی در آینده لازم داشت استفاده کنه
با تشکر از همه دوستان


string ConStr = "server=(local);database=mydatabase;Trusted_Connect ion=yes;";
SqlConnection conn = new SqlConnection(ConStr);
conn.Open();
String strCommand = "select * from tblStudent where Name = @name AND Family = @family AND Shenasnameh = @shenasname AND BirthYear = @birthyear";

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

cmd.CommandType = CommandType.Text;
cmd.CommandText = strCommand;
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@family", txtFamily.Text);
cmd.Parameters.AddWithValue("@shenasname", txtShenasname.Text);
cmd.Parameters.AddWithValue("@birthyear", txtBirthYear.Text);
SqlDataReader dr = cmd.ExecuteReader();


if (dr.HasRows)
{ Response.Write("Search Compelete!");
}
else
{
Response.Write("Not Found");
}

conn.Close();