[英]c# using a binding source to add row to an sql table with incremental identity field
I have a program I created in Visual studio. 我有一个在Visual Studio中创建的程序。 The program is basically a place for everyone to store passwords for company and external accounts.
该程序基本上是每个人都可以存储公司和外部帐户密码的地方。 I want to further this application by automatically creating the company accounts when I create a new user.
我想通过在创建新用户时自动创建公司帐户来扩展此应用程序。 I approached this by using the binding source.
我通过使用绑定源来解决这个问题。 I can get the row into the database but it doesn't use the sql supplied auto increment.
我可以将行放入数据库,但不使用sql提供的自动增量。 I will post the code but I am trying to figure out if I went about this the wrong way.
我将发布代码,但是我试图弄清楚我是否以错误的方式进行操作。 I am not 100% familiar with how the connector and classes that visual studio create when you connect the solution to the database.
当您将解决方案连接到数据库时,我不是100%熟悉Visual Studio如何创建连接器和类。 I am not looking for code to help me do this I am looking for explanations and guidance.
我不是在寻找帮助我做到这一点的代码,而是在寻找解释和指导。 If responding with code please help me understand by explaining the code.
如果回复代码,请通过解释代码来帮助我理解。
DataSet.AccountsRow newdomainuserrow = DBDataSet.Accounts.NewAccountsRow();
newdomainuserrow.USer = userIDTextBox.Text.ToString();
newdomainuserrow.UserName = userIDTextBox.Text.ToString();
System.DateTime moment = new DateTime();
newdomainuserrow.Password = moment.Year;
newdomainuserrow.AccountName = "Domain";
drawingNumDBDataSet.Accounts.Rows.Add(newdomainuserrow);
MessageBox.Show("User Saved");
this.Validate();
this.usersBindingSource.EndEdit();
this.accountBindingSource.Endedit();
this.tableAdapterManager.UpdateAll(this.DataSet);
All help is greatly appreciated. 非常感谢所有帮助。
Matt 马特
I found a solution. 我找到了解决方案。 The id field is not longer an identity autoincrement field.
id字段不再是身份自动递增字段。 To increment the id field one by one programmatically like I need to I wrote a simply while statement to get all numbers that were not used.
要像我需要的那样以编程方式逐一递增id字段,我编写了一个while语句来获取所有未使用的数字。 This works if there is a deleted row it will insert one where there is one missing.
如果有被删除的行,这将起作用,它将在缺少的行中插入一个。 here is the code I used.
这是我使用的代码。
Boolean gotnum;
gotnum = false;
int idnum = 1;
while (gotnum != true)
{
DrawingNumDBDataSet.AccountsRow actrw = drawingNumDBDataSet.Accounts.FindById(idnum);
idnum++;
if (actrw==null)
{
gotnum = true;
idnum--;
}
}
I then set the Id field = to idnum. 然后,我将Id字段=设置为idnum。 This is probably not the best practice but it is the best I could come up with.
这可能不是最佳做法,但我可以提出的最佳做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.