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

时间:2017-06-29 11:43:16

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

3 个答案:

答案 0 :(得分: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");
            }
        }
    }

答案 1 :(得分:2)

您需要实际运行INSERT命令。查看SqlCommand类上的ExecuteNonQuery方法。

答案 2 :(得分:0)

EXECUTE命令缺失

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