none
更新dataGridView中的数据到数据库文件中 RRS feed

  • 问题

  • 看MSDN示例也看了半天了,自己的就是不成功。以下是代码,挺简单的

    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.Data.OleDb;
    
    namespace StudentStatusManagementSystem.Admin
    {
        public partial class AdminManage : Form
        {
            private DataTable dt = new DataTable();
            private SqlDataAdapter sda = new SqlDataAdapter(); 
            public AdminManage()
            {
                InitializeComponent();
                
    
            }
    
            private void AdminManage_Load(object sender, EventArgs e)
            {
    
    
                SqlConnection conn = new SqlConnection(Properties.Settings.Default.DatabaseConnectionString);
                SqlCommand scd = new SqlCommand("select * from Student", conn);
                sda.SelectCommand = scd;
                sda.Fill(dt);
                dataGridView.DataSource = dt; 
            }
    
            SqlCommandBuilder scb = new SqlCommandBuilder();
    
            private void 保存SToolStripButton_Click(object sender, EventArgs e)
            {
                
                try
                {
                    this.Validate();
                    this.studentBindingSource.EndEdit();
                    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                    sda.Update(dt);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
                MessageBox.Show("更新成功!");
                //studentTableAdapter.Update(this.databaseDataSet.Student);
    
               
            }
    
        }
    }
    

    数据能正确读出来并显示出来,但是就是更新不了数据啊!代码运行也没问题,一切正常但是数据库里的数据就是不变!!!

    求高人解答!!!

    2014年6月11日 15:58

答案