cn=new SqlConnection("server=UST2C\\UST2CSQL;uid=terrasoft1;pwd=gjcnyjt;database=TerraSoftCRM_T");
myFilter="Select ID,YearName,MonthName,Seriya,Model,Machta,KolvoMiddle,KolvoEnd,Main from sgz_PlanPogrKomatsu"+" Where YearName like \'"+currentyear.ToString()+"\' and MonthName like \'"+currentmonth.ToString()+"\'"+" and Main='1'";
BaseAdapter=new SqlDataAdapter();
SelectCommand=new System.Data.SqlClient.SqlCommand();
BaseAdapter.SelectCommand=SelectCommand;
SelectCommand.CommandText=myFilter;
SelectCommand.Connection=cn;
BaseBuilder = new SqlCommandBuilder(BaseAdapter);
myDS=new DataSet("Base");
BaseAdapter.Fill(myDS,"sgz_PlanPogrKomatsu");
BaseTable=myDS.Tables["sgz_PlanPogrKomatsu"];
BaseTable.TableName="sgz_PlanPogrKomatsu";
DataColumn[] way=new DataColumn[1];
way[0]=BaseTable.Columns["ID"];
BaseTable.PrimaryKey=way;
BaseTable.Columns["ID"].AutoIncrement=true;
,,,,,,,,,,
далее удаление строк,сравниваются строки двух таблиц,если в другой таблице я не нахожу нужной строки,то удаляю строку из своей таблицы
cn.Open();
rowList=new ArrayList();
string myCommand="";
SqlCommand killCmd = new SqlCommand();
for(int row=0;row<BaseTable.Rows.Count;row++)
{
DataRow ro=BaseTable.Rows[row];
string Filter="Seriya = '"+ro[3]+"'"+" and Machta = '"+ro[5]+"'"+" and Model = '"+ro[4]+"'";
DataRow[] find=myTable.Select(Filter);
if (find.Length==0)
{
rowList.Add(row);
myCommand=myCommand+" DELETE FROM sgz_PlanPogrKomatsu where "+"Seriya= '"+ro[3]+"'"+" and Machta = '"+ro[5]+"'"+" and"+" Model = '"+ro[4]+"'"+"and MonthName = '"+currentmonth+"'"+" and YearName = '"+currentyear+"' and Main='1';";
rowList.Add(row);
}
if (rowList.Count>0)
{
int dlina=myCommand.Length-1;
myCommand=myCommand.Substring(0,dlina);
killCmd.CommandText=myCommand;
killCmd.Connection=cn;
killCmd.ExecuteNonQuery();
}
rowList.Sort();
for (int arrow=(rowList.Count-1);arrow>0;arrow--)
{
BaseTable.Rows.RemoveAt((int)rowList[arrow]);
}
,,далее делаю апдэйт
MyDataGrid.Refresh();
int count=BaseAdapter.Update(myDS,"sgz_PlanPogrKomatsu");
cn.Close();