如何在C#中的sql关系表中插入数据 [英] how to insert data in sql relational tables in c#

查看:147
本文介绍了如何在C#中的sql关系表中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我需要帮助,我在彼此相关的sql数据库中有两个表,因此是相关的.这两个表是成员和销售额.成员是父表,而sales是子表.成员位于详细信息视图中,而销售位于datagridview中.
我想在销售表中进行更新,但与成员表无关.
销售更新的代码是:

Hello i need help i have two tables in an sql database which is related to each other thus is related. the two tables are member and sales. member is the parent table and sales is the child table. member is in the details view and sales is in datagridview.
i want to update in the sales table but is not relating to member table.
the code for sales update is :

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;
using System.IO;
using System.Diagnostics;

namespace EMYS
{
    public partial class Payment : Form
    {
        DataTable dt = new DataTable();
        SqlConnection con;
        SqlDataAdapter adap;
        DataSet ds;
        SqlCommandBuilder cmdbl;
        public Payment()
        {
            InitializeComponent();
            loadall();
        }

        private void teacherBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.teacherBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.eMYSDataSet4);

        }

        private void teacherBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
        {
            this.Validate();
            this.teacherBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.eMYSDataSet4);
            this.titheBindingSource.EndEdit();
            this.titheTableAdapter.Update(this.eMYSDataSet4);
        }

        public void loadall()
        {
            // TODO: This line of code loads data into the 'eMYSDataSet4.Tithe' table. You can move, or remove it, as needed.
            this.titheTableAdapter.Fill(this.eMYSDataSet4.Tithe);
            // TODO: This line of code loads data into the 'eMYSDataSet4.Teacher' table. You can move, or remove it, as needed.
            this.teacherTableAdapter.Fill(this.eMYSDataSet4.Teacher);

            try
            {
                con = new SqlConnection();
                con.ConnectionString = @"Data Source=BROTHER-PC;Initial Catalog=EMYS;Integrated Security=True";
                con.Open();
                adap = new SqlDataAdapter("SELECT TitheId, Month, [2013], [2014], [2015], [2016], [2017], [2018], [2019], [2020], Id FROM dbo.Tithe",con);
                ds = new System.Data.DataSet();
                adap.Fill(ds, "Tithe");
                titheDataGridView.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Payment_Load(object sender, EventArgs e)
        {
            loadall();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                cmdbl = new SqlCommandBuilder(adap);
                adap.Update(ds, "Tithe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string constring = @"Data Source=BROTHER-PC;Initial Catalog=EMYS;Integrated Security=True";
            SqlConnection conDatabase = new SqlConnection(constring);
            SqlCommand cmdDatabase = new SqlCommand("select *from Tithe;", conDatabase);
           // SqlCommand cmdDatabase = new SqlCommand("select *from Teacher;", conDatabase);
            try
            {
                loadall();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
    }
}





其中button1是更新按钮.
成员表和销售表具有相同的形式.
问题是更新表成员之后与销售表无关.请帮助





Where button1 is the update button.
member table and sales table are on the same form.
the problem is that after the update table member is not relating to sales table. PLEASE help

推荐答案

您好,DataAdapter仅更新一个表.请检查以下链接:
http://stackoverflow.com/questions/5889102/ado-net-updating-multiple-datatables [^ ]
Hello friend, DataAdapters update only a single table. Please check the below link:
http://stackoverflow.com/questions/5889102/ado-net-updating-multiple-datatables[^]


这篇关于如何在C#中的sql关系表中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆