我有一个方法,它从视图中获取检查值(可以有2个或更多,值名称为courseName)并将其存储在名为" SelectedCourses"的集合中。在这种情况下的问题是,我不知道是否应该将courseName值存储在RegisterTeacher表中,其中我存储所有其他值,或者存储在名为Course的单独表中。这些是2个表:
CREATE TABLE [dbo].[Courses] (
[courseName] NVARCHAR (MAX) NOT NULL,
[SNTeacher] INT NOT NULL,
[education] NVARCHAR (50) NOT NULL,
[ClassID] NVARCHAR (50) NULL,
[courseID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([courseID] ASC),
CONSTRAINT [FK_Courses_ToTable] FOREIGN KEY ([SNTeacher]) REFERENCES [dbo].[RegisterTeacher] ([SNTeacher])
);
这是表RegisterTeacher:
CREATE TABLE [dbo].[RegisterTeacher] (
[SNTeacher] INT NOT NULL,
[UserName] NVARCHAR (10) NOT NULL,
[pwd] INT NOT NULL,
[fullName] NVARCHAR (MAX) NOT NULL,
[CourseName] NVARCHAR (MAX) NOT NULL,
[courseID] INT NULL,
[education] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([SNTeacher] ASC),
CONSTRAINT [FK_RegisterTeacher_ToTable] FOREIGN KEY ([courseID]) REFERENCES [dbo].[Courses] ([courseID])
);
我的问题是如何从SelectedCourses集合中存储课程,如果我需要将它们存储在一个单独的表中,在这种情况下连接语法是否会起作用?任何帮助或建议都将受到赞赏。 这是将值存储到db:
的方法 public void SaveTeacher(object param)
{
string connectionString = null;
string sql = null;
connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\User\\Desktop\\BackupVersion\\LastAndFinalVersion\\DatabaseStudents.mdf;Integrated Security=True";
using (SqlConnection con = new SqlConnection(connectionString))
{
sql = "Insert into RegisterTeacher(SNTeacher,UserName,pwd,fullName,CourseName,education)values(@SNTeacher,@_UserName,@pwd,@fullName,@courseName,@education)";
con.Open();
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddWithValue("@SNTeacher", SNTeacher);
cmd.Parameters.AddWithValue("@UserName", _UserName);
cmd.Parameters.AddWithValue("@pwd", pwd);
cmd.Parameters.AddWithValue("@fullName", fullName);
SelectedCourses = new ObservableCollection<Cours>();
foreach (var item in Courses)
{
if (item.IsChecked)
{
SelectedCourses.Add(item);
}
cmd.Parameters.AddWithValue("@CourseName", courseName);
cmd.Parameters.AddWithValue("@education", education);
cmd.ExecuteNonQuery();
MessageBox.Show("Registration succesfull! ");
}
}
}
}