如何将值插入两个表?

时间:2014-02-07 18:49:28

标签: c# winforms insert

我有一个medicalcentre和windowsadmin表,它们使用mcID相互关联。我在表单中有4个输入。输入的两个输入(即输入的mcType和mcCentre值)应插入到medicalcentre表中,输入的winUsername和winPassword值中的另外两个输入中的两个应该转到windowsadmin表。

这意味着我想要的是,我输入了mcType,mcCentre,winUsername和winPassword的值。单击register时,mcCentre和mcType将插入到medicalcentre表中并自动递增一个mcID,mcID将与输入的winUsername和winPassword一起插入到窗口管理表中。怎么做?我的代码如下。我只知道如何插入仅在medicalcentre表中的mcType和mcCentre。

我的注册医疗中心和WindowsAdmin帐户表单

单选按钮名称 - 医院:rbHopistal

单选按钮名称 - 诊所:rbClinic

文本框名称 - mcCentre:txtmcCentre

文本框名称 - winUsername:txtwinUsername

文本框名称 - winPassword:txtwinPassword

My Register MedicalCentre and WindowsAdmin account form

医疗中心和WINDOWSADMIN表 MEDICAL CENTRE AND WINDOWSADMIN table

错误尝试关注@Sergey Berezovskiy代码。必须声明标量变量“@mcID”。

Must declare the scalar variable "@mcID

我的表关系   My tables Relationship

    private int AddMedicalCentreRecord()
            {
                int result = 0;

                string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;

                SqlConnection myConnect = new SqlConnection(strConnectionString);

                String strCommandText = "INSERT MEDICALCENTRE(mcType, mcCentre) "
                    + " VALUES (@NewmcType,@NewmcCentre)";

                SqlCommand updateCmd = new SqlCommand(strCommandText, myConnect);

                if (rbHopistal.Checked)
                    updateCmd.Parameters.AddWithValue("@NewmcType", "Hospital");
                else
                    updateCmd.Parameters.AddWithValue("@NewmcType", "Clinic");
                updateCmd.Parameters.AddWithValue("@NewmcCentre", txtmcCentre.Text);

                myConnect.Open();

                result = updateCmd.ExecuteNonQuery();

                // STEP 5: Close
                myConnect.Close();
                return result;
            }

错误尝试关注@Sergey Berezovskiy代码。必须声明标量变量“@mcID”。

private int AddMedicalCentreRecord()
    {
        int result = 0;

        string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(strConnectionString))
        {
            conn.Open();
            using (SqlTransaction transaction = conn.BeginTransaction())
            {

                string insertCentreQuery =
                    @"INSERT MEDICALCENTRE (mcType, mcCentre) 
          VALUES (@NewmcType, @NewmcCentre)
          SELECT SCOPE_IDENTITY()";

                SqlCommand insertCentreCmd =
                    new SqlCommand(insertCentreQuery, conn, transaction);

                if (rbHopistal.Checked)
                    insertCentreCmd.Parameters.AddWithValue("@NewmcType", "Hospital");
                else
                    insertCentreCmd.Parameters.AddWithValue("@NewmcType", "Clinic");
                insertCentreCmd.Parameters.AddWithValue("@NewmcCentre", txtmcCentre.Text);

                int mcID = Convert.ToInt32(insertCentreCmd.ExecuteScalar());  

                string insertUserQuery =
                    @"INSERT WINDOWSADMIN (winUsername, winPassword, mcID)
          VALUES (@userName, @password, @mcID)";
                SqlCommand insertUserCmd =

                    new SqlCommand(insertUserQuery, conn, transaction);

                insertUserCmd.Parameters.AddWithValue("@userName", txtwinUsername.Text);
                insertUserCmd.Parameters.AddWithValue("@password", txtwinPassword.Text);

                insertUserCmd.ExecuteNonQuery();

                transaction.Commit();

                //conn.Open();

                //result = insertUserCmd.ExecuteNonQuery();

                conn.Close();
                return result;
            }
        }         


    }

1 个答案:

答案 0 :(得分:1)

您需要使用transaction - 开始事务,然后将数据插入到medicalcentre表中并获取mcID。之后,将数据插入到windowsadmin表并提交事务:

using(SqlConnection conn = new SqlConnection(strConnectionString))
{
    conn.Open();
    using(SqlTransaction transaction = conn.BeginTransaction())
    {
        // execute first command
        string insertCentreQuery = 
            @"INSERT MEDICALCENTRE (mcType, mcCentre) 
              VALUES (@NewmcType, @NewmcCentre)
              SELECT SCOPE_IDENTITY()";

        SqlCommand insertCentreCmd = 
            new SqlCommand(insertCentreQuery, conn, transaction);
        // ...provide command parameters
        int mcID = Convert.ToInt32(insertCentreCmd.ExecuteScalar());         

        // execute second command
        string insertUserQuery =
            @"INSERT WINDOWSADMIN (winUsername, winPassword, mcID)
              VALUES (@userName, @password, @mcID)";
        SqlCommand insertUserCmd = 
            new SqlCommand(insertUserQuery, conn, transaction);
        // ...provide command parameters
        insertUserCmd.ExecuteNonQuery();

        transaction.Commit();
    }   
}