简体   繁体   English

将行插入数据库(C#,SQL Server CE)

[英]insert row into database (C#, SQL Server CE)

I am trying to add a row to a table in the database using the table adapter's update method. 我正在尝试使用表适配器的update方法向数据库中的表添加一行。 I am not getting any errors. 我没有任何错误。 It looks like it is working fine, but there is no actual insertion into the database. 看起来一切正常,但没有实际插入数据库。 However, if I try to insert the same row again I get a constraint exception saying that I am trying to insert a data that is already in the table. 但是,如果我尝试再次插入同一行,则会收到约束异常,表示我正在尝试插入表中已有的数据。 But in the end there is no data physically inserted into the table. 但是最后,实际上没有数据插入表中。 I tried to use table adapter's insert method but I got the same error. 我尝试使用表适配器的插入方法,但出现了相同的错误。

Where am I doing wrong? 我在哪里做错了?

Here is the code where insertion is made:(database name: pompadatabase , table name: pompa ) 这里是插入由代码:(数据库名: pompadatabase ,表名: pompa

pompadatabaseDataSet.pompaRow tmprow;
tmprow = pompadatabaseDataSet.pompa.NewpompaRow();
tmprow.isim = textBox5.Text;
tmprow.info = richTextBox1.Text;
tmprow.resim = bao;
tmprow.seri_id = Convert.ToInt32(sinifBox.SelectedValue.ToString());
this.pompadatabaseDataSet.pompa.Rows.Add(tmprow);
this.pompaTableAdapter.Update(this.pompadatabaseDataSet.pompa);

Where are you "physically" looking for the data ? 您在哪里“物理上”寻找数据? i mean, are you checking upon the db instance you have in your solution explorer or in the one inside the debug folder ? 我的意思是,您是要检查解决方案资源管理器中的数据库实例还是debug文件夹中的数据库实例? When you run your application, a copy of the SQL Server CE db file will be put inside that folder and the rows will be saved there. 当您运行应用程序时,SQL Server CE db文件的副本将放置在该文件夹中,并且行将保存在该文件夹中。 Each time you re-run your application, that file will be overwritten. 每次您重新运行应用程序时,该文件都会被覆盖。 Try to run your application from that folder, if everything goes well, that was the problem. 如果一切顺利,请尝试从该文件夹运行您的应用程序,这就是问题所在。 Well it could sound silly but it happened to me when i first started to work with DataSets and TableAdapters. 好吧,这听起来很愚蠢,但是当我第一次开始使用DataSet和TableAdapters时,它发生在我身上。

the transaction of pompaTableAdapter needs to b commited. 需要提交pompaTableAdapter的事务。 ie urtransaction.Commit(); 即urtransaction.Commit(); then look in Db ur data wud be saved. 然后在dbur中查找要保存的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM