自定义选择更新和编辑命令 [英] Custom Select update and edit commands

查看:82
本文介绍了自定义选择更新和编辑命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的沮丧程度是如此高,我可以大喊大叫,所以请忽略任何错误。



我们有一个传统的Access系统,有表格和数据库初始化Access系统是实时的,我们正试图通过Web表单将其移植到C#。 net。



由于MS迁移过程中出现大量错误,我们无法迁移到SQL。



关于Access的一个好处是构建表单是多么容易,用C#做事我发现不合逻辑和令人沮丧。



At这就是我坚持的那一刻。我们有一张非常规范的桌子。这是一份交付订单的表格。如果客户一天有多个订单,则订单日期,准备(或烘焙)日期,所需日期和其他字段将重复。



所以我很热衷创建主详细信息表单。我创建了一个带标题和详细信息的对象:



< blockquote  class  =   FQ>< div  class  =   FQA> Quote: < /   div  >   namespace  TrackerDotNet.classes 
{
/// < 摘要 >
/// 基于订单查询类存储数据
/// SQL:SELECT订单Tbl.CustomerID,CustomersTbl.CompanyName,OrdersTbl.OrderDate,+
// OrdersTbl .RoastDate,OrdersTbl.RequiredByDate,OrdersTbl.Confirmed,OrdersTbl.Notes,+
// PersonsTbl.Abreviation
/// < / summary >
public class OrderHeaderDetails
{

// CustomersTbl.CompanyName,OrdersTbl.CustomerId,OrdersTbl.OrderDate,OrdersTbl.RoastDate,
// OrdersTbl.RequiredByDate,PersonsTbl.Ab reviation,OrdersTbl.Confirmed,OrdersTbl.Done,OrdersTblNotes
public OrderHeaderDetails()
{
_otCustomerID = 0 ;
_ctCompanyName = _ptAbreviation = _otNotes = ;
_otOrderDate = _otRoastDate = _otRequiredByDate = DateTime.Now;
_otConfirmed = true ;
_otDone = false ;
}

private long _otCustomerID;
private string _ctCompanyName;
private DateTime _otOrderDate;
private DateTime _otRoastDate;
private DateTime _otRequiredByDate;
private bool _otConfirmed;
private bool _otDone;
private string _otNotes;
private string _ptAbreviation;

public long CustomerID
{
get { return _otCustomerID; }
set {_otCustomerID = value ; }
}
public string CompanyName
{
get { return _ctCompanyName; }
set {_ctCompanyName = value ; }
}
public DateTime OrderDate
{
get { return _otOrderDate; }
set {_otOrderDate = value ; }
}
public DateTime RoastDate
{
get { return _otRoastDate; }
set {_otRoastDate = value ; }
}
public DateTime RequiredByDate
{
get { return _otRequiredByDate; }
set {_otRequiredByDate = value ; }
}
public bool 确认
{
get { return _otConfirmed; }
set {_otConfirmed = value ; }
}
public bool 完成
{
get { return _otDone; }
set {_otDone = value ; }
}
public string
{
get { return _otNotes; }
set {_otNotes = value ; }
}
public string Abreviation
{
get { return _ptAbreviation; }
set {_ptAbreviation = value ; }
}
}

public class OrderLinesDetails
{
// ItemTypeID,QuantityOrdered,PrepTypeID
public OrderLinesDetails()
{
_otItemTypeID = _otPrepTypeID = 0 ;
_otQuantityOrdered = 0 00 ;
}

private long _otItemTypeID,_otPrepTypeID;
private double _otQuantityOrdered;

public long otItemTypeID { get { return _otItemTypeID; } set {_ intItemTypeID = value ; }
public long otPrepTypeID { get { return _otPrepTypeID; } set {_otPrepTypeID = value ; }
public double otQuantityOrdered { get { return _otQuantityOrdered; } set {_otQuantityOrdered = value ; }
}

}
< / blockquote >





我还创建了一个对象,我希望将作为对象数据源链接到detailview(header / master)和gridview(行/细节)



< blockquote  class  =   FQ>< ; div  class  =   FQA> ; Quote:< /   div  >  命名空间 TrackerDotNet.App_Code 
{
public OrdersData
{

const string CONST_CONSTRING = Tracker08ConnectionString;
const string CONST_ORDERSHEADER_SELECT = SELECT CustomersTbl.CompanyName,OrdersTbl.CustomerId As CustomerId,OrdersTbl.OrderDate,OrdersTbl.RoastDate, +
OrdersTbl.RequiredByDate,PersonsTbl.Abreviation,OrdersTbl.Confirmed,OrdersTbl.Done,OrdersTbl.Notes +
FROM((OrdersTbl LE​​FT OUTER JOIN PersonsTbl ON OrdersTbl.ToBeDeliveredBy = PersonsTbl.PersonID) +
LEFT OUTER JOIN CustomersTbl ON OrdersTbl.CustomerId = CustomersTbl.CustomerID) +
WHERE([CustomerId] =?)AND([RoastDate] =?);
const string CONST_ORDERSLINES_SELECT = SELECT ItemTypeID,QuantityOrdered,PrepTypeID FROM OrdersTbl WHERE([CustomerId] =?)AND([RoastDate] =?);

// connection Strinbg
private string _connectionString;

public OrdersData()
{
Initialize();
}
public void Initialize()
{
// 初始化数据源。使用配置中的Tracker08连接字符串。

if (ConfigurationManager.ConnectionStrings [CONST_CONSTRING] == null ||
ConfigurationManager.ConnectionStrings [CONST_CONSTRING] .ConnectionString.Trim()==
{
throw new 异常( 名为 + CONST_CONSTRING + 的连接字符串 带有效连接字符串 +
必须存在于应用程序的< connectionStrings>配置部分中。);
}
_connectionString =
ConfigurationManager.ConnectionStrings [CONST_CONSTRING] .ConnectionString;
}

public List< OrderHeaderDetails> LoadOrderHeader( Int32 pCustomerId,DateTime pPrepDate)
{
List< OrderHeaderDetails> ohDetails = new List< OrderHeaderDetails> ;;

string _sqlCmd = CONST_ORDERSHEADER_SELECT;
OleDbConnection _conn = new OleDbConnection(_connectionString);

OleDbCommand _cmd = new OleDbCommand(_sqlCmd,_conn);
_cmd.Parameters.Add( new OleDbParameter {Value = pCustomerDone});
_cmd.Parameters.Add( new OleDbParameter {Value = pPrepDate});

_conn.Open();
objReader = objCmd.ExecuteReader();

while (objReader.Read())
{
OrderHeaderDetails ohDetail = new OrderHeaderDetails();

ohDetail.CustomerID =( Int32 )objReader [ 客户id];
ohDetail.CompanyName =( string )objReader [ 公司名称];
ohDetail.Abreviation =( string )objReader [ 减刑];
ohDetail.Notes =( string )objReader [ 注释];
ohDetail.OrderDate =( string )objReader [ 订购日期];
ohDetail.RoastDate =( string )objReader [ RoastDate];
ohDetail.RequiredByDate =( string )objReader [ RequiredByDate];
ohDetail.Confirmed =( bool )objReader [ 确认];
ohDetail.Done =( bool )objReader [ 完成];

ohDetails.Add(ohDetail);
}
objReader.Close();
objConn.Close();

return ohDetails;
}


}
} < / blockquote >





我之前使用的是

 da.Fill(ds,  Orders); 

但这似乎只返回OrdersTbl中的数据而非customer表。



我想做的就是能够创建自己的SELECT,UPDATE,INSERT和DELETE或CRED命令,并将它们附加到网格/详细信息视图。这真的是这样的使命!





我在CodeProject和ASP.NET上使用了很多文章来实现这一点,现在我也是沮丧。有没有更简单的方法来执行此操作?



我曾尝试将类附加到Object数据源,如下所示,但这也是有问题的,因为放置app_code文件夹中的类似乎也使得编译不起作用和自动填充编程。

 <   asp:ObjectDataSource     ID   =  odsOrderHeader    runat   =  server    TypeName   =  TrackerDotNet.App_Code.OrdersData     

EnablePaging = true SelectMethod = LoadOrderHeader >
< SelectParameters >
< asp:QueryStringParameter DefaultValue = 1 名称 = CustomerId < span class =code-attribute> QueryStringField = CustomerID 类型 = Int32 / >
< asp:QueryStringParameter 名称 = RoastDate QueryStringField = PrepDate 类型 = DateTime / >
< / SelectParameters >

< / asp:ObjectDataSource >

解决方案

经过多次重命名和重新创建后,我才能做到这一点告诉那些有同样挫折感的人。



1.首先是你要访问的数据类的公共类(在我的情况下是

 public obHeaderDetails类

必须在App_code文件夹中,我不知道为什么。

2.同样在同一个文件夹中必须是obj的类ect数据源也绑定了,我也不知道为什么,但是当我把它放在另一个文件夹中时我得到名称空间错误

3. app_code文件夹中的代码似乎没有正常的功能如果你编译/构建它不会像其他代码那样选择编译/构建错误,而只是在运行时你会得到运行时错误然后需要修复(这让我想起了一种我讨厌的语言 p并以hp结尾。我不确定是否有解决方法。

4.代码自动完成(允许您轻松访问库例程和变量名称的东西)在奇怪的原因下创建的类不起作用bermund三角ish文件夹app_code



一旦你按照这些指南行,并有耐心修复错误后错误,可能是由于作为一个错误的函数调用愚蠢的东西以

 OldDbDataReader 

为例。这段代码确实有效。



这是我在这里找到的这种编码的最佳参考: http://msdn.microsoft.com/en-us/library/ms178538(v = VS.100)的.aspx [<一个href =http://msdn.microsoft.com/en-us/library/ms178538(v=vs.100).aspx\"target =_ blanktitle =New Window> ^ ]







http://www.manuelabadia.com/blog/PermaLink,guid,c72852ae-1fdd-4934-a715-f565ceaf21cc.aspx [ ^ ]


Hi, my frustration levels are so high at the moment I could Yell, so please ignore any errors.

We have a legacy Access system that has forms and database initialization. The Access system is live and we are trying to port it across form by web form to C#. net.

We have not been able to migrate to SQL due to the large number of errors that occur during the MS migration.

The one great thing about Access is how easy it is to build forms, doing things in C# I have found illogical and frustrating.

At the moment this is what I am stuck on. We have a table that is very badly normallized. It is a table of orders to be delivered. If a client has a more than one order for a day the orderdate, preparation (or roast) date, required date and other fields are duplicated.

So I am keen to create a master detail form. I have created an object with header and details:

<blockquote class="FQ"><div class="FQA">Quote:</div>namespace TrackerDotNet.classes
{
  /// <summary>
  /// based on the order Query the classes stores the data
  /// SQL: SELECT OrdersTbl.CustomerID, CustomersTbl.CompanyName,  OrdersTbl.OrderDate, " +
  //           " OrdersTbl.RoastDate, OrdersTbl.RequiredByDate, OrdersTbl.Confirmed, OrdersTbl.Notes, " + 
  //           "PersonsTbl.Abreviation
  /// </summary>
  public class OrderHeaderDetails
  {

    //  CustomersTbl.CompanyName, OrdersTbl.CustomerId, OrdersTbl.OrderDate, OrdersTbl.RoastDate, 
    //  OrdersTbl.RequiredByDate, PersonsTbl.Abreviation, OrdersTbl.Confirmed, OrdersTbl.Done, OrdersTblNotes
    public OrderHeaderDetails()
    {
      _otCustomerID = 0;
      _ctCompanyName = _ptAbreviation = _otNotes = "";
      _otOrderDate = _otRoastDate = _otRequiredByDate  = DateTime.Now;
      _otConfirmed = true;
      _otDone = false;
    }

    private long _otCustomerID;
    private string _ctCompanyName;
    private DateTime _otOrderDate;
    private DateTime _otRoastDate;
    private DateTime _otRequiredByDate;
    private bool _otConfirmed;
    private bool _otDone;
    private string _otNotes;
    private string _ptAbreviation;

    public long CustomerID
    {
       get { return _otCustomerID; }
       set { _otCustomerID = value; }
    }
    public string CompanyName
    {
       get { return _ctCompanyName; }
       set { _ctCompanyName = value; }
    }
    public DateTime OrderDate
    {
       get { return _otOrderDate; }
       set { _otOrderDate = value; }
    }
    public DateTime RoastDate
    {
       get { return _otRoastDate; }
       set { _otRoastDate = value; }
    }
    public DateTime RequiredByDate
    {
       get { return _otRequiredByDate; }
       set { _otRequiredByDate = value; }
    }
    public bool Confirmed
    {
      get { return _otConfirmed; }
      set { _otConfirmed = value; }
    }
    public bool Done
    {
      get { return _otDone; }
      set { _otDone = value; }
    }
    public string Notes
    {
       get { return _otNotes; }
       set { _otNotes = value; }
    }
    public string Abreviation
    {
       get { return _ptAbreviation; }
       set { _ptAbreviation = value; }
    }
  }

  public class OrderLinesDetails
  {
    // ItemTypeID, QuantityOrdered, PrepTypeID 
    public OrderLinesDetails()
    {
      _otItemTypeID = _otPrepTypeID = 0;
      _otQuantityOrdered = 0.00;
    }

    private long _otItemTypeID, _otPrepTypeID;
    private double _otQuantityOrdered;

    public long otItemTypeID { get { return _otItemTypeID; } set { _otItemTypeID = value; } }
    public long otPrepTypeID { get { return _otPrepTypeID; } set { _otPrepTypeID = value; } }
    public double otQuantityOrdered { get { return _otQuantityOrdered; } set { _otQuantityOrdered = value; } }
  }
  
}
</blockquote>



I have then also created an object which I was hoping to link as an object datasource to the detailview (header / master) and gridview (lines / details)

<blockquote class="FQ"><div class="FQA">Quote:</div>namespace TrackerDotNet.App_Code
{
  public class OrdersData
  {

    const string CONST_CONSTRING = "Tracker08ConnectionString";
    const string CONST_ORDERSHEADER_SELECT = "SELECT CustomersTbl.CompanyName, OrdersTbl.CustomerId As CustomerId, OrdersTbl.OrderDate, OrdersTbl.RoastDate, " +
                                             " OrdersTbl.RequiredByDate, PersonsTbl.Abreviation, OrdersTbl.Confirmed, OrdersTbl.Done, OrdersTbl.Notes " +
                                             " FROM ((OrdersTbl LEFT OUTER JOIN PersonsTbl ON OrdersTbl.ToBeDeliveredBy = PersonsTbl.PersonID)" +
                                             " LEFT OUTER JOIN CustomersTbl ON OrdersTbl.CustomerId = CustomersTbl.CustomerID)" +
                                             " WHERE ([CustomerId] = ?) AND ([RoastDate] = ?)";
    const string CONST_ORDERSLINES_SELECT = "SELECT ItemTypeID, QuantityOrdered, PrepTypeID FROM OrdersTbl WHERE ([CustomerId] = ?) AND ([RoastDate] = ?)";

    // connection Strinbg
    private string _connectionString;

    public OrdersData()
    {
      Initialize();
    }
    public void Initialize()
    {
      // Initialize data source. Use "Tracker08" connection string from configuration.

      if (ConfigurationManager.ConnectionStrings[CONST_CONSTRING] == null ||
          ConfigurationManager.ConnectionStrings[CONST_CONSTRING].ConnectionString.Trim() == "")
      {
        throw new Exception("A connection string named " + CONST_CONSTRING + " with a valid connection string " +
                            "must exist in the <connectionStrings> configuration section for the application.");
      }
      _connectionString =
        ConfigurationManager.ConnectionStrings[CONST_CONSTRING].ConnectionString;
    }

    public List<OrderHeaderDetails> LoadOrderHeader(Int32 pCustomerId, DateTime pPrepDate)
    {
      List<OrderHeaderDetails> ohDetails = new List<OrderHeaderDetails>;

      string _sqlCmd = CONST_ORDERSHEADER_SELECT;
      OleDbConnection _conn = new OleDbConnection(_connectionString);

      OleDbCommand _cmd = new OleDbCommand(_sqlCmd, _conn);
      _cmd.Parameters.Add(new OleDbParameter { Value = pCustomerDone } );
      _cmd.Parameters.Add(new OleDbParameter { Value = pPrepDate } );

      _conn.Open();
      objReader = objCmd.ExecuteReader();
      
      while (objReader.Read()) 
      {
        OrderHeaderDetails ohDetail = new OrderHeaderDetails();
 
        ohDetail.CustomerID = (Int32)objReader["CustomerID"];
        ohDetail.CompanyName = (string)objReader["CompanyName"];
        ohDetail.Abreviation = (string)objReader["Abreviation "];
        ohDetail.Notes = (string)objReader["Notes"];
        ohDetail.OrderDate = (string)objReader["OrderDate"];
        ohDetail.RoastDate = (string)objReader["RoastDate"];
        ohDetail.RequiredByDate = (string)objReader["RequiredByDate"];
        ohDetail.Confirmed = (bool)objReader["Confirmed"];
        ohDetail.Done = (bool)objReader["Done"];
        
        ohDetails.Add(ohDetail);
      }
      objReader.Close();  
      objConn.Close();

      return ohDetails;
    }


  }
}</blockquote>



I had previously used

da.Fill(ds, "Orders");

but this seems to only return the data from the OrdersTbl not the customers table.

All I want to do is be able to create my own SELECT, UPDATE, INSERT and DELETE or CRED commands, and attach them to the grid / detail view. Is this really such a mission!


I have used many articles on CodeProject and ASP.NET to get this far and now I too frustrated. Is there not an easier way to do this?

I had tried to attach the class to the Object data source as below, but this is also problematic, since placing the class in the app_code folder seems to make to compiling not work and the auto fillful programming too.

<asp:ObjectDataSource ID="odsOrderHeader" runat="server" TypeName="TrackerDotNet.App_Code.OrdersData" 

    EnablePaging="true" SelectMethod="LoadOrderHeader" >
        <SelectParameters>
          <asp:QueryStringParameter DefaultValue="1" Name="CustomerId" QueryStringField="CustomerID" Type="Int32" />
          <asp:QueryStringParameter Name="RoastDate" QueryStringField="PrepDate" Type="DateTime" />
        </SelectParameters>

      </asp:ObjectDataSource>

解决方案

After much renaming and recreating this is what I can tell those that have the same frustrations.

1. Firstly the public class that you are accessing for both the data class (in my case

public class OrderHeaderDetails

must be in the App_code folder, I have no idea why.
2. Also in the same folder must be the class that the object data source binds too, I am not sure why either, but when I put it in another folder I got name space errors
3. code in the app_code folder does not seem to have normal features if you compile / build it does not pick up compile / build errors like the rest of the code, instead only at run time do you get run time errors that you then need to fix (gee this reminds me of a language that I hate starts with "p" and ends in "hp"). I am not sure if there is a way around this.
4. Code auto complete (the thing that lets you easily access library routines and variable names) does not work in classes created under the strange an unexplained bermund triangle ish folder app_code

Once you follow these guide lines, and have the patience to fix error after error that may be cause by something as stupid as a misspelt function call to

OldDbDataReader 

for example. This code does work.

the best references for this type of coding I have found here: http://msdn.microsoft.com/en-us/library/ms178538(v=vs.100).aspx[^]

And

http://www.manuelabadia.com/blog/PermaLink,guid,c72852ae-1fdd-4934-a715-f565ceaf21cc.aspx[^]


这篇关于自定义选择更新和编辑命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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