PDA

View Full Version : جستجو جدول داخل Dataset بدون Primary key



didaaa
شنبه 24 بهمن 1388, 11:59 صبح
بچه همون جور كه از عنوان تاپيك معلومه من مي خوام داخل يه جدول كه داخل يه ديتاست هستش و كليد اصلي هم نداره بگردم اگه ميشه كمك كنيد چه كدي بنويسم. ممنون

unique1984
شنبه 24 بهمن 1388, 12:44 عصر
سلام
برای جستجو نیازی به فیلد کلید نیست.با توجه به فیلد مورد نظرتون جستجو کنین.

msdsbi
شنبه 24 بهمن 1388, 12:59 عصر
DataRow[] dr = ds.Tables["Table"].Select("Field='" + شرط + "'", "Field DESC");
string ss = (dr[0]["Filed2"].ToString());

این مشکلتو حل می کنه

asefy2008
شنبه 24 بهمن 1388, 13:20 عصر
فکر نکنم بشه جدول بدون کلید داشت(شاید دارم اشتباه میگم).اگر دیتابیس رو درست طراحی کرده باشی فکر کنم داشته باشه.

didaaa
شنبه 24 بهمن 1388, 20:07 عصر
ممنون که جواب میدید!
راستش من دارم یه برنامه چتد لایه می نویسم تو این برنامم یه کلاس در لایه common تعریف که از detaset ارث بری میکنه و داخلش یک جدول با کد طراحی کردم بعد تو لایه DataAccess یک کلاس براش نوشتم بصورت زیر

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CrisisCommon;
using System.Data;

namespace CrisisDataAccess
{
public class UCArrestedDA
{
SqlDataAdapter da;
public UCArrestedDA()
{
da = new SqlDataAdapter("select * from TblArrested", new myConnection().Cnn);
}



private SqlCommand CreateDeleteCommand()
{

SqlCommand cmd = new SqlCommand();
cmd.Connection = new myConnection().Cnn;
cmd.CommandText = "DeleteArrested";
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@IDForm", SqlDbType.Int).SourceColumn = UCArrestedDataset.IDForm_Field;
cmd.Parameters.Add("@ID", SqlDbType.Int).SourceColumn = UCArrestedDataset.ID_Field;
return cmd;
}

#region CRUD





public string Delete(UCArrestedDataset data)
{
try
{
da.DeleteCommand = this.CreateDeleteCommand();
da.Update(data, UCArrestedDataset.Table_Name);
return "true";
}
catch (Exception ex)
{
return ex.Message;
}
}


#endregion


}
}
و بعد در لایه BisinessRole یه کلاس دیگه تعریف کردم به صورت زیر:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrisisCommon;
using CrisisDataAccess;
namespace CrisisBusinessRules
{
public class UCArrestedBR
{
UCArrestedDA tda;
public UCArrestedBR()
{
tda = new UCArrestedDA();
}

public UCArrestedDA getrecordsBR(UCArrestedDataset data)
{
return null;
}

public string InsertMain123(UCArrestedDataset data)
{
return tda.Insert(data);
}

public string UpdateMain123(UCArrestedDataset data)
{
return tda.Update(data);
}

public string DeleteMain123(UCArrestedDataset data)
{
return tda.Delete(data);
}

}
}


وبعد در لایه UI این کدو نوشتم:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CrisisCommon;
using CrisisBusinessRules;
using Janus.Windows.GridEX;
using System.Globalization;
using System.Collections;

namespace CrisisWinUI.UserControls
{
public partial class ArrestedUC : UserControl
{



BindingclassBR brbinding;
UCArrestedDataset data;
UCArrestedBR br;

DateAndTimeBR dateTime = new DateAndTimeBR();
public ArrestedUC()
{


InitializeComponent();
brbinding = new BindingclassBR();
data = new UCArrestedDataset();
br = new UCArrestedBR();

}




private void panel9_Paint(object sender, PaintEventArgs e)
{

}

private void gridArrested_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void gridArrested_CurrentCellChanged(object sender, EventArgs e)
{



}


private void btnDel_Click(object sender, EventArgs e)
{
DataRow drD;
object[] keys = { txt1.Text, str.Text,txtArretedNo.Text };
drD = data.Tables[UCArrestedDataset.Table_Name].Rows.Find(keys);

if (drD != null)
{
if (MessageBox.Show("آیا از خدف رکورد مطمئن هستید", "توجه", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
drD.Delete();

if (br.DeleteArrested(data) == "true")
{
MessageBox.Show("Success");



}
else

}
}
}


}
}



حالا نمیدونم با اینکه مثلا رکوردی با فلان مشخصات هست ولی حذف نمیکنه؟ تا یه جایی خودم فهمیدم ولی ندونستم چه جوری حلش کنم فکر کنم وقتی دیتا ستی که ساختم و وقتی ازش نمونه گیری میکنم داخلش هیچ رکوردی نیست یعنی تو این قسمت
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CrisisCommon;
using System.Data;

namespace CrisisDataAccess
{
public class UCArrestedDA
{

public SqlDataAdapter da;
public UCArrestedDA()
{
da = new SqlDataAdapter("select * from TblArrested", new myConnection().Cnn);

}


private SqlCommand CreateDeleteCommand()
{

SqlCommand cmd = new SqlCommand();
cmd.Connection = new myConnection().Cnn;
cmd.CommandText = "DeleteArrested";
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@IDForm", SqlDbType.Int).SourceColumn = UCArrestedDataset.IDForm_Field;
cmd.Parameters.Add("@ID", SqlDbType.Int).SourceColumn = UCArrestedDataset.ID_Field;
cmd.Parameters.Add("@ArrestedNo", SqlDbType.Int).SourceColumn = UCArrestedDataset.ArrestedNo_Field;

return cmd;
}



public string Delete(UCArrestedDataset data)
{



try
{

da.DeleteCommand = this.CreateDeleteCommand();
da.Update(data, UCArrestedDataset.Table_Name);
return "true";
}
catch (Exception ex)
{
return ex.Message;
}
}





}
}