如何在此代码中使用事务?

时间:2016-12-22 19:21:14

标签: c# sql-server

我试图在没有事务的情况下使用此代码,但它给了我错误,所以我想在事务中使用它。我怎么用呢?

我的代码是:

SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");

SqlCommand cmd;
SqlDataAdapter adapt;

private void btn_Update_Click(object sender, EventArgs e)
{
    string query = "insert into users(Name, Password) values('ubaid', 'ali')";
    cmd = new SqlCommand(query, con);

    con.Open();
    cmd.ExecuteNonQuery();

    MessageBox.Show("Record Updated Successfully");
    con.Close();
}

1 个答案:

答案 0 :(得分:0)

import { Injectable } from '@angular/core';
import { Category } from 'yourPath'; // add the correct path here!
import { IBook } from 'yourPath'; // add the correct path here!

@Injectable()
export class BooksService {

    books: Array<IBook> = [];
    idNumber = 0;


    ID(): number {
        return this.idNumber++; // this was missing!
    }

    generateBooks(): void { // this missing on all!
        this.createBooks("Programming for dummies", "Drew Jocham", Category.Programming, false);
        this.createBooks("Love story I won't read", "Justin C", Category.Fiction, true);
        this.createBooks("Shooting for dummies", "Autin Powers", Category.nonFiction, true);
        this.createBooks("Master Management", "Dick Tracy", Category.nonFiction, false);
        this.createBooks("Advanced C++", "Kevin Jocham", Category.Programming, true);
        this.createBooks("How to eat pizza", "Ted", Category.nonFiction, true);
    }

    clearBooks(): void {
        this.books = []; // this was missing!
    }

    createBooks(name: string, author: string, category: Category, isAvailable: boolean): void {
        let newBook: IBook = { id: this.ID(), name: name, author: author, category: category, isAvailable: isAvailable };
        this.books.push(newBook); // this was missing!
    }

    getAvailbleFictionBooks(books): string[] {
        this.clearBooks(); // this was missing!
        this.generateBooks(); // this was missing!

        let availableBooks: Array<string> = [];

        for (let currentBook of books) {
            if (currentBook.isAvailable && currentBook.catergory === Category.Fiction) {
                availableBooks.push(currentBook);
            }
        }
        return availableBooks;
    }

    getAvailbleProgrammingBooks(books): string[] {
        this.clearBooks(); // this was missing!
        this.generateBooks(); // this was missing!

        let availableBooks: Array<string> = [];
        for (let currentBook of books) {
            if (currentBook.isAvailable && currentBook.catergory === Category.Programming) {
                availableBooks.push(currentBook);
            }
        }
        return availableBooks;
    }

    getAvailbleNonFictionBooks(books): string[] {

        this.clearBooks(); // this was missing!
        this.generateBooks(); // this was missing!

        let availableBooks: Array<string> = [];

        for (let currentBook of books) {
            if (currentBook.isAvailable && currentBook.catergory === Category.nonFiction) {
                availableBooks.push(currentBook);
            }
        }
        return availableBooks;
    }

    getAllBooks(): Array<IBook> {
        this.clearBooks();
        this.generateBooks();
        return this.books;
    }

    checkoutBook(id: number): boolean {
        let bookCollection = this.getAllBooks(); //error here!!
        for (let book of bookCollection) { // You had an exta ( here
            if (book.id === id && book.isAvailable) {
                let index = this.books.indexOf(book);
                // delete books[index]; WRONG!
                this.books.splice(index, 1); // CORRECT
                return true;
            }
        }
        return false;
    }

    checkInBook(book): void {
        this.books.push(book);
    }
}

试试上面的代码。希望它可以帮到你。