如何从TextBox中将值(或更新)写回DataBase(通过DataBinding)? [英] How to write back value (or Update) to DataBase (through DataBinding) from TextBox ?

查看:168
本文介绍了如何从TextBox中将值(或更新)写回DataBase(通过DataBinding)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都请指导.....



windows应用程序(C#)

主题: TextBox DataBinding



问题:如何从TextBox中将值(或更新)写回DataBase(通过DataBinding)???

Anyone please guide.....

windows Application (C#)
Topic: TextBox DataBinding

Question: How to write back value (or Update) to DataBase (through DataBinding) from TextBox ???

推荐答案

我写了这段代码来解决你的问题。它非常简单。

我写信给Access数据库添加新行,因为我目前没有在我的机器上安装Sql Server。

所以只需用Sql替换OleDb在这段代码中。

更改你的连接字符串。

然后,用你的Update命令替换insert命令它会起作用。



I wrote this code to solve your problem. It is really simple.
I wrote to add new rows to Access Database as I currently don't have Sql Server installed on my machine.
So just replace OleDb with Sql in this code.
Change your connection String.
And, replace the insert command with your Update Command and it will work.

protected void btn1_Click(object sender, EventArgs e)
    {
        Label1.Visible = false; //Label1 is the label to display exception
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%\\UpdatefromTB.accdb;Persist Security Info=False;");
        string OleDbStatement = string.Empty;
        OleDbStatement = "insert into UpdatefromTB (TBValue) values (\"" + TextBox1.Text + "\")";

        try
        {
            connection.Open();

            OleDbCommand cmd = new OleDbCommand(OleDbStatement, connection);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            Label1.Visible = true;
            Label1.Text = msg;
        }
            
        finally
        {
            connection.Close();
        }
    }





请将此标记为答案,因为它是。



谢谢。



Mark this as Answer please because it is.

Thanks.


TextBox Binding:文本框中的更改值未在DataBase中更新 [ ^ ]


这篇关于如何从TextBox中将值(或更新)写回DataBase(通过DataBinding)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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