2017-06-29 1 views
-5

Hallo Stapelüberlauf Sie sind meine einzige Hoffnung, Es ist meine erste Frage hier. Ich habe Probleme mit dem Einfügen von Dingen in eine Datenbank aus Textfeldern Ich habe es durchsucht Ich habe alles versucht, es funktioniert einfach nicht. Hier ist mein Code ->Fügen Sie Daten aus einem Textfeld in eine Datenbank ein. C#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace E_BANK 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btn_save_Click(object sender, EventArgs e) 
     { 
      //objekti per konektim me DBne 
      SqlConnection conn = new SqlConnection(@"Data Source=desktop-ndocu0t\sqlexpress;Initial Catalog=MetinShop;Integrated Security=True"); 

      conn.Open(); 
      using (SqlCommand command = conn.CreateCommand()) 
      { 
       using (SqlDataReader reader = command.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         string accref = tb_accref.Text; 
         int deposit = Convert.ToInt32(tb_depamount.Text); 
         int psID = Convert.ToInt32(tb_personalID.Text); 
         string query = "INSERT INTO Bank1(accref, deposit, psID) " + 
       "Values('" + accref + "', '" + deposit + "', '" + psID + "')"; 
        } 
       } 
      } 

      //mbylle lidhjen me DB 
      conn.Close(); 
      MessageBox.Show("Transition Updatet Sucessfully"); 
     } 

     private void btn_reset_Click(object sender, EventArgs e) 
     { 
      tb_accref.Text = ""; 
      tb_depamount.Text = ""; 
      tb_personalID.Text = ""; 
      lblamount.Text = "0"; 
     } 

     private void btn_exit_Click(object sender, EventArgs e) 
     { 
      Close(); 
     } 
    } 
} 
+2

fehlt EXECUTE Wenn dies Ihre erste Frage ist, lesen Sie bitte [fragen] - Sie mehr Details als *“... es zu bieten haben funktioniert einfach nicht "*. – Filburt

+0

Sql-Injektion wird nicht empfohlen, wie beschrieben [hier] (https://stackoverflow.com/questions/13484118/sql-injection-prevention-in-net) –

+0

Hallo und willkommen. Wie Filburt sagt, wäre mehr Information schön. was ist das Problem? wo ist es? Hast du eine Ausnahme? Welche Ausnahme bekommst du? Da ist dein +1. Genießen Sie Ihren Aufenthalt in SO. – pijemcolu

Antwort

2

Sie müssen tatsächlich den INSERT-Befehl ausführen. Überprüfen Sie die ExecuteNonQuery-Methode für die SqlCommand-Klasse.

+0

command.Connection.Open(); command.ExecuteNonQuery(); command.Connection.Close(); – pijemcolu

5

auf diese Weise versuchen

private void btn_save_Click(object sender, EventArgs e) 
    { 

     using (SqlConnection sqlConn = new SqlConnection(@"Data Source=desktop-ndocu0t\sqlexpress;Initial Catalog=MetinShop;Integrated Security=True")) 
     { 
      using (SqlCommand sqlComm = new SqlCommand("INSERT INTO Bank1 (accref, deposit, psID) VALUES (@accref, @deposit, @psID)", sqlConn)) 
      { 
       if (sqlComm.Connection.State == ConnectionState.Closed) 
        sqlComm.Connection.Open(); 
       string accref = tb_accref.Text; 
       int deposit = Convert.ToInt32(tb_depamount.Text); 
       int psID = Convert.ToInt32(tb_personalID.Text); 

       sqlComm.Parameters.AddWithValue("@accref", accref); 
       sqlComm.Parameters.AddWithValue("@deposit", deposit); 
       sqlComm.Parameters.AddWithValue("@psID", psID); 
       sqlComm.ExecuteNonQuery(); 
       MessageBox.Show("Transition Updatet Sucessfully"); 
      } 
     } 
    } 
+0

Ich habe es versucht, aber es sagt Ausnahme inkorrekte Syntax in der Nähe von Bank1 –

+0

Es gab keine Leerzeichen zwischen Bank1 und der Paranthesis '('. Ich änderte den Code, Versuchen Sie dies. –

+0

Ist Ihre Datenbank Case Sensible? –

0

Befehl

private void btn_save_Click(object sender, EventArgs e) 
{ 
    //objekti per konektim me DBne 
    SqlConnection conn = new SqlConnection(@"Data Source=desktop-ndocu0t\sqlexpress;Initial Catalog=MetinShop;Integrated Security=True"); 

    conn.Open(); 

    string accref = tb_accref.Text; 
    int deposit = Convert.ToInt32(tb_depamount.Text); 
    int psID = Convert.ToInt32(tb_personalID.Text); 
    //try all fields are string 
    string query = "INSERT INTO Bank1(accref, deposit, psID) Values('" + accref + "', '" + deposit + "', '" + psID + "')"; 
    //try last fields are numeric 
    //string query = "INSERT INTO Bank1(accref, deposit, psID) Values('" + accref + "', " + deposit + ", " + psID + ")"; 

    SqlCommand command = new SqlCommand(); 
    command.Connection = conn; 
    command.CommandText = query; 
    command.ExecuteNonQuery() 

    //mbylle lidhjen me DB 
    conn.Close(); 
    MessageBox.Show("Transition Updatet Sucessfully"); 

} 

private void btn_reset_Click(object sender, EventArgs e) 
{ 
    tb_accref.Text = "0"; 
    tb_depamount.Text = "0"; 
    tb_personalID.Text = "0"; 
    lblamount.Text = "0"; 
} 

private void btn_exit_Click(object sender, EventArgs e) 
{ 
    Close(); 
} 
Verwandte Themen