C# 使用数据集和数据适配器的.net framework SQL server CRUD
我在数据库中有以下两个表: 表语言的父级电影(id、标题、年份) 语言(电影id,语言)--电影id引用电影(id)。 我编写了以下代码,但我仍然需要执行更新/删除部分,并在子表中添加一条新记录 -当我从子项(语言)中选择一条记录时,我必须删除并更新一条记录 -当我从父表(电影)中选择一条记录时,我必须在子表中添加一条新记录C# 使用数据集和数据适配器的.net framework SQL server CRUD,c#,sql,.net,visual-studio,.net-framework-version,C#,Sql,.net,Visual Studio,.net Framework Version,我在数据库中有以下两个表: 表语言的父级电影(id、标题、年份) 语言(电影id,语言)--电影id引用电影(id)。 我编写了以下代码,但我仍然需要执行更新/删除部分,并在子表中添加一条新记录 -当我从子项(语言)中选择一条记录时,我必须删除并更新一条记录 -当我从父表(电影)中选择一条记录时,我必须在子表中添加一条新记录 using System; using System.Collections.Generic; using System.ComponentModel; using Sys
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace lab1_v2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connectionString = "Server=DESKTOP-T33VBF8;Database=lab1SGBD_v6;Integrated Security=true";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{//Open connection
connection.Open();
MessageBox.Show("State of the connection: " + connection.State.ToString());
//Create the data set
DataSet dataset = new DataSet();
//Create the two SQL Data Adapters for parent and child tables.
SqlDataAdapter parentAdapter = new SqlDataAdapter("SELECT * FROM Movies;", connection);
SqlDataAdapter childAdapter = new SqlDataAdapter("SELECT * FROM Languages;", connection);
//Create and populate the parent DataTable and the child DataTable
parentAdapter.Fill(dataset, "Movies");
childAdapter.Fill(dataset, "Languages");
//Create the two BindingSources for parent and child DataTable
BindingSource parentBS = new BindingSource();
BindingSource childBS = new BindingSource();
//Show all the records from parent DataTable in dataGridViewParent
parentBS.DataSource = dataset.Tables["Movies"];
dataGridViewParent.DataSource = parentBS;
//Create and add in the DataSet the DataRelation between parent DataTable and child DataTable
DataColumn parentPK = dataset.Tables["Movies"].Columns["id"];
DataColumn childFK = dataset.Tables["Languages"].Columns["movie_id"];
DataRelation relation = new DataRelation("fk_parent_child", parentPK, childFK);
dataset.Relations.Add(relation);
//Show in dataGriedView the child records which are from the parent record selected
childBS.DataSource = parentBS;
childBS.DataMember = "fk_parent_child";
dataGridViewChild.DataSource = childBS;
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
你让你的生活变得如此困难和复杂。VisualStudio可以在几秒钟内写出您在那里写的每一行,甚至更多
- 开始一个新项目(如果你不喜欢我推荐的项目,你可以把它扔掉,但你可能会)
- 向项目中添加新的数据集类型的文件并将其打开
- 将出现一个空白的灰色曲面,单击鼠标右键并选择“添加TableAdapter”
- 按照向导,输入连接字符串,选择SQL语句,下载行,键入
,命名并保存SELECT*FROM movies
- 以同样的方式添加另一个表适配器,但用于语言(我个人认为您的关系不正确,因为一种语言有许多电影,而一部电影有许多语言,所以您应该将它们作为父表和一个MovieLanguage表来拆分,但嘿……现在让我们按照您的方式进行)
- 如果在数据库中定义了一个关系,则会出现一行,表示一个DataRelation。如果数据库中没有设置外键,请在表中movie.id行的淡灰色左侧边距上单击一次,使整行变为蓝色(不仅仅是单词),然后在同一位置单击保持(边距)然后开始拖动到languages.movie_id-拖动时会出现一条线,这样就可以在两列之间绘制连接。当你放手时,会出现一个窗口。检查各列是否正确(父列是行的起点,子列是行的终点。如果错误,请在另一个方向上再次行或编辑此对话框设置)
- 切换到窗体设计器
- 打开数据源工具面板(查看菜单,其他窗口)
- 将“电影”节点从数据源中拖出,并将其放到窗体上
- 展开数据源中的“电影”节点,然后将其下的“语言”节点从“数据源”窗口拖到窗体上
- 不要将作为父节点的语言节点拖出“数据源”窗口
- 检查您的languagesbindingsource是否有一个.DataSource of moviesbindingsource和一个DataMember设置为表之间关系的名称(通过右键单击并显示关系名称或获取其属性,可以在数据集中看到关系名称)。如果您的语言绑定源将数据源设置为数据集的名称,则将错误的语言节点从数据源中拖出。在属性网格中手动设置正确的设置,或者删除表单中的所有内容,然后再次执行,确保将电影的子语言从数据源中拖出
- 运行程序
SELECT*FROM movies WHERE title like@title
,将其称为FillByTitle,并在代码中使用,如moviesTableAdapter.FillByTitle(someDataSet.Movies,“Jaws%”
请记住,您也必须填写所有要自己使用的相关行。因此,我很少让tableadapter中的第一个查询成为简单的“select*from”-我总是在primarykeyid=@id中添加一个
,然后我可以循环浏览填充每个语言id的电影,或者如果性能是一个问题,我将向语言添加另一个查询,如SELECT*FROM Languages WHERE id IN(从电影中选择不同的languageid,如@title)
所以我可以用Jaws%填充电影,然后用Jaws%填充语言我仍然需要做以下事情
?我不知道删除/更新时从何处开始,如何开始