无法连续插入数据库

时间:2015-06-19 22:43:39

标签: c# asp.net scope-identity

我正在尝试连续2个SQL插入2个不同的表。

第一个表插入工作正常。它使用SELECT SCOPE_IDENTITY()来提取索引号,然后将其存储在变量Registree_Index中。这很好用。

然后我尝试将Registree_Index和其他一些变量插入到第二个表中。这根本不会写入第二个表。也没有错误消息。

最初我认为错误与重用旧查询和连接字符串变量有关,所以我创建了新的。这没有帮助。

有没有人有这个想法?代码如下......

private void WriteToDatabase()
{
    Guid newGuid = Guid.NewGuid();

    string yearstring = DateTime.Now.Year.ToString();
    string twodigityear = yearstring.Substring(yearstring.Length-2);
    string dateAndGuid = twodigityear + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-"  + DateTime.Now.Hour.ToString() + "-"  + DateTime.Now.Minute.ToString() + "-"  + DateTime.Now.Second.ToString() + "-" + newGuid;

    string connectionString = GetConnectionString();

    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = connectionString;
    connection.Open();

    string insertQuery = "INSERT INTO registrees (UIDindex, Submission_Number, Homecoming_Form, HC_form, NewRecord, First_Name, Last_Name, Billing_Phone, Addresses_Same, Email) VALUES (@UIDindex, @Submission_Number, @Homecoming_Form, @HC_form, @NewRecord, @First_Name, @Last_Name, @Billing_Phone, @Addresses_Same, @Email)               SELECT SCOPE_IDENTITY()";

    SqlCommand cmd = new SqlCommand(insertQuery, connection);

    cmd.Parameters.AddWithValue("@UIDindex", dateAndGuid);
    cmd.Parameters.AddWithValue("@Submission_Number", 1);
    cmd.Parameters.AddWithValue("@Homecoming_Form", 1);
    cmd.Parameters.AddWithValue("@HC_form", "platform");
    cmd.Parameters.AddWithValue("@NewRecord", 1);

    cmd.Parameters.AddWithValue("@First_Name", First_Name.Text);
    cmd.Parameters.AddWithValue("@Last_Name", Last_Name.Text);

    cmd.Parameters.AddWithValue("@Billing_Phone", Phone.Text);
    cmd.Parameters.AddWithValue("@Addresses_Same", 1); 
    cmd.Parameters.AddWithValue("@Email", Email.Text);

    ///get index from scope identity
    int Registree_Index = Convert.ToInt32(cmd.ExecuteScalar());



    ///SO FAR EVERYTHING WORKS GREAT! BUT THE REST OF THIS CODE FAILS SOMEHOW.
    connection.Close();
    connection = null;
    insertQuery = null;
    cmd = null;

    string connectionString2 = GetConnectionString();
    SqlConnection connection2 = new SqlConnection();
    connection2.ConnectionString = connectionString2;
    connection2.Open();

    string insertQuery2 = "INSERT INTO event_registration (Registree_Index, UIDindex, Submission_Number) VALUES (@Registree_Index, @UIDindex, @Submission_Number)";

    SqlCommand cmd2 = new SqlCommand(insertQuery2, connection2);

    cmd2.Parameters.AddWithValue("@Registree_Index", Registree_Index);
    cmd2.Parameters.AddWithValue("@UIDindex", dateAndGuid);
    cmd2.Parameters.AddWithValue("@Submission_Number", 1);

}

3 个答案:

答案 0 :(得分:4)

我无法看到您呼叫cmd2.ExecuteScalar()

的任何地方

答案 1 :(得分:1)

这个怎么样......

private void WriteToDatabase()
    {
        Guid newGuid = Guid.NewGuid();

        string yearstring = DateTime.Now.Year.ToString();
        string twodigityear = yearstring.Substring(yearstring.Length - 2);
        string dateAndGuid = twodigityear + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second.ToString() + "-" + newGuid;
        int Registree_Index;

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            connection.Open();

            string insertQuery = "INSERT INTO registrees (UIDindex, Submission_Number, Homecoming_Form, HC_form, NewRecord, First_Name, Last_Name, Billing_Phone, Addresses_Same, Email) VALUES (@UIDindex, @Submission_Number, @Homecoming_Form, @HC_form, @NewRecord, @First_Name, @Last_Name, @Billing_Phone, @Addresses_Same, @Email)               SELECT SCOPE_IDENTITY()";

            using (SqlCommand cmd = new SqlCommand(insertQuery, connection))
            {

                cmd.Parameters.AddWithValue("@UIDindex", dateAndGuid);
                cmd.Parameters.AddWithValue("@Submission_Number", 1);
                cmd.Parameters.AddWithValue("@Homecoming_Form", 1);
                cmd.Parameters.AddWithValue("@HC_form", "platform");
                cmd.Parameters.AddWithValue("@NewRecord", 1);

                cmd.Parameters.AddWithValue("@First_Name", First_Name.Text);
                cmd.Parameters.AddWithValue("@Last_Name", Last_Name.Text);

                cmd.Parameters.AddWithValue("@Billing_Phone", Phone.Text);
                cmd.Parameters.AddWithValue("@Addresses_Same", 1);
                cmd.Parameters.AddWithValue("@Email", Email.Text);

                ///get index from scope identity
                Registree_Index = Convert.ToInt32(cmd.ExecuteScalar());
            }

            string insertQuery2 = "INSERT INTO event_registration (Registree_Index, UIDindex, Submission_Number) VALUES (@Registree_Index, @UIDindex, @Submission_Number)";

            using (SqlCommand cmd = new SqlCommand(insertQuery2, connection))
            {
                cmd.Parameters.AddWithValue("@Registree_Index", Registree_Index);
                cmd.Parameters.AddWithValue("@UIDindex", dateAndGuid);
                cmd.Parameters.AddWithValue("@Submission_Number", 1);

                cmd.ExecuteNonQuery();
            }
        }

    }

答案 2 :(得分:0)

那是因为你永远不会执行cmd2。由于insertQuery2仅插入event_registration表,因此您可以通过调用cmd2来执行cmd2.ExecuteNonQuery()

在旁注中,您应该考虑使用using statement来确保在执行查询后关闭connectionconnection2。以下是使用using statement

的修改后的代码
private void WriteToDatabase()
{
    Guid newGuid = Guid.NewGuid();

    string yearstring = DateTime.Now.Year.ToString();
    string twodigityear = yearstring.Substring(yearstring.Length-2);
    string dateAndGuid = twodigityear + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-"  + DateTime.Now.Hour.ToString() + "-"  + DateTime.Now.Minute.ToString() + "-"  + DateTime.Now.Second.ToString() + "-" + newGuid;

    string connectionString = GetConnectionString();

    string insertQuery = "INSERT INTO registrees (UIDindex, Submission_Number, Homecoming_Form, HC_form, NewRecord, First_Name, Last_Name, Billing_Phone, Addresses_Same, Email) VALUES (@UIDindex, @Submission_Number, @Homecoming_Form, @HC_form, @NewRecord, @First_Name, @Last_Name, @Billing_Phone, @Addresses_Same, @Email)               SELECT SCOPE_IDENTITY()";
    int Registree_Index = 0;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand(insertQuery, connection))
        {
            cmd.Parameters.AddWithValue("@UIDindex", dateAndGuid);
            cmd.Parameters.AddWithValue("@Submission_Number", 1);
            cmd.Parameters.AddWithValue("@Homecoming_Form", 1);
            cmd.Parameters.AddWithValue("@HC_form", "platform");
            cmd.Parameters.AddWithValue("@NewRecord", 1);

            cmd.Parameters.AddWithValue("@First_Name", First_Name.Text);
            cmd.Parameters.AddWithValue("@Last_Name", Last_Name.Text);

            cmd.Parameters.AddWithValue("@Billing_Phone", Phone.Text);
            cmd.Parameters.AddWithValue("@Addresses_Same", 1); 
            cmd.Parameters.AddWithValue("@Email", Email.Text);

            connection.Open();

            ///get index from scope identity
            Registree_Index = Convert.ToInt32(cmd.ExecuteScalar());
        }
    }

    string connectionString2 = GetConnectionString();
    string insertQuery2 = "INSERT INTO event_registration (Registree_Index, UIDindex, Submission_Number) VALUES (@Registree_Index, @UIDindex, @Submission_Number)";

    using (SqlConnection connection2 = new SqlConnection(connectionString2))
    {
        using (SqlCommand cmd2 = new SqlCommand(insertQuery2, connection2))
        {
            cmd2.Parameters.AddWithValue("@Registree_Index", Registree_Index);
            cmd2.Parameters.AddWithValue("@UIDindex", dateAndGuid);
            cmd2.Parameters.AddWithValue("@Submission_Number", 1);

            connection2.Open();

            cmd2.ExecuteNonQuery();
        }
    }
}