SQL更新无法更新数据库

时间:2017-05-31 04:03:36

标签: c# sql

这是我的数据更新代码。但它没有在我的数据库中更新。不知道为什么。任何建议请。 我检查了数据库连接,它运行正常。我没有使用{..}声明连接字符串。 实际上我没有收到插入的任何错误消息。我收到了一条记录更新消息。但是在我的数据库中,它没有更新。

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


namespace StudentDataDisplay2
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=TestData;Integrated Security=True");

        public Form1()
        {
            InitializeComponent();
            this.Text = "Student Data Display Form";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void display_data()
        {
            conn.Open();//establish connection
            SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandType = CommandType.Text();
            cmd.CommandText = "SELECT * from StudentDetails";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;

            conn.Close();

        }
        private void btnInsert_Click(object sender, EventArgs e)
        {
            conn.Open();//establish connection
            SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandType = CommandType.Text();
            cmd.CommandText = "INSERT INTO StudentDetails VALUES (@Name,@Subject)";
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text; //add values in textbox1 and store in db
            cmd.Parameters.Add("@Subject", SqlDbType.NVarChar).Value = textBox2.Text; //add values in textbox2 and store in db
            cmd.ExecuteNonQuery();
            conn.Close();

            display_data();

            MessageBox.Show("Record added");
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            display_data();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn.Open();//establish connection
            SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandType = CommandType.Text();
            cmd.CommandText = "DELETE FROM StudentDetails WHERE Name= @Name";
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text; //add values in textbox1 and store in db
            cmd.ExecuteNonQuery();
            conn.Close();

            display_data();

            MessageBox.Show("Record deleted");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //conn.Open();//establish connection
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "UPDATE StudentDetails SET Name = @Name WHERE Subject = @Subject";
            cmd.Parameters.AddWithValue("@Name", textBox1.Text);
            cmd.Parameters.AddWithValue("@Subject", textBox2.Text);

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

            display_data();

            MessageBox.Show("Record updated!");
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            conn.Open();//establish connection
            SqlCommand cmd = conn.CreateCommand();
            //cmd.CommandType = CommandType.Text();
            cmd.CommandText = "DELETE FROM StudentDetails WHERE Name= @Name";
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textBox1.Text; //add values in textbox1 and store in db
            cmd.ExecuteNonQuery();
            conn.Close();


            display_data();

            MessageBox.Show("Search completed!");
        }
    }
}

0 个答案:

没有答案