C#.NET4.0 TableAdapter.Update()不会插入新记录 [英] C# .NET4.0 TableAdapter.Update() won't insert new record

查看:172
本文介绍了C#.NET4.0 TableAdapter.Update()不会插入新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型应用程序,它由Visual Studio 2010中一个解决方案下的不同项目中的DAL,BLL和应用程序本身组成。

I have a small application which consists of a DAL, BLL and the Application itself all in different projects under one solution in Visual Studio 2010.

我的DAL正在使用一个xsd文件查询数据库,我的BLL有一些方法可以从DAL中获取信息并对其进行处理。

My DAL is using an xsd file to query the database and my BLL has methods to get the information from the DAL and do stuff with it.

当前,我正在尝试使用应用程序后面的代码以调用BLL中的方法,然后该方法尝试使用DAL的xsd文件中生成的Tableadapter.Update()方法插入记录。

Currently I am trying to insert a record using the code behind of the application to make a call to a method in the BLL which then attempts to insert the record by using the Tableadapter.Update() method generated in the xsd file of the DAL.

我已经可以毫无问题地选择和更新记录,但是我不能插入记录。

I can already select and update records with no problem, but I can't insert records.

据我所知,TableAdapter.Update()如果我向它提供新行,该方法应该知道插入一条新记录,但是它返回的值为0-意味着0行受到影响,因此它无法正常工作。

As far as I know the TableAdapter.Update() method should know to insert a new record if I provide it with a new row, however it is returning a value of 0 - meaning 0 rows were affected, so it isn't working.

我要插入的表称为tblRoles。

The table I am trying to insert into is called tblRoles.

它有一个'ID'co lumn,它是一个int,主键和标识列。它有一个使用nvarchar(50)的名称列,并有4个使用一种比特类型的 CanAdduser等列。

It has an 'ID' column, which is an int, primary key and identity column. It has a 'Name' column which takes nvarchar(50), and it has 4 'CanAdduser' etc columns which take a type of bit.

这是我的代码:

后面的APP代码:

protected void CreateRole(object sender, EventArgs e) {
    RolesBL roles = new RolesBL();

    roles.CreateRole(NewRoleName.Text);

    RolesGridView.DataBind();
}

BLL:

private tblRolesTableAdapter adapter = new tblRolesTableAdapter();

public bool CreateRole(string Name) {
    CMSDS.tblRolesDataTable roles = new CMSDS.tblRolesDataTable();
    CMSDS.tblRolesRow row = roles.NewtblRolesRow();

    row.Name = Name;
    row.CanAddUsers = false;
    row.CanEditUsers = false;
    row.CanSuspendUsers = false;
    row.CanChangeUserPasswords = false;

    int result = adapter.Update(row);

    if(result == 1) {
        return true;
    }

    return false;
}

根据我的理解,这应该在表中插入新行,但是 adapter.Update(row)继续返回0,我不知道为什么。

From what I understand this should insert a new row into the table, but adapter.Update(row) keeps on returning a 0 and I don't know why.

当我调试时可以看到所有行列均已分配了正确的值,并且不会引发任何错误。

When I debug I can see that all of the row columns have been assigned the correct values, and no errors are thrown.

任何帮助将不胜感激!

编辑:

我忘了提到在xsd中配置默认​​的Fill,GetData()查询时我确实确保文件自动生成了Insert,Update和Delete语句。

I forgot to mention that when I configured the default Fill,GetData() query in the xsd file I did make sure that it auto generated Insert, Update and Delete statements.

推荐答案

您尚未添加新的Role-

You haven't added the new Role-Row to the DataTable.

CMSDS.tblRolesDataTable roles = new CMSDS.tblRolesDataTable();
CMSDS.tblRolesRow row = roles.NewtblRolesRow();
row.Name = Name;
roles.AddtblRolesRow(row); 
int result = adapter.Update(roles); //the same as `adapter.Update(row)`

这篇关于C#.NET4.0 TableAdapter.Update()不会插入新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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