在C#中为每条记录创建记录

时间:2017-05-27 14:37:04

标签: c# sql

  • 我想在一条记录中创建记录。

  • 例如:

订单#0001

Color: 1,4,6,8
PCs: 6,4,3
Cutting, 30-25,30

订单#0002

与订单#0001

不同
Color: 2,8,7,9
PCs: 3,1,2
Cutting, 25-30,40
  • 当我浏览订单号时,它会显示不同的客户名称及其订单详情。

  • 我将输入订单详情,然后按"添加到订单"它将转到下面的datagridview,一旦所有订单完成,我将按"提交订单"这是一个完整的记录。

    • 当我按下" New Order"数据网格视图中的顺序如下,而不是在DataGridView中没有任何数据的情况下创建一个全新的顺序。

enter image description here

这是我的代码:

void AddRecords()
    {
                Connection con = new OrderManager.Connection();
                SqlCommand cmd = new SqlCommand();
                string query = @"INSERT INTO [MasterDatabase].[dbo].[Neworder]
        ([OrderID]
        ,[Date]
        ,[Customer_Name]
        ,[Quality]
        ,[Color]
        ,[PCs]
        ,[Cutting]
        ,[TotalYards])
VALUES
        (@OrderID
        ,@Date
        ,@Customer_Name
        ,@Quality
        ,@Color
        ,@PCs
        ,@Cutting
        ,@TotalYards)";

            cmd = new SqlCommand(query, con.ActiveCon());
            pcs = Convert.ToInt32(updown_PCs.Text);
            cutting = Convert.ToInt32(combo_Cutting.Text);
            total_yards = pcs * cutting;
            cmd.Parameters.AddWithValue("@OrderID", TxtBox_OrderID.Text);
            cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value.ToString());
            cmd.Parameters.AddWithValue("@Customer_Name", txtbox_CusName.Text);
            cmd.Parameters.AddWithValue("@Quality", combo_Quality.Text);
            cmd.Parameters.AddWithValue("@Color", combo_Color.Text);
            cmd.Parameters.AddWithValue("@PCs", updown_PCs.Text);
            cmd.Parameters.AddWithValue("@Cutting", combo_Cutting.Text);
            cmd.Parameters.AddWithValue("@TotalYards", total_yards);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Record Inserted", "Record Entered Successfully!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        else { MessageBox.Show("Please Check all fields", "Check your Input", MessageBoxButtons.OK, MessageBoxIcon.Error); }
    }

1 个答案:

答案 0 :(得分:0)

我不知道您使用SqlCommand的原因(可能更好地使用现代ORM(EF或NHibernate)重写此应用程序......但在您的情况下需要修复错误行为:

    // need create domain type for keep order information
    // if need you can implement INotifyPropertyChanged
    public class Order {
            int      OrderID {get;set}
            DateTime Date {get;set}
            string   Customer_Name {get;set}
            int      Quality {get;set}
            string   Color {get;set}
            string   PCs       {get;set}
            string   Cutting {get;set}
            decimal  TotalYards {get;set}
    }

    // add property inside your form or viewmodel which is source for your form 
    private Order _currentOrder;
    public Order CurrentOrder
    {
        get{ return _currentOrder ;}
        set {
            if (_currentOrder!=value)
            {           
                 FillOrderDetailsBy(_currentOrder.Id)
            }
        }
    }

    // implement FillOrderDetails (
    void FillOrderDetails(int orderId)
    {
       var cmd = new SqlCommand(@"Select * from details", conn)
       var reader = cmd.ExecuteReader()

       while (reader.Read())
       {
            // add records to your dataTable which is sour
       }  
    }

    // change your newOrder button click handler
    void bntNewOrder_Click(sender, ars)
    {
         CurrentOrder = new Order(); // it will clear your dataGrid
    }

    // in case if in your domain you implmenet INotifyPropertyChanged
    // you can bind you controls to property of this class
    // this give you fill the power of WinForms dataBinding :))
    // for example in your constructor (after line with InitializeComponents())
    public YourFormName()
   {
    InitializeComponents();

    TxtBox_OrderID.DataBindings.Add("Text", CurrentOrder, "OrderID");
    dateTimePicker1.DataBindings.Add("Value", CurrentOrder, "Date");
    // etc with all controls which are related to Order
   } 

希望这种方法能解决您的问题。