问题描述
当我运行程序时,此代码读取并更新表单,但在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问题,无法更新到数据库,但可以读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!