Mavjud qatorni o'zgartirish va to'liq qatorni o'chirish

Bir tugmani bosish orqali dasturni amalga oshirishga harakat qildim, dastur TextBoxes-dan barcha ma'lumotlarni oladi va uni oldingi qatorga qo'yadi va uni Access ma'lumotlar bazasida o'zgartiradi.

Har qanday DataGridView hujayrasidagi kontentlardan birini bosish bilan mukammal ishlaydigan dinamik ma'lumotlar ulashini amalga oshirdim, u tegishli TextBoxes-da butun qator uchun ma'lumotni qo'yadi. Men matn qutilarining ismlarini tegishli ustun nomlariga o'xshash tarzda aniqladim.

Men disk raskadrovka qilsam, dastur to'xtaydi va SQL buyrug'i bilan bog'liq muammo borligini aytadi.

To'liq qatorni qanday o'chirish va ma'lumotlar bazasini yangilash kodini ham talab qilaman va buni qanday amalga oshirishim kerak.

Mana kod yozdim:

namespace myProject
{
  public partial class EditCodons : Form
  {
    private OleDbConnection dataConnection;
    private int row;
    private string name;
    public EditCodons()
    {
      InitializeComponent();
      OpenDb();
    }

    private void EditCodons_Load(object sender, EventArgs e)
    {
     //TODO: This line of code loads data into the 'myProjectDataSet.tblCodons' table. You can move, or remove it, as needed.
      this.tblCodonsTableAdapter.Fill(this.myProjectDataSet.tblCodons);
    }

    private void OpenDb()
    {
      dataConnection = new OleDbConnection();
      try
      {
        dataConnection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
        dataConnection.Open();
      }
      catch (Exception e)
      {
        MessageBox.Show("Error accessing the database: " +
                         e.Message,
                         "Errors",
                         MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
      }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
      this.row = e.RowIndex;
      this.name = fullName.Text = dataGridView1.Rows[e.RowIndex].Cells[8].FormattedValue.ToString();
      codon1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
      codon3.Text = dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString();
      triplet1.Text = dataGridView1.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
      triplet2.Text = dataGridView1.Rows[e.RowIndex].Cells[3].FormattedValue.ToString();
      triplet3.Text = dataGridView1.Rows[e.RowIndex].Cells[4].FormattedValue.ToString();
      triplet4.Text = dataGridView1.Rows[e.RowIndex].Cells[5].FormattedValue.ToString();
      triplet5.Text = dataGridView1.Rows[e.RowIndex].Cells[6].FormattedValue.ToString();
      triplet6.Text = dataGridView1.Rows[e.RowIndex].Cells[7].FormattedValue.ToString();
      fullName.Text = dataGridView1.Rows[e.RowIndex].Cells[8].FormattedValue.ToString();
      start.Text = dataGridView1.Rows[e.RowIndex].Cells[9].FormattedValue.ToString();
      end.Text = dataGridView1.Rows[e.RowIndex].Cells[10].FormattedValue.ToString();
    }

    private void addRow_Click(object sender, EventArgs e)
    {
    }

    private void update_Click(object sender, EventArgs e)
    {
      string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
      OleDbConnection myConnection = new OleDbConnection(connectionString);
      string myInsertQuery = "INSERT INTO tblCodons (codonsCodon1, codonsCodon3, codonsTriplet1, codonsTriplet2, codonsTriplet3,codonsTriplet4, codonsTriplet5, codonsTriplet6, codonsFullName, codonsStart, codonsEnd  )" +
            " Values(('codon1.Text', 'codon3.Text', 'triplet1.Text', 'triplet2.Text', 'triplet3.Text','triplet4.Text', 'triplet5.Text', 'triplet6.Text', 'fullName.Text', 'start.Text', 'end.Text')" +
            "WHERE codonsFullName =" + this.name;
      OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
      myCommand.Connection = myConnection;
      myConnection.Open();
      myCommand.ExecuteNonQuery();
      myCommand.Connection.Close();
    }

    private void reset_Click(object sender, EventArgs e)
    {
      codon1.Clear();
      codon3.Clear();
      triplet1.Clear();
      triplet2.Clear();
      triplet3.Clear();
      triplet4.Clear();
      triplet5.Clear();
      triplet6.Clear();
      start.Clear();
      end.Clear();
      fullName.Clear();
    }

    private void deleteRow_Click(object sender, EventArgs e)
    {

    }

  }
}

* EDIT: * Sizning SQL o'zgarishingizni sinab ko'rdim va "xato ro'yxati" yozildi

Xato 1 Faqat tayinlash, chaqirish, ortish, kamaytirish va yangi ob'ekt so'zlari C: \ Projects_2012 \ Project_Noam \ Project \ myProject \ myProject \ EditCodons.cs 82 59 myProject sifatida ishlatilishi mumkin Xato 2 noto'g'ri ifoda iborasi ')' C: \ Projects_2012 \ Project_Noam \ Project \ myProject \ myProject \ EditCodons.cs 82 60 myProject Xato 3; kutilgan C: \ Projects_2012 \ Project_Noam \ Project \ myProject \ myProject \ EditCodons.cs 82 60 myProject shuning uchun uni o'zgartiraman:

string myInsertQuery =(String.Format("UPDATE tblCodons SET codonsCodon1='{0}', codonsCodon3='{1}', " + 
        "codonsTriplet1='{2}', codonsTriplet2='{3}', codonsTriplet3='{4}', codonsTriplet4='{5}', " + 
        "codonsTriplet5='{6}', codonsTriplet6='{7}', codonsFullName='{8}', codonsStart='{9}', " + 
        "codonsEnd='{10}' WHERE codonsFullName='{11}'",
        triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
        fullName.Text, start.Text, end.Text, this.name));

va po'latdan ishlamaydi. va oxirgi izohdan xatoga yo'l qo'yaman.

0

1 javoblar

Bu bilan bir necha muammo bor.

Birinchidan, SQL buyruqlarida "qiymatlar" dan keyin ikkita ochiq parentez mavjud, ulardan biri yopiq emas. Ikkinchisi, aslida siz kiritgan o'zgaruvchilardan siz kiritadigan so'zda asl qiymati emas, balki "codon1.Text", "codon3.Text" kabi ma'lumotlar bazasiga kiritishga harakat qilyapsiz. Ustun ma'lumot turlariga qarab, bu xatoga olib kelishi mumkin.

Biroq, bu bilan asosiy muammo, siz Qo'shimchalar iborasi bilan yangilanishlarni qila olmasligingizdir. Aksincha, sizga o'xshab ko'rinadigan, yangilanish bayonnomasidan foydalaning

UPDATE tableName SET column=value WHERE column=something

SQL kodini belgilab qo'yadigan kod satri quyidagicha bo'lishi kerak:

//Linebreaks used to make the code easier to read.
string myInsertQuery = String.Format("UPDATE tblCodons SET codonsCodon1='{0}', codonsCodon3='{1}', " + 
            "codonsTriplet1='{2}', codonsTriplet2='{3}', codonsTriplet3='{4}', codonsTriplet4='{5}', " + 
            "codonsTriplet5='{6}', codonsTriplet6='{7}', codonsFullName='{8}', codonsStart='{9}', " + 
            "codonsEnd='{10}' WHERE codonsFullName='{11}'",
            codon1.Text, codon3.Text, triplet1.Text, triplet2.Text,
            triplet3.Text, triplet4.Text, triplet5.Text, triplet6.Text,
            fullName.Text, start.Text, end.Text, this.name);

Ushbu so'z codonsFullName bu.name o'zgaruvchida saqlanganiga teng bo'lgan har qanday satrni yangilaydi.

O'chirish osonroq va quyidagilar bilan amalga oshiriladi:

string myDeleteQuery = "DELETE FROM tblCodons WHERE codonsFullName='" + this.name + "'";

codonsFullName faylini o'chirishni xohlayotganingizni nazarda tutadi.

Tahrirlash:

Men javobni tahrir qilib, asl nusxasini kiritishni unutgan birgina tirnoqlarni kiritish uchun tahrir qildim.

0
qo'shib qo'ydi
updaye tugmachasini bosishga harakat qilganda xato xabari ko'rsatiladi: System.Data.OleDb.OleDbException (0x80040E10): kerakli parametrlardan birini yoki bir nechtasini qiymatini o'zgartira olmaydi
qo'shib qo'ydi muallif user1017315, manba
yana bir bor rahmat "@Men javob berish uchun, lekin endi boshqa xato: System.Data.OleDb.OleDbException (0x80040E14): sintaksisi xatosi (yo'qolgan operator) so'rovni ifodalaydi".
qo'shib qo'ydi muallif user1017315, manba
() Da "myInsertQuery" ni qo'ydim va hozir bu xato paydo bo'ldim: System.FormatException: Indeks (nolga asoslangan) noldan katta yoki teng va argumentlar ro'yxatidan kattaroq bo'lishi kerak.
qo'shib qo'ydi muallif user1017315, manba
Oh, rahmat! perectly ishlaydi! siz ajoyibsiz!
qo'shib qo'ydi muallif user1017315, manba
To'liq qatorni o'chirib tashlamoqchi bo'lsangiz, mening endi tahrirlangan javobingizning ikkinchi qismi dasturingizdan chaqirilganda ishlashi kerak: string myDeleteQuery = "TblCodonsdan qaerga codonsFullName =" "this.name +" ""; Aslida, agar kerak bo'lsa da birlamchi tirnoqlarni kiritishni unutgan edim. Yuborgan xatolik, qiymatlar atrofida bitta tirnoq mavjud bo'lmaganida, albatta, yo'qolgan yoki bo'sh bo'lgan qiymat ekanligini ko'rsatdi. O'zgartirishlarimni sinab ko'rsangiz, u ishlamoqda.
qo'shib qo'ydi muallif Ben, manba
Fikringizni joylashtirasizmi yoki tahrirlanganingiz bilan asl savolingizni tahrir qila olasizmi? Men yuborgan kodni ishlatib, men oddiy kodli jadvalga ulanishga muvaffaq bo'ldim va codonsFullName asosida satrlarni yangiladim.
qo'shib qo'ydi muallif Ben, manba
Men yozganimda tasodifan yopilgan parantezda qoldirgan edim, shuning uchun siz boshida qo'shimcha parantez qo'shishingiz kerak edi. Yangi xat olishning sababi shundaki, siz parterning birinchi satrini yo'qotib qo'yishingiz mumkin. codon1.Text, codon3.Text, triplet1.Text, triplet2.Text, ya'ni satrda ko'rsatilgan parametrlar sonidan kamroq foydalanasiz.
qo'shib qo'ydi muallif Ben, manba