使用c#更新数据库中的表

时间:2014-07-17 21:07:03

标签: c# sql database

请让我们考虑下表

enter image description here

这是使用c#窗口形式完成的,也让我们考虑来自我的sql的以下图片

enter image description here

以及

enter image description here

问题是,当我们向form1添加一些数据时,它没有保存在数据库中,有基于此表单创建的c#代码,我的问题是我应该添加的内容,以便它也可以在sql中更新表数据库?这是sql代码

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void tableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tableBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.tatoDataSet1);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tatoDataSet1.Table' table. You can move, or remove it, as needed.
            this.tableTableAdapter.Fill(this.tatoDataSet1.Table);

        }

        private void tableDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.tableBindingSource.AddNew();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.tableBindingSource.RemoveCurrent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.tableBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.tatoDataSet1);
            MessageBox.Show("information is saved");
        }

        private void კოდიTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

提前致谢

1 个答案:

答案 0 :(得分:0)

进一步看,在提交更改之前,需要使用输入表单中指定的值将新行添加到表中。我添加了指导你的伪代码。我无法提供确切的代码,因为我不知道所有输入字段或数据库列的名称。我假设button3_click是你的保存按钮。

然后,在将更新应用到适配器之前单击“保存”按钮时,尝试将更改提交到数据集:

 private void button3_Click(object sender, EventArgs e)
            {
                //Create a new row for your table
                dim row as DataRow 
                row = this.tatoDataSet1.tables(0).NewRow 
                ' Add Values to Row here
                ' This is where you will set the values for each column in your rows to the input entered 
                ' Something like:
                dr.Column1 = txtInput1.text;
                ds.tables(0).rows.add(row)
                this.Validate();
                this.tableBindingSource.EndEdit();
                this.tatoDataSet1.AcceptChanges();
                this.tableAdapterManager.UpdateAll(this.tatoDataSet1);
                MessageBox.Show("information is saved");
            }