【问题标题】:How to save data or value on textbox to SQL Server for selected table如何将文本框中的数据或值保存到选定表的 SQL Server
【发布时间】:2020-04-04 08:33:08
【问题描述】:

这是单击 from3 按钮时从表单 1 传递的数据:

string DataT1, Table2, T3;
DataTable DT5,DT;

public Form3(DataTable DT4,string DT1, string T2,string T31)
{
            DataT1 = DT1;
            Table2 = T2;
            T3 = T31;
            DT5 = DT4;
            InitializeComponent();
            loaddata();
}

datagridview 填充数据时文本框自动填充

 private void FillData(DataTable DataT)
    {
        TextBox TB1;
        int y = 12;
        foreach (DataColumn DC in DataT.Columns)
        {
            TB1 = TB(0, y);
            TB1.DataBindings.Add(new Binding("Text".ToString(), DataT, DC.ColumnName));
            panel1.Controls.Add(TB1);
            y += 23;
        }
    }

        //the textbox object
        private TextBox TB(int x,int y)
        {
            TextBox TB1 = new TextBox 
            { 
                Text = "",
                Size = new Size(150,50),
                Location=new Point(x,y)
            };
            return TB1;
        }

下面是我想在单击保存按钮时将文本框数据或值添加到 SQL Server 的代码:

private void loaddata()
    {
        DataTable DT1 = new DataTable();
        SqlConnection SC = new SqlConnection(DataT1);
        SqlDataAdapter SDA = new SqlDataAdapter(Table2, SC);
        SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
        SDA.Fill(DT1);
        DT = DT1;
        dataGridView1.DataSource = DT;
        //the code to save and update data on textbox
        FillData((DataTable)dataGridView1.DataSource);
    }
    private void button1_Click(object sender, EventArgs e)
    {

        int j = 0;
        string[] Text = null;
        foreach (TextBox TB in panel1.Controls)
        {
            j++;
            Text[j].Insert(j, TB.Text.ToString());//this way i create a string[] to save each textbox value
        }
        /*how the code to save programmatically added textbox value to sql
         * database follow by each textbox to each columns in database 
         * table and another sql command to insert new record*/
    }

这是form1 SQL Server连接字符串

//this is the form one, the database and table is choose by combobox and fill it to datagridview
        string T1, T2, T3;
        DataTable Data2;

        private void LoadData()
        {
            T1 = "Server=localhost;Initial Catalog=" + comboBox2.SelectedItem + ";Integrated Security=SSPI;";
            T2 = "Select * from " + comboBox3.SelectedItem;
            T3 = comboBox3.SelectedItem.ToString();
            SqlConnection SCConnect = new SqlConnection(T1);
            SCConnect.Open();
            StringBuilder SBBuilder = new StringBuilder(T2);
            SqlDataAdapter SDA = new SqlDataAdapter(SBBuilder.ToString(),SCConnect);
            SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
            DataTable DT = new DataTable();
            SDA.Fill(DT);
            dataGridView1.DataSource = DT;
            Data2 = DT;
            filltexttotextbox((DataTable)dataGridView1.DataSource);
        }

当按钮点击时,它将打开并将数据传递给form3:

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 F3 = new Form3(Data2, T1, T2,T3);
            F3.ShowDialog();
        }

向数据库添加新记录

enter image description here

更新记录到数据库

enter image description here

【问题讨论】:

  • 您的意思是要将文本框列中的所有文本框值保存到 SQL 服务器?每个文本框都有自己的记录?
  • @TấnNguyên 不,因为在表单加载后,datagridview 将自动填充数据,并且文本框将以编程方式添加到 panel1,我想要的代码是在文本框加载并填写 panel1 之后,当我在文本框中键入内容并单击按钮,它将通过在表上插入新行或更新将文本框上的文本或值保存到 sql 数据库。
  • @1878alian我知道,但是如果您更改了多个文本框上的文本,然后单击按钮保存,您需要什么?保存所有更改?我的观点是你应该再实现一个按钮列,如果它被点击,触发事件并在你的数据库上更新插入。全部解决
  • @TấnNguyên 现在我已经可以在文本框中获取值了,但是在 sql 命令中,我如何更新我在每个文本框中更改的数据以保存在 sql 数据库中,因为单击按钮时,它将将我在文本框上所做的更改保存到 sql server。以及如何将新记录插入数据库表列的sql命令将每个文本框跟在每个列中?TQ
  • 要触发每一行的更改,您有 3 个选项: TextChanged 事件将在插入或删除的每个字母上触发,所以这很糟糕。当指针离开焦点时会触发离开事件,这看起来不错。第三,实现一个按钮列,当按钮被点击时会触发事件。最后两个选项很好,因为可以逐行插入新记录,不需要button1_Click

标签: c# sql-server winforms textbox


【解决方案1】:

我找到了处理基本上不会触发更改事件的更改的最佳方法。 只需将加载的保存为 OriginDataTable 并使用 EditorDataTable 作为编辑器。

为了快速实施。我将直接使用 DataGridView 作为编辑器,而无需在您的代码中传递给 panel1。

    DataTable OriginalDataTable = new DataTable();
    DataTable EditorDataTable = new DataTable();

    public MainForm()
    {
        InitializeComponent();

        EditorDataTable.Columns.Add("Col1");
        EditorDataTable.Columns.Add("Col2");
        EditorDataTable.Columns.Add("Col3");

        EditorDataTable.Rows.Add("my-col-1-row-1", "my-col-2-row-1", "my-col-3-row-1");
        EditorDataTable.Rows.Add("my-col-1-row-2", "my-col-2-row-2", "my-col-3-row-2");
        EditorDataTable.Rows.Add("my-col-1-row-3", "my-col-2-row-3", "my-col-3-row-3");

        dataGridView.DataSource = EditorDataTable;
        OriginalDataTable = EditorDataTable.Copy(); // Save loaded datatable
    }

那我就用origin和editor的比较

    private IEnumerable<DataRow> ListOutDataRowChanged(DataTable originDt, DataTable changeDt)
    {
        for (int i = 0; i < originDt.Rows.Count; i++)
        {
            if (string.Join(";",originDt.Rows[i].ItemArray) != string.Join(";", changeDt.Rows[i].ItemArray))
                yield return originDt.Rows[i];
        }
    }

保存按钮是

    private void btnSave_Click(object sender, EventArgs e)
    {
        var list = ListOutDataRowChanged(OriginalDataTable, EditorDataTable);
        MessageBox.Show("List changes count: " + list.Count());
        // Insert this list to SQL here
    }

这种实现的好处是:

  • 我们可以处理何时发送到 SQL
  • 编辑后恢复不计为更改
  • 无需触发触发事件

回滚

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2013-09-01
    • 2012-09-23
    相关资源
    最近更新 更多