如何使用绑定导航器插入数据(帮帮我) [英] how to insert data using binding navigator(help me)

查看:69
本文介绍了如何使用绑定导航器插入数据(帮帮我)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用绑定导航器和绑定源来插入数据并从数据库中删除数据但是,它没有插入和删除....如果我删除它删除的数据但是数据库没有变化,如果添加它但是没有数据库更新...





i have used binding navigator and binding source to insert data and delete data from database but, its not inserting and deleting.... if i delete the data its deleting but there is no change in database and if add its adding but no update in database ...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace datanavi
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;

        // my table name is dn
        // It has two fields name,age
        // two textbox i m having in form 

        public Form1()
        {
            InitializeComponent();
        }

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

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            //this code is not working while inserting
           con.Open();
            cmd = new SqlCommand("insert into table dn values '" + textBox1.Text + "','" + textBox2.Text + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

//i used bindingsource and bindingnavigator control to interact with Database table

推荐答案

因为你直接从控件中提取数据我假设你不熟悉 BindingNavigator.BindingSource [ ^ ]属性。



看看 Depends4Net - Part 2 [ ^ ]关于如何绑定到自定义元素的示例 - 这是您通常需要的操作当您使用SqlCommand和SqlDataReader访问数据库时,请注意。



您也可以切换到实体框架,如果您这样做,您可以查看: WinForms中的实体框架 [ ^ ]



Best问候

Espen Harlinn
Since you are pulling data directly from the controls I assume you are not familiar with the BindingNavigator.BindingSource[^] property.

Have a look at Depends4Net - Part 2[^] for an example on how to bind to "custom" elements - which is what you usually have to operate on when you are using SqlCommand and SqlDataReader to access the database.

You can also switch to the Entity Framework, and if you do then you can have a look at:Entity Framework in WinForms[^]

Best regards
Espen Harlinn


这篇关于如何使用绑定导航器插入数据(帮帮我)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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