将数据从文本框插入数据库.C#

-5 c# sql database oop

你好堆栈溢出你是我唯一的希望,这是我的第一个问题.我有问题从我搜索的文本框插入数据库的东西我尝试了它只是不起作用的一切.这是我的代码 - >

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cos*_*gul 5

试试这种方式

    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");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)