在Windows窗体C#中保存对数据库的更改

在Windows窗体C#中保存对数据库的更改

本文介绍了在Windows窗体C#中保存对数据库的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将更改保存到数据库时出现问题,下面是代码的狙击:



在c#类中,ManageUsers.c:

I have a problem with saving the changes to the database, below is a snipe of the code:

In c# class, ManageUsers.c:

private string sConnectionString = ConfigurationSettings.AppSettings["ScreenDB"];

        public void vDeleteUser(string sTagID)
        {
            SqlConnection cnConnection = new SqlConnection(sConnectionString);
            try
            {
                if (cnConnection.State == ConnectionState.Closed)//check the connection
                {
                    //  Open the connection
                    cnConnection.Open();
                }
                //
                //Use the connection
                //

                SqlCommand cmdDelete = new SqlCommand();
                cmdDelete = new SqlCommand("SP_DeleteUser", cnConnection);
                cmdDelete.CommandType = CommandType.StoredProcedure;
                //if (!sTagID.Equals(string.Empty))
                //{
                   cmdDelete.Parameters.Add("@TagID", SqlDbType.VarChar).Value = sTagID;
                //}
                // Get query results

                cmdDelete.ExecuteNonQuery();
            }//end try
            //To handle SQl exception
            catch (SqlException ex)
            {
                MessageBox.Show(ex.ToString());
                           }
            //To handle any type of exception
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());

            }
            finally
            {
                //cnConnection.Close();
            }

        }



在Windows窗体中Form1:


In windows form Form1:

private void button1_Click(object sender, EventArgs e)
        {
           ManageUser oUser = new ManageUser();
           oUser.vDeleteUser("11221");

        }





注意:

存储过程工作正常,我已在数据库服务器上测试过它。



数据库连接很好,因为它在检索数据时连接。同样,它在调用select语句时从数据库中检索数据,因此它已连接。



Notes:
The stored procedure works fine, I've tested it on database server.

The database connection is fine, because it connects when retrieving data. As in, it retrieves data from the database when a select statement is called, so it is connected.

推荐答案


这篇关于在Windows窗体C#中保存对数据库的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:22