在表中存储数据(SQL)

时间:2016-10-13 08:43:27

标签: c# mysql 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).

我通过编码成功地将数据存储在我的第一个表(Assets)中,但是当我尝试将数据存储在第二个表中时,编译被跳转到" catch"部分。

这是我的代码

此代码用于第一个表中的商店数据

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 )代码时,我的结果在

之下

SAMPLE OUTPUT

任何人请指出我做错了也建议我正确的方式

1 个答案:

答案 0 :(得分:1)

我得到了我想要的输出, 我在这里做的是,我定义“assetId”是一个“非空”类型。在上面的代码我没有添加文本来存储 assetId 所以只有我得到错误的输出。

正确的代码在

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