C#: SqlCe Question, cannot update to database, but can read data

此生再无相见时 提交于 2019-12-11 17:17:11

问题


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.

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);
            }
        }
    }
}

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


回答1:


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?



来源:https://stackoverflow.com/questions/6479816/c-sqlce-question-cannot-update-to-database-but-can-read-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!