简体   繁体   English

C#:使用linq将数据插入数据库

[英]C# : insert data into database using linq

i want to insert the data into my database, using linq. 我想使用linq将数据插入到我的数据库中。 i follow the instruction , then i do some coding here, based on the followed instruction 我按照说明进行操作 ,然后根据遵循的说明在此处进行一些编码

 private void addNasabah()
    {
        char gender = cekRadio();

        customer customer = new customer()
        {
            nomor_customer = int.Parse(innomor.Text),
            nama = innama.Text.ToString(),
            jenis_kelamin = gender,
            alamat = inalamat.Text.ToString(),
            nomor_telepon = innomor.Text.ToString(),
            saldo = 0
        };

        dc.customers.InsertOnSubmit(customer);
        try
        {
            dc.SubmitChanges();
            cmd.cetakSukses("Berhasil Menyimpan Nasabah", "Tambah Nasabah");
            innomor.Text = genNextId().ToString();
        }
        catch (Exception e)
        {
            cmd.cetakGagal(e.ToString(), "Tambah Nasabah");                                
        }

    }

here is cekRadio() method 这是cekRadio()方法

 private char cekRadio()
    {
        if (radioButton1.Checked)
        {
            return 'p';
        }
        else
        {
            return 'w';
        }
    }

but i encountered some error, here's the error 但我遇到了一些错误,这是错误 错误

the picture said "input string was not in a correct format" . 图片显示“输入字符串的格式不正确”。

what is the expected string correct format ? 预期的字符串正确格式是什么?

here is my genNextId() method 这是我的genNextId()方法

  private int genNextId()
    {
        int id = (from a in dc.customers
                  select a.nomor_customer).Max();

        return id+1;
    }

i try running the application, the messagebox showed "Berhasil Menyimpan Nasabah", "Tambah Nasabah" but then i checked in my database, the entered data wasn't there. 我尝试运行该应用程序,消息框显示“ Berhasil Menyimpan Nasabah”,“ Tambah Nasabah”,但随后我在数据库中签入,输入的数据不存在。

here is my customer table on the database 这是我在数据库上的customer 缺少属性

Put a break point next to this line Customer customer = new Customer() and you can now check what the value of each variable is, the int.Parse() could be an issue if the string is not able to be parsed into an integer . 在此行旁边放置一个断点Customer customer = new Customer() ,现在您可以检查每个变量的值,如果无法将字符串解析为整数 ,则int.Parse()可能会成为问题。 。

Also, make sure the properties of Customer are the expected types , check jenis_kelamin for example. 另外,确保Customer的属性是预期的类型 ,例如,检查jenis_kelamin

Another (unrelated to bug) point, if you're using Object Initializer ie new Object {} , then you can drop the parenthesis (), so you would have... 另一个要点(与错误无关),如果您使用的是Object Initializer,new Object {} ,则可以删除括号(),这样就可以...

Customer customer = new Customer { ..properties.. };

EDIT : In light of some of the comments you have posted, I need to point out the way you are retrieving the Next ID is a bad approach. 编辑 :鉴于您发表的一些评论,我需要指出您检索下一个ID的方式是一种不好的方法。 I specified in one of the comments above that you should use an Identity field in SQL, this way you set the seed and increment , and SQL will generate the next ID on your behalf. 我在上面的注释中指定了您应该在SQL中使用一个Identity字段,这样您可以设置seedincrement ,SQL会代表您生成下一个ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM