クラス 'Gebruikersklasse'のオブジェクト 'gebruiker'の値をbuttonclickのクエリに入れたいと思います。クエリは 'Databaseconnection'クラスに送信され、そこで実行されます。私はこれを実行するとエラーが発生します。それはGebruikers.Naamが見つからないことを私に伝えます。だから、彼は「gebruiker」と呼ばれるオブジェクトを見つけることができないと思いますか?オブジェクトクラスをSQLクエリに取得する方法C#?
クエリを手動で値に変更すると機能します。
私は、これらのクラス使用しています:
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;
を、これはdatabaseconnection(クエリの実行)
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
コードです:
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;
}
編集:
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.
何全体的な正確なエラーですか? 2つの答えはあなたのオブジェクトを 'QuerryToDatabase'に渡しても正しいですが、あなたはそれを使うことは決してありません。 – Crowcoder
@crowcoder元の投稿に正確なエラーを追加しました。 – Gigitex
これはSQLエラーです。文字列に 'gebruiker.Naam'を置くことができないので、変数の実際の値をそのように拡張しないので、答えの1つとして表示されます。 – Crowcoder