C#:SqlCe问题,无法更新到数据库,但可以读取数据 [英] C#: SqlCe Question, cannot update to database, but can read data

查看:496
本文介绍了C#:SqlCe问题,无法更新到数据库,但可以读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行程序时,此代码读取并更新表单,但在datagridview中编辑值并单击update时不会更新。

我正在跟踪视频教程系列

This code reads and updates the form fine when I run the program, but it does not update when I edit the values in the datagridview and click update.
I am following a video tutorial series and this code does work in the video to update the table.

问题:为什么在编辑datagridview中的值时不会更新?

Question: Why does it not update when I edit the values in the datagridview?

using System.Data.SqlServerCe;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private SqlCeConnection conn;       // Our Connection
        private SqlCeDataAdapter da;        // Data Adapter
        private DataSet ds;                 // Dataset
        private string sTable = "authors";  // Table Name

        public Form1()
        {
            InitializeComponent();
            InitData(); // Get the Data
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = sTable;              
        }

        public void InitData()
        {
            try
            {
                // Instantiate the Connection
                conn = new SqlCeConnection("Data Source=|DataDirectory|\\Database1.sdf");

                // Instantiate a new DataSet
                ds = new DataSet();

                // init SQLDataAdapter with select command and connection
                da = new SqlCeDataAdapter("SELECT id, name, email FROM " + sTable, conn);

                // Automatically generates insert, update and delete commands
                SqlCeCommandBuilder cmdBldr = new SqlCeCommandBuilder(da);

                // Fill in the dataset view
                da.Fill(ds, sTable);

            }
            catch (Exception excep)
            {
                MessageBox.Show("Exception: " + excep.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                da.Update(ds, sTable);
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.Message);
            }
        }
    }
}

程序编译,但更新命令无法通过点击button1更新数据库。

The program compiles, but the update command is not working to update the database on click of button1.

推荐答案

这可能看起来很傻但有您检查了您用于连接的用户名是否具有写入权限,而不仅仅是数据库中的只读?

This may seem silly but have you checked that the user name you are using to connect has write permissions and not just read-only on the database?

这篇关于C#:SqlCe问题,无法更新到数据库,但可以读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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