C# .NET4.0 TableAdapter.Update() 不会插入新记录

发布于 2024-11-24 07:38:08 字数 1497 浏览 1 评论 0原文

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

我的 DAL 使用 xsd 文件来查询数据库,我的 BLL 有方法从DAL 并用它做一些事情。

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

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

据我所知,如果我向 TableAdapter.Update() 方法提供新行,它应该知道插入新记录,但它返回值 0 - 意味着 0 行受到影响,因此它不起作用。

我尝试插入的表称为 tblRoles。

它有一个“ID”列,它是一个 int、主键和标识列。它有一个采用 nvarchar(50) 的“Name”列,还有 4 个采用位类型的“CanAdduser”等列。

这是我的代码:

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,我不知道为什么。

当我调试时,我可以看到所有行列都已分配了正确的值,并且没有抛出任何错误。

任何帮助将不胜感激!

编辑:

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

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.

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.

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.

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.

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

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.

Here is my code:

APP code behind:

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

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.

Any help would be appreciated!

Edit:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔姬 2024-12-01 07:38:08

您尚未将新的角色行添加到数据表中。

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)`

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)`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文