多个“插入到”;同一事件下的操作

本文关键字:事件 操作 插入 插入到 多个 | 更新日期: 2023-09-27 18:11:34

伙计们,我到处找,但没有什么可以帮助我,所以我想是时候问了。在我写问题之前,我需要说我需要尽快解决它,因为这是一个我明天要交的项目,我已经在同一个主题上停留了很长时间,仍然在浪费时间。

好的,在这里;

我需要将一本书添加到图书馆系统,在第一阶段,我添加了只有"一个值"的标准图书功能(名称,页码,出版时间,publisherID等),但正如我想要的,书可能有多个作家和类别,这杀死了我,我仍然无法解决。我尝试将book添加到它的(books)表中,然后使用我从中获得的信息,我对(bookWriters)表进行了另一次插入操作。当我检查它时,编译器按顺序做一切,没有错误,但当我从SQL Server检查表时,没有任何东西。

这是我尝试做的;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace Project_
{
    public partial class addBook: Form
    {
        public addBook()
        {
            InitializeComponent();
        }
        public main refForm;
        int chosenWritersNumber; //how many writers have selected on listbox
        int[] writers= { }; // an array list that i keep writerIDs that comes from class
        int ind = 0;
        int insertedBookID; // to catch latest added book's ID
        int chosenWriterID; // writer that will be added
        private void bookAddingPreps()
        {
            chosenWritersNumber = lstWriters.SelectedItems.Count;
            Array.Resize<int>(ref writers, chosenWritersNumber );
            for (int i = 0; i < chosenWritersNumber ; i++)
            {
                writers[i] = ((X_Writers)lstWriters.SelectedItems[i]).XWriterID;
            }
        }
        private void addMainBookInfos()
        {
            SqlConnection con = new SqlConnection(Conn.Activated);
            SqlCommand com = new SqlCommand("AddBook", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@BookISBN", txtISBN.Text);            
            con.Close();
        }
        private void catchAddedBookID()
        {
            SqlConnection con = new SqlConnection(Conn.Activated);
            SqlCommand com = new SqlCommand("catchBookID", con);
            com.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    insertedBookID = dr.GetInt32(0);
                }
            }
            dr.Close();
            con.Close();
        }
        private void addWritersOfTheBook()
        {
            chosenWriterID = writers[ind];
            SqlConnection con = new SqlConnection(Conn.Activated);
            SqlCommand com = new SqlCommand("addBookWriters", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@BookID", insertedBookID);
            com.Parameters.AddWithValue("@WriterID", chosenWriterID);
            con.Close();
        }

在点击按钮时调用这些方法。您还可以看到存储过程名称,但当我检查它们都正确时,该页中一定有错误,我仍然看不到,但如果需要,我可以添加过程写的内容,但它们都经过测试并且似乎正常工作。

所以正如我所说,当我这样做的时候,作为ind = 0,应该添加一个作家,断点显示一切正常,编译器没有显示任何错误,但当我检查sql server表时,它是空的。

使用Visual Studio 2010 Ultimate和SQL Server 2008 Dev, c#编写。

谢谢

多个“插入到”;同一事件下的操作

忘记执行SqlCommand。调用command.ExecuteNonReader();执行它而不期望任何结果。参见:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

除此之外,不要忘记处理方法中获得的资源。比如:

private void addMainBookInfos() 
{ 
    using (SqlConnection con = new SqlConnection(Conn.Activated))
    using (SqlCommand com = new SqlCommand("AddBook", con))
    {
        com.CommandType = CommandType.StoredProcedure; 
        com.Parameters.AddWithValue("@BookISBN", txtISBN.Text);             
        com.ExecuteNonQuery()
        // close can be omitted since you are already using the 'using' statement which automatically closes the connection
        con.Close(); 
    }
}