设想:
我正在使用 Visual Studio 2010,并且正在制作数据库示例应用程序。我必须将这些光谱文件存储在数据库中。这些光谱文件被加载到一个光谱数据对象中。我打算将这些对象存储在数据库中。对象具有许多不同的属性(int、double、string 等)。此外,该对象还包括一个 y 分量数据值数组。
我将我的数据库设计为由两个表组成(一个用于光谱数据对象属性,另一个用于双数组点。我创建了第一个表以自动增加 ID。
这是我整理的一些代码,它们基本上采用这些对象值并将它们分配给表参数。
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 并发。我假设的是,这种关系基本上是说“如果主键和外键匹配,那么它们是相关的”。