簡體   English   中英

將數據存儲在表中 (SQL)

[英]Storing the data in table ( SQL)

我是這個編程領域的新手,基本上我來自電氣領域。 現在我學習 SQL 概念。我在那個數據庫中有一個數據庫有兩個表。 我的數據庫名稱是“ MyDataBase ”,第一個表名稱是“資產”,然后我的第二個故事名稱是“ AssetAssigneeMapper ”。

第一個表列是(資產)

    assetId int (Identity),
    assetName varChar(100),
    modelNo varChar(100),
    price decimal,
    quantity int.

我的第二個表列是 (AssetAssigneeMapper)

    assignId int (Identity),
    assetId int(Foreign Key ),
    assignedQty int,
    employeeName varChar(100).

我通過編碼成功地將數據存儲在我的第一個表(資產)中,但是當我嘗試將數據存儲在第二個表中時,編譯被跳轉到“捕獲”部分。

這是我的代碼

此代碼用於在第一個表中存儲數據

try
                {
                    Console.WriteLine("Enter the asset name");
                    string assetName = (Console.ReadLine()).ToLower();
                    Console.WriteLine("Enter the model number");
                    string modelNo = Console.ReadLine();
                    Console.WriteLine("Enter the price of the asset");
                    decimal price = decimal.Parse(Console.ReadLine());
                    Console.WriteLine("Enter the quantity ");
                    int quantity = int.Parse(Console.ReadLine());
                    using (var db = new MyDataBaseEntities())
                    {
                        db.Assets.Add(new Asset()
                        {
                            assetName = assetName,
                            modelNo = modelNo,
                            price = price,
                            quantity = quantity
                        });
                        db.SaveChanges();
                        Console.WriteLine("New asset '{0}' Added in database successfully", assetName);
                    }
                }
                catch
                {
                    Console.WriteLine("Enter the proper input");
                    AddSingleAsset();
                }

我將數據存儲在第二個表中的代碼是

try
                {
                    Console.WriteLine("Enter the asset name to assign");
                    string assetName = Console.ReadLine().ToLower();

                    using (var database = new MyDataBaseEntities())
                    {
                        var Check = database.Assets.FirstOrDefault
                                         (x => x.assetName == assetName);
                        if (Check.assetName == assetName)
                        {
                            Console.WriteLine("How many asset to be assigned to employee");
                            string qty = Console.ReadLine();
                            int quantity;
                            if (int.TryParse(qty, out quantity))
                            {
                                if (Check.quantity >= quantity)
                                {

                                    Console.WriteLine("Enter the name of the employee");
                                    String employeeName = Console.ReadLine();

                                       database.AssetAssigneeMappers.Add(new AssetAssigneeMapper()
                                        {
                                            assignedQty = quantity,
                                            employeeName = employeeName
                                        });
                                        database.SaveChanges();

                                    Check.quantity = Check.quantity - quantity;
                                    database.SaveChanges();
                                    Console.WriteLine("{0}[{1}]-Quantity has successfully assigned to {2}", assetName, Check.modelNo, employeeName);
                                }
                                else
                                {
                                    Console.WriteLine("Quantity level is lower than your requirment");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Given Quantity input is INVALID");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Given Asset name is not available in database");
                        }


                    }
                }
                catch
                {
                    Console.WriteLine("Invalid input");
                }

當我編譯上面( AssetAssigneeMapper )代碼時,我的結果如下

樣品輸出

任何人請指出我做錯了什么也建議我正確的方法

我得到了我想要的輸出,我在這里所做的是,我定義“assetId”是一個“非空”類型。在上面的代碼中,我沒有添加文本來存儲資產 ID,所以只有我得到了錯誤的輸出。

正確的代碼在下面

database.AssetAssigneeMappers.Add(new AssetAssigneeMapper()
                                    {
                                        assignedQty = quantity,
                                        employeeName = employeeName,
                                        assetId=Check.assetId
                                    });
                                    database.SaveChanges();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM