为什么TableAdapter.Insert方法不起作用? C# [英] why TableAdapter.Insert method doesn't work? C#

查看:81
本文介绍了为什么TableAdapter.Insert方法不起作用? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我正在使用Ying Bai - 用Visual C#.Net实用数据库编程构建我的应用程序(至少部分内容) )。



关于向db插入数据它说你可以使用TableAdapter.Insert方法直接向db添加数据。好吧,我做的就像示例说的那样,当我运行我的应用程序时,我可以看到添加的数据但是当我关闭应用程序并再次运行它时,数据就不存在了。



让我困惑的一件事是它在理论部分说你需要运行executeNonQuery命令让这个方法起作用,但是在示例代码中命令不存在,它假设工作!



请帮忙! :)



我错过了什么或误解了吗?



这里是我的代码:



这是我在tableAdapter中通过数据集构建的insert语句设计者,该查询称为InsertNewKombiQuery:

Hi everyone!

i''m using Ying Bai - Practical Database Programming with Visual C#.Net book to built my application(at least some parts of it).

about inserting data to db it says that you can use TableAdapter.Insert method to add data directly to the db. well I did it all like the example said and when I run my application I can see the added data but when I close the application and run it again then the data is not there.

One thing that makes me confused is that it says in the theory part that you need to run executeNonQuery command for this method to work, but in the example code the command is not there and it''s suppose to work!

please Help! :)

Am I missing something or missunderstanding?

here''s my code:

this is my insert statement built in the tableAdapter via dataset designer, the query is called InsertNewKombiQuery:

INSERT INTO KombiStateTbl(NumberPlate, LicenseExpiryDate, ControlDate, Documentos, FaróisLanternas, EstepeMacacoChaveDeRoda, Triângulo, Extintor, Acendedor, RodasCalota, Retrovisores, Párabrisa, BancosTapetes, Pneus, ParachoqueDianteiro, ParachoqueTraseiro, PortaDianteiraDireitaM, PortaDianteiraEsquerdaP, PortaLateral, LateralEsquerda, LateralEsquerdaTraseira, LateralDireitaTraseira, ParalamaTraseiroDireito, ParalamaTraseiroEsquerdo, Teto, PortaDoPortaMalas, PortaTraseiraDoMotor, Traseira, Dianteira, FaróisDianteiros, FaroisTraseiros, CurrentPic, CurrentPicPath) VALUES (@Param1,@Param2,@Param3, 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', NULL, NULL);





和我的表格中的代码:





and code in my form:

private void okButton_Click(object sender, EventArgs e)
int intInsert = 0;
            
intInsert = kombiStateTblTableAdapter.InsertNewKombiQuery(noPlateTextBox.Text,
                licenseDateTimePicker.Value.Date, controlDateTimePicker.Value.Date);
            
            if (intInsert != 0)
            {
                MessageBox.Show("The data insertion is successful", "Success", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                noPlateTextBox.Text = "";                
                okButton.Enabled = false;
                this.Close();
            }
            else
            {
                MessageBox.Show("The data insertion is failed", "Error", MessageBoxButtons.RetryCancel,
                    MessageBoxIcon.Error);
            }            
        }
        {

推荐答案

你还没有提到过在InsertNewKombiQuery方法中写的内容但你说作者解释了为dataAdapter类调用ExecuteNonQuery方法。



是的,为了插入数据库表,你需要对数据库本身执行数据操作在本地适配器类中,并不意味着您已成功将数据插入数据库。

You havent mentioned about what is written inside the method InsertNewKombiQuery but you said the Author explained about calling ExecuteNonQuery method to dataAdapter class.

Yes inorder insert into the database table you need to perform the action on the database itself having the data''s inside your local adapter class doesnt mean that you have inserted the data successfully to your database.
using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }



检查你的代码/类,找到执行实际数据库插入的上面一段代码。

你可以修改你的代码将insert语句作为查询字符串传递。


check your code/class for above piece of code which performs the actual database insert.
you may modify your code to pass the insert statement as the query string.


尝试调用表adpaters .Update( dataset 插入调用后的方法。这应该提交对数据库的更改。
Try calling the table adpaters .Update(dataset) method after the insert call. This should commit the changes to the database.


这篇关于为什么TableAdapter.Insert方法不起作用? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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