Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

sql - database Table is not updating c#

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!

here is the code:

        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);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using parameterized query like this instead:

string employeeName = textBoxName.Text;
string employeeUserName = textBoxUserName.Text;
string emplpyeeNotes = richTextBoxNotes.Text;
string employeePassword = textBoxPassword.Text;

SqlConnection cn = new SqlConnection(global::StudentManagementProject.Properties.Settings.Default.StudentManagementDBConnectionString);

    try
    {
        string sql = "INSERT INTO Employee (Name,Salary,UserName,Password,Notes) VALUES(@name, @salary, @userName, @password, @notes)";
        SqlCommand exsql = new SqlCommand(sql, cn);
        cn.Open();
        exsql.Parameters.AddWithValue("@name", employeeName);
        exsql.Parameters.AddWithValue("@salary", 2000);
        exsql.Parameters.AddWithValue("@userName", employeeUserName);
        exsql.Parameters.AddWithValue("@password", employeePassword);
        exsql.Parameters.AddWithValue("@notes", emplpyeeNotes);
        int result=exsql.ExecuteNonQuery();
        if (result > 0 )
        {
        MessageBox.Show("employee added Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }else
        { 
          MessageBox.Show("employee insertion failed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        cn.Close();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...