数据库表不更新c# [英] database Table is not updating c#

查看:158
本文介绍了数据库表不更新c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有form和dataGridView连接到数据库中的表,
当我添加记录时它显示在dataGridView中,但是当我停止运行时表返回为空!

I have form and dataGridView connected to table in the database, when I add records It shows up in the dataGridView, but when I stop running the table returns empty!

这里是代码:

        string employeeName =  "person";
        string employeeUserName ="person1";
        string emplpyeeNotes = "empty";
        string employeePassword = "123";
        int employeeSalary = 4000;
        try
        {

           SqlConnection cn = new SqlConnection(Properties.Settings.Default.StudentManagementDBConnectionString);
           cn.Open();

           string sql = "INSERT INTO Employee (Name,Salary,UserName,Password,Notes) VALUES(@name, @salary, @userName, @password, @notes)";
           SqlCommand exsql = new SqlCommand(sql, cn);
           exsql.Parameters.AddWithValue("@name", employeeName);
           exsql.Parameters.AddWithValue("@salary", employeeSalary);
           exsql.Parameters.AddWithValue("@userName", employeeUserName);
           exsql.Parameters.AddWithValue("@password", employeePassword);
           exsql.Parameters.AddWithValue("@notes", emplpyeeNotes);
           exsql.ExecuteNonQuery();

           MessageBox.Show("employee added Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
           this.employeeTableAdapter.Fill(this.studentManagementDBDataSet.Employee);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


推荐答案

连接字符串,并通过从数据集的属性中复制它将其设置为mdf文件的路径。

the solve was to change the connection string and set it to path of mdf file by copying it from the properties of the dataset.

这篇关于数据库表不更新c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆