如何在数据库中同时正确更新2个表?

时间:2013-07-19 19:22:44

标签: c# database visual-studio-2010 database-design ado.net

情景:

我正在使用visual studio 2010,我正在制作数据库示例应用程序。我必须将这些光谱学文件存储在数据库中。这些光谱学文件被加载到一个spectradata对象中。我打算将这些对象存储在数据库中。对象有许多不同的属性(int,double,string等)。此外,该对象还包括一个y分量数据值数组。

我设计的数据库由两个表组成(一个用于spectradata对象属性,另一个表用于双数组点。我创建了第一个表来自动增加ID。

Database design http://i43.tinypic.com/dm63yc.jpg

以下是我放在一起的一些代码,它们基本上采用这些对象值并将它们分配给表参数。

SPCFile spc = new SPCFile();
TSpectraData spectra = new TSpectraData();
spectra = spc.LoadSPC(openSPCFile.FileName);

using(SqlConnection mySqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["DBLocal"].ConnectionString))
{
      SqlCommand command = mySqlConnect.CreateCommand();
      command.CommandText = "INSERT INTO Spectras VALUES"
                                     + "(@DateTime, "
                                     + "@Name, "
                                     + "@Version, "
                                     + "@SerialHighNumber, "
                                     + "@SerialLowNumber, "
                                     + "@Completed, "
                                     + "@SpectrometerID, "
                                     + "@GasCellID, "
                                     + "@Format, "
                                     + "@Apodization, "
                                     + "@PhaseApodization, "
                                     + "@Temperature, "
                                     + "@Pressure, "
                                     + "@NumScans, "
                                     + "@Resolution, "
                                     + "@Gain, "
                                     + "@PathLength, "
                                     + "@FirstPoint, "
                                     + "@LastPoint, "
                                     + "@MaxFrequency, "
                                     + "@MaxLocPoint, "
                                     + "@MinLocPoint, "
                                     + "@NumDataPoints, "
                                     + "@NumDataPhase, "
                                     + "@Step, "
                                     + "@IgramType)";

                //Adding parameters to command
                //Value for ID will be added automatically since it is set to auto increment

                command.Parameters.Add("@DateTime",         SqlDbType.DateTime, Int32.MaxValue);
                command.Parameters.Add("@Name",             SqlDbType.NVarChar, Int32.MaxValue);
                command.Parameters.Add("@Version",          SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@SerialHighNumber", SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@SerialLowNumber",  SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@Completed",        SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@SpectrometerID",   SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@GasCellID",        SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@Format",           SqlDbType.SmallInt, Int32.MaxValue);
                command.Parameters.Add("@Apodization",      SqlDbType.SmallInt, Int32.MaxValue);
                command.Parameters.Add("@PhaseApodization", SqlDbType.SmallInt, Int32.MaxValue);
                command.Parameters.Add("@Temperature",      SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@Pressure",         SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@NumScans",         SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@Resolution",       SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@Gain",             SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@PathLength",       SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@FirstPoint",       SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@LastPoint",        SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@MaxFrequency",     SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@MaxLocPoint",      SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@MinLocPoint",      SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@NumDataPoints",    SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@NumDataPhase",     SqlDbType.Int, Int32.MaxValue);
                command.Parameters.Add("@Step",             SqlDbType.Float, Int32.MaxValue);
                command.Parameters.Add("@IgramType",        SqlDbType.SmallInt, Int32.MaxValue);
                //Adding values to these parameters
                command.Parameters["@DateTime"].Value = DateTime.Now;
                command.Parameters["@Name"].Value = spectra.Info.Name;
                command.Parameters["@Version"].Value = spectra.Info.Version;
                command.Parameters["@SerialHighNumber"].Value = spectra.Info.SerialHighNumber;
                command.Parameters["@SerialLowNumber"].Value = spectra.Info.SerialLowNumber;
                command.Parameters["@Completed"].Value = spectra.Info.Completed;
                command.Parameters["@SpectrometerID"].Value = spectra.Info.SpectrometerID;
                command.Parameters["@GasCellID"].Value = spectra.Info.GasCellID;
                command.Parameters["@Format"].Value = (short)spectra.Info.Format;
                command.Parameters["@Apodization"].Value = (short)spectra.Info.Apodization;
                command.Parameters["@PhaseApodization"].Value = (short)spectra.Info.PhaseApodization;
                command.Parameters["@Temperature"].Value = spectra.Info.Temperature;
                command.Parameters["@Pressure"].Value = spectra.Info.Pressure;
                command.Parameters["@NumScans"].Value = spectra.Info.NumScans;
                command.Parameters["@Resolution"].Value = spectra.Info.Resolution;
                command.Parameters["@Gain"].Value = spectra.Info.Gain;
                command.Parameters["@PathLength"].Value = spectra.Info.PathLength;
                command.Parameters["@FirstPoint"].Value = spectra.Info.FirstPoint;
                command.Parameters["@LastPoint"].Value = spectra.Info.LastPoint;
                command.Parameters["@MaxFrequency"].Value = spectra.Info.MaxFrequency;
                command.Parameters["@MaxLocPoint"].Value = spectra.Info.MaxLocPoint;
                command.Parameters["@MinLocPoint"].Value = spectra.Info.MinLocPoint;
                command.Parameters["@NumDataPoints"].Value = spectra.Info.NumDataPoints;
                command.Parameters["@NumDataPhase"].Value = spectra.Info.NumDataPhase;
                command.Parameters["@Step"].Value = spectra.Info.Step;
                command.Parameters["@IgramType"].Value = (short)spectra.Info.IgramType;
                mySqlConnect.Open();
                command.ExecuteNonQuery();
                fileChosen = false;
                //Don't have to call connection close due tp using statment
                label1.Hide();
            }

由于ID是自动递增的,因此我不需要将它包含在VALUES语句中。我的过程的下一步是添加到数据点表。我计划重用命令对象并重新分配命令文本。我的问题是如何获取这个数据点数组并将它们分配到另一个表中,同时在两个表中保持ID并发。我假设,这种关系基本上是说“如果主键和外键匹配,那么它们是相关的”。

1 个答案:

答案 0 :(得分:1)

我在这里假设很多东西,但是,我希望给出这个伪代码。

首先,您可以使用查询SELECT SCOPE_IDENTITY()检索连接上最后插入的自动增量编号(IDENTITY)。

其次,知道这个数字你就可以轻松地准备一个循环来插入数据点,其中参数是用循环外的虚拟值创建的,并用数据点上循环内的有效值替换。

事务应该是一个好处,因为如果在数据点上循环时出现问题,则不需要清除已插入的数据

// Start a TransactionScope to handle all/or/nothing scenario
using(TransactionScope scope = new TransactionScope())
{
    using(SqlConnection mySqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["DBLocal"].ConnectionString))
    {
          SqlCommand command = mySqlConnect.CreateCommand();

          // Append the SELECT SCOPE_IDENTITY as a second command on this text.....
          command.CommandText = "INSERT INTO Spectras VALUES"
                                         + "(......)"
                                         + ";SELECT SCOPE_IDENTITY();"
          mySqlConnect.Open();

          // HERE YOU EXECUTE THE COMMAND AND GET BACK THE LAST IDENTITY USED 
          SqlDataReader reader = command.ExecuteReader();
          if (reader.HasRows)
          {
              reader.Read();
              int spectraID = Convert.ToInt32(reader[0]);
              // Ready to start the insert of points

              SqlCommand cmdPoints = new SqlCommand("INSERT INTO DataPoints " + 
                                      "VALUES (@ID, @IDX, @VAL)", mySqlConnect);
              cmdPoint.Parameters.AddWithValue("@ID",spectraID);
              cmdPoint.Parameters.AddWithValue("@IDX",0);
              cmdPoint.Parameters.AddWithValue("@VAL",0);

              foreach(DataPoint dp in DataPoints)
              {
                  cmdPoint.Parameters["@IDX"].Value = dp.Index);
                  cmdPoint.Parameters["@VAL"].Value = dp.Value);
                  cmdPoint.ExecuteNonQuery();
              }
        }      
    }
    scope.Complete(); // This will write everything on the database

}

如果由于异常而退出,则范围将无法完成,整个记录集将被丢弃

注意:您没有在INSERT INTO命令中指定列名。这意味着框架将尝试插入每个列,包括ID(IDENTITY)和第一个参数的值(日期时间?)。您应该在传递参数的情况下按照确切的顺序插入字段的名称。

command.CommandText = "INSERT INTO Spectras " 
                      + "(Name, Version, SerialHighNumber, .......)"
                      + "VALUES (@Name, @Version, @SerialHighNumber, ......)"
                      + ";SELECT SCOPE_IDENTITY();"