0

在我的应用程序中,用户将插入一本书。例如 someBook 将插入 3 个副本。Table1.BookID = 1,Table1.Copy = 3,然后在另一个表中,这 3 本书将有它们的主键,因此它将是 Table2.AccessionID = 1,2,3 Table2.BookID = 1,1,1。

这是我目前正在做的,但正如 Aaron Bertrand 所说的那样,这是一种不好的做法。

int BookTitlesID;
public void addBookTitle()
{
  int copy = int.Parse(textBox2.Text);
  try
  {
    using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
    {
      myDatabaseConnection.Open();

      using (SqlCommand mySqlCommand1 = new SqlCommand("INSERT INTO BookTitles(BookTitle, Copies) Values(@BookTitle,  @Copies)", myDatabaseConnection))
      {
        mySqlCommand1.Parameters.AddWithValue("@BookTitle", BookTitletextBox.Text);
        mySqlCommand1.Parameters.AddWithValue("@Copies", copy);
        mySqlCommand1.ExecuteNonQuery();
      }
    }
  }
  catch (Exception Ex)
  {
    MessageBox.Show(Ex.Message, "Exception");
  }
}

public void addBook()
{
  int copy = int.Parse(textBox2.Text);
  try
  {
    for (int x = 0; x < copy; x++)
    {
      using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
      {
        myDatabaseConnection.Open();
        using (SqlCommand mySqlCommand1 = new SqlCommand("INSERT INTO book(BookTitleID) Values(@BookTitleID)", myDatabaseConnection))
        {
          mySqlCommand1.Parameters.AddWithValue("@BookTitleID", BookTitlesID);
          mySqlCommand1.ExecuteNonQuery();
        }
      }
    }
  }
  catch (Exception Ex)
  {
    MessageBox.Show(Ex.Message, "Exception");
  }
}

private void Form_Load(object sender, EventArgs e)
{
  using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
  {
    myDatabaseConnection.Open();
    using (SqlCommand mySqlCommand1 = new SqlCommand("SELECT TOP 1 BookTitleID + 1 FROM BookTitles ORDER BY BookTitleID DESC", myDatabaseConnection))
    {
      string x = mySqlCommand1.ExecuteScalar().ToString();
      BookTitlesID = Convert.ToInt32(x);
    }
  }
}

private void button1_Click(object sender, EventArgs e)
{
  addBookTitle();
  addBook();
}
4

1 回答 1

5

简化。最值得注意的是,正如您在其他相关问题中所讨论的那样,出去看看电流MAX是什么,加 1 并假设这将是生成的下一个身份值,绝对没有安全性。如果您在插入后检索它,则只能依赖该数字,最可靠的方法是使用SCOPE_IDENTITY()(或者,对于多行INSERT,一个OUTPUT子句)。

CREATE PROCEDURE dbo.InsertBook
  @BookTitle NVARCHAR(256),
  @Copies    INT
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @BookTitleID INT;

  INSERT dbo.BookTitles(BookTitle, Copies) SELECT @BookTitle, @Copies;

  SELECT @BookTitleID = SCOPE_IDENTITY();

  INSERT dbo.Books(BookTitleID) SELECT @BookTitleID
  FROM (SELECT TOP (@Copies) rn = ROW_NUMBER() OVER (ORDER BY [object_id])
        FROM sys.all_objects ORDER BY [object_id]) AS y;
END
GO

现在您可以真正简化您的 C# 代码(我不是 C# 人,所以这是否是最好的方法,或者它是否可以编译超出了我的范围,但希望您可以自己解决)。

public void addBook()
{
  try
  {
    int Copies    = int.Parse(textBox2.Text);
    string BTitle = BookTitletextBox.Text
    using (SqlConnection conn = new SqlConnection ...blah blah...)
    {
      conn.Open();

      using (SqlCommand cmd = new SqlCommand("EXEC dbo.InsertBook", conn))
      {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@BookTitle", BTitle);
        cmd.Parameters.AddWithValue("@Copies",    Copies);
        cmd.ExecuteNonQuery();
      }
    }
  }
  catch (Exception Ex)
  {
    MessageBox.Show(Ex.Message, "Exception");
  }
}

private void button1_Click(object sender, EventArgs e)
{
  addBook();
}
于 2013-08-12T19:55:43.817 回答