问题描述
好吧,
我正在努力从我的.net应用程序中正确访问和更新我的数据差不多一个月但是我没有成功并且非常沮丧。
It is almost a month that I’m struggling to correctly access and update my data from my .net application but I am not successful and very upset.
我已经关注了许多不同的教程和红色的几篇文章,看起来相当容易,但显然我有些不对劲,我变老了吗?
I have followed many different tutorials and red several articles, It looked fairly easy, but apparently something is wrong with me, am I getting old?
此处的情景和步骤(当然失败):
Here the scenario and the steps that I took (failed of course):
我的MSSQL数据库中有几个表,每个表都有一个Shadow表,用于记录插入/更新/删除操作审核。
I have several tables in my MSSQL database and each table has a Shadow table which is used to log the Insert/Update/Delete operation audits.
在一个记录中插入一个记录表,将在包含原始表infor的影子表中创建记录mation加上一些其他字段,如"AuditAction","UserID",AuditDateTime",所以我必须自定义插入操作。
When insert a record in a table, a record will be created in the shadow table containing the original table information plus some other fields like "AuditAction", "UserID", AuditDateTime", So I have to customize the insert operation to do so.
Step1:
我已将Linq To SQL类"LOG.dbml"添加到我的应用程序中。
I have added a Linq To SQL class "LOG.dbml" to my application.
然后我使用ORM设计器(VS2008)将以下表添加到其中。
Then I added the following tables to it using the ORM designer (VS2008).
Logbook,Logbook_shd,Tests和Tests_shd
Logbook, Logbook_shd, Tests and Tests_shd
Test表有一个Logbook的外键.LogbookID
The Test table has a foreign key to Logbook.LogbookID
我也是使用表格唯一ID字段将每个表格链接到设计器中的阴影。
I also linked each table to its shadow within the designer using the table unique ID field.
Step2:
< div>
<div>
我在我的应用程序中添加了一个C#Class"BAL"。并创建了以下函数:
I added a C#Class "BAL" to my application. And created the following function:
public int insertLogbook()
{
using (var Context = new LOGDataContext())
{
String UserID= System.Environment.UserName;
DateTime TransDateTime = DateTime.Now;
// Add a new logbook record
Logbook l = new Logbook();
l.CreatedBy = UserID;
l.CreatedDate = TransDateTime;
l.Stat = "New";
l.Ver = 0;
Context.Logbooks.InsertOnSubmit(l);
//Add a new Shadow record
Logbooks_shd ls = new Logbooks_shd();
ls.LogBookID = l.LogBookID;
ls.CreatedBy = l.CreatedBy;
ls.CreatedDate = l.CreatedDate;
ls.Stat = l.Stat;
ls.Ver = l.Ver;
ls.AuditAction = "Create";
ls.AuditDateTime = TransDateTime;
ls.AuditUser = UserID;
ls.AuditApp = "APS Electronic Log";
Context.Logbooks_shds.InsertOnSubmit(ls);
l.Logbooks_shds.Add(ls);
Context.SubmitChanges();
return l.LogBookID;
}
}
Step3:
我在我的应用程序中添加了一个webform并将以下控件放入其中。
I added a webform to my application and throw the following controls into it.
Button1:将新记录插入日志表并执行shadow操作。
Button1: To insert a new record to the logbook table and perform the shadow operation on Click.
Label1:显示插入记录的LogbookID。
Label1: To display the LogbookID of the inserted record.
GridView1:显示与LogbookID(Lable1)相关的测试并执行插入并在Test表上更新操作。
GridView1: To display the tests related to the LogbookID (Lable1) and perform insert and update operations on Test table.
然后我将以下代码添加到Button1的click事件处理程序。
Then I added the following code to the click event handler of Button1.
我在浏览器上运行了表单(IE)并单击Button1,一切顺利,插入操作正确执行,Label1显示新的logbookID。 Yayyyyy
I ran the form on the browser (IE) and Clicking on Button1, everything goes well and the insert operation performed correctly and the new logbookID shown by Label1. Yayyyyy
但是等一下这就是问题来了...... ..
But wait a minute here is the problem coming…..
我在"BAL"类中创建了另外两个函数以通过LogbookID进行测试,还插入测试记录:
I created two other functions within "BAL" class to get tests by LogbookID, and also Insert Test records:
public List<Test> getTestsByLogbookIDs(string LogBookID)
{
using (var context = new APSELOGDataContext())
{
return (from T in context.Tests
where T.LogbookID == int.Parse(LogBookID)
select T).ToList();
}
}
public int InsertTest(Test pt )
{
using (var Context = new LOGDataContext())
{
String UserID = System.Environment.UserName;
DateTime TransDateTime = DateTime.Now;
// Add a new Test record
Test t = new Test();
t.CreatedBy = UserID;
t.CreatedDate = TransDateTime;
t.Stat = "New";
t.Ver = 0;
t.LogbookID = pt.LogbookID;
t.TestName = pt.TestName;
Context.Tests.InsertOnSubmit(t);
//Add a new Test hadow record
Tests_shd ts = new Tests_shd();
ts.TestID = t.ID;
ts.CreatedBy = t.CreatedBy;
ts.CreatedDate = t.CreatedDate;
ts.Stat = t.Stat;
ts.Ver = t.Ver;
ts.LogbookID = t.LogbookID;
ts.TestName = t.TestName;
ts.AuditAction = "Create";
ts.AuditDateTime = TransDateTime;
ts.AuditUser = UserID;
ts.AuditApp = "APS Electronic Log";
Context.Tests_shds.InsertOnSubmit(ts);
t.Tests_shds.Add(ts);
Context.SubmitChanges();
return t.ID;
}
}
然后我使用gridview1的智能标记创建了一个ObjectDataSource,"ObjectDataSource1",它在我的内容中抛出了以下aspx代码表格。
Then I created an ObjectDataSource, "ObjectDataSource1" using the smart tag of the gridview1 and it has thrown the following aspx code in my form.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
onselecting="ObjectDataSource1_Selecting" SelectMethod="getTestsByLogbookIDs"
TypeName="ElogObjectBoundingTest.Bal"
DataObjectTypeName="ElogObjectBoundingTest.Test" InsertMethod="InsertTest">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="Null" Name="LogBookID"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
当我再次在浏览器上运行我的表单时,我得到了"呃 - 哦"出错!错误代码:500"
When I ran my form on the browser again I got "Uh-Oh Something went wrong! Error Code:500"
有没有人可以帮助我摆脱痛苦?????
Is there anybody who can help me out of my misery?????
.NET中的初学者
推荐答案
这篇关于如何使用“ObjectDataSource控件”使用自定义插入/更新/删除操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!