How to get an object class into SQL Query c#?

梦想的初衷 提交于 2019-12-25 09:25:59

问题


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.

I am using these classes:

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

and this is the buttonclick code:

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

Edit:

The exact error that I'm getting:

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

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

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

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

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

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

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

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

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

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


回答1:


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

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

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

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




回答2:


You're getting that error because SQL is not finding any table or reference with that name, you can achieve something similar with a stored procedure.

First create the procedure in sql:

CREATE PROCEDURE insertGebruiker(
    @Naam VARCHAR(50),
    @Achternaam VARCHAR(50),
    @Leeftijd INT, 
    @Geslacht VARCHAR(50),
    @Huidiggewicht INT,
    @Streefgewicht INT,
    @Gebruikersnaam VARCHAR(50),
    @Email VARCHAR(50),
    @Wachtwoord VARCHAR(50),
)
AS
BEGIN
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,
)
END

Now from your c# code:

public void QueryToDatabase(string procedureName, Gebruikerklasse gebruiker)
    {

        using (SqlConnection conn = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand(procedureName, conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Naam", gebruiker.Naam);
            cmd.Parameters.AddWithValue("@Achternaam", gebruiker.Achternaam);
            cmd.Parameters.AddWithValue("@Leeftijd", gebruiker.Leeftijd);
            cmd.Parameters.AddWithValue("@Geslacht", gebruiker.Geslacht);
            cmd.Parameters.AddWithValue("@Huidiggewicht", gebruiker.Huidiggewicht);
            cmd.Parameters.AddWithValue("@Streefgewicht", gebruiker.Streefgewicht);
            cmd.Parameters.AddWithValue("@Gebruikersnaam", gebruiker.Gebruikersnaam);
            cmd.Parameters.AddWithValue("@Email", gebruiker.Email);
            cmd.Parameters.AddWithValue("@Wachtwoord", gebruiker.Wachtwoord);

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

        }
    }

And you just call it using the name of your procedure:

db.QueryToDatabase("insertGebruiker", gebruiker);


来源:https://stackoverflow.com/questions/41505362/how-to-get-an-object-class-into-sql-query-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!