简体   繁体   English

将数据存储在表中 (SQL)

[英]Storing the data in table ( SQL)

I am new to this programming field basically i from electrical field.我是这个编程领域的新手,基本上我来自电气领域。 now i learn SQL concepts.i have one database in that database had two table.现在我学习 SQL 概念。我在那个数据库中有一个数据库有两个表。 my database name is " MyDataBase ",and first table name is "Assets" then my second tale name is " AssetAssigneeMapper ".我的数据库名称是“ MyDataBase ”,第一个表名称是“资产”,然后我的第二个故事名称是“ AssetAssigneeMapper ”。

First table columns are (Assets)第一个表列是(资产)

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

My second table columns are (AssetAssigneeMapper)我的第二个表列是 (AssetAssigneeMapper)

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

I successfully store the data in my first table (Assets) by my coding but when i try to store data in second table, Compile is jumped to "catch" part.我通过编码成功地将数据存储在我的第一个表(资产)中,但是当我尝试将数据存储在第二个表中时,编译被跳转到“捕获”部分。

here is my code这是我的代码

This code is for store data in first table此代码用于在第一个表中存储数据

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();
                }

My code for store the data in second table is我将数据存储在第二个表中的代码是

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");
                }

When i compile above( AssetAssigneeMapper ) code, my result is below当我编译上面( AssetAssigneeMapper )代码时,我的结果如下

SAMPLE OUTPUT样品输出

Any one please point out what i did wrong also suggest me right way任何人请指出我做错了什么也建议我正确的方法

i got my desired output, what i did in here is ,i defined "assetId" is a "not null" type.In above code i didn`t add text to store assetId so only i get wrong output.我得到了我想要的输出,我在这里所做的是,我定义“assetId”是一个“非空”类型。在上面的代码中,我没有添加文本来存储资产 ID,所以只有我得到了错误的输出。

right code is below正确的代码在下面

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