如何将对象类放入SQL Query C#中?

问题描述:

我想将来自类'Gebruikersklasse'的对象'gebruiker'的值放入buttonclick中的查询中.该查询将被发送到'Databaseconnection'类,并在那里执行.如果运行此程序,我将收到错误消息.它告诉我找不到gebruikers.Naam.所以他找不到我认为的名为"gebruiker"的对象?

I would like to put the values of object 'gebruiker' from class 'Gebruikersklasse' to the query in the buttonclick. The query will be sent to the 'Databaseconnection' class and will be executed there. I'm getting an error if I run this. It tells me that gebruikers.Naam can't be found. So he can't find an object called 'gebruiker' I think?

如果我将查询更改为手动值,它将起作用.

If I change the query to manually values, it works.

我正在使用这些类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BurnThatFat
{
    class Gebruikerklasse
    {
        public string Naam;
        public string Achternaam;
        public int Leeftijd;
        public string Geslacht;
        public int Huidiggewicht;
        public int Streefgewicht;
        public string Gebruikersnaam;
        public string Email;
        public string Wachtwoord;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// voor sql connectie.
using System.Data.SqlClient;
using System.Windows.Forms;

这是数据库连接的代码(执行查询)

and this is the code for the databaseconnection (executing the queries)

namespace BurnThatFat
{
  class databaseconnection
  {
    string connectionString = @"Data Source=(LocalDB)\V11.0;AttachDbFilename=C:\Users\Cihan\Documents\BurnThatFat\BurnThatFat\Database2.mdf;Integrated Security=True";



    public void QueryToDatabase(string commandText, Gebruikerklasse gebruiker)
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand(commandText, conn))
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
     }
  }
}

,这是 buttonclick 代码:

private void btn_emailvolgende_Click(object sender, EventArgs e)
{
    gebruiker = new Gebruikerklasse();
    gebruiker.Naam = Convert.ToString(tb_voornaam.Text);
    gebruiker.Achternaam = Convert.ToString(tb_achternaam.Text);
    gebruiker.Leeftijd = Convert.ToInt32(nud_leeftijd.Value);
    gebruiker.Geslacht = Convert.ToString(cb_geslacht.Text);
    gebruiker.Huidiggewicht = Convert.ToInt32(nud_huidiggewicht.Value);
    gebruiker.Streefgewicht = Convert.ToInt32(nud_streefgewicht.Value);
    gebruiker.Gebruikersnaam = Convert.ToString(tb_gebruikersnaam2.Text);
    gebruiker.Email = Convert.ToString(tb_email.Text);
    gebruiker.Wachtwoord = Convert.ToString(tb_wachtwoordsignup.Text);

    db.QueryToDatabase("INSERT INTO Gebruiker ([Gebruiker-ID], Naam, Achternaam, Leeftijd, Geslacht, Huidig_gewicht, Streef_gewicht, Gebruikersnaam, Email, Wachtwoord) VALUES(1, gebruiker.Naam, gebruiker.Achternaam, gebruiker.Leeftijd, gebruiker.Geslacht, gebruiker.Huidiggewicht, gebruiker.Streefgewicht, gebruiker.Gebruikersnaam, gebruiker.Email, gebruiker.Wachtwoord);", gebruiker);
    gb_email.Visible = false;
}

我得到的确切错误:

在BurnThatFat.exe中发生了类型为'System.Data.SqlClient.SqlException'的未处理异常

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in BurnThatFat.exe

其他信息:多部分标识符"gebruiker.Naam"无法绑定.

Additional information: The multi-part identifier "gebruiker.Naam" could not be bound.

无法绑定多部分标识符"gebruiker.Achternaam".

The multi-part identifier "gebruiker.Achternaam" could not be bound.

无法绑定多部分标识符"gebruiker.Leeftijd".

The multi-part identifier "gebruiker.Leeftijd" could not be bound.

无法绑定多部分标识符"gebruiker.Geslacht".

The multi-part identifier "gebruiker.Geslacht" could not be bound.

无法绑定多部分标识符"gebruiker.Huidiggewicht".

The multi-part identifier "gebruiker.Huidiggewicht" could not be bound.

无法绑定多部分标识符"gebruiker.Streefgewicht".

The multi-part identifier "gebruiker.Streefgewicht" could not be bound.

无法绑定多部分标识符"gebruiker.Gebruikersnaam".

The multi-part identifier "gebruiker.Gebruikersnaam" could not be bound.

无法绑定多部分标识符"gebruiker.Email".

The multi-part identifier "gebruiker.Email" could not be bound.

无法绑定多部分标识符"gebruiker.Wachtwoord".

The multi-part identifier "gebruiker.Wachtwoord" could not be bound.

您需要将 gebruiker 值传递给SQL命令.现在,您没有这样做.您的 QueryToDatabase 应该通过传递参数来扩展

You need to pass gebruiker values to the SQL command. Right now you're not doing that. Your QueryToDatabase should be extended with argument passing

public void QueryToDatabase(string commandText, Gebruikerklasse gebruiker)
{
    using (SqlConnection conn = new SqlConnection(connectionString))
    using (SqlCommand cmd = new SqlCommand(commandText, conn))
    {
        conn.Open();
        cmd.Parameters.AddWithValue("@Naam", gebruiker.Naam);
        cmd.Parameters.AddWithValue("@Achternaam, gebruiker.Achternaam);
        // do the same with others properties in gebruiker

        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

您的查询也是错误的.您需要告诉SQL应该在哪里寻找参数.

Also your query is wrong. You need to tell SQL where it should look for parameters.

"INSERT INTO Gebruiker ([Gebruiker-ID], Naam, Achternaam, Leeftijd, Geslacht, Huidig_gewicht, Streef_gewicht, Gebruikersnaam, Email, Wachtwoord) VALUES(1, @Naam, @Achternaam, @Leeftijd, @Geslacht, @Huidiggewicht, @Streefgewicht, @Gebruikersnaam, @Email, @Wachtwoord);

SQL查询中的名称与 AddWithValue 中使用的名称应匹配.

The names in the SQL query and names used in AddWithValue should match.

如果您不想手动进行操作,请查看ORM.

If you do not want to do that manually - have a look at ORMs.