在Windows窗体C#中保存对数据库的更改 [英] Save changes to database in windows form C#

查看:263
本文介绍了在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.

推荐答案

如果您可以检索信息但不能插入,更新或删除行,然后要做的第一件事是查看您的连接用户被允许的权限 - 可能是您的用户没有删除权限。



它是为安全目的使用最小可能权限的正常情况:最小特权原则 [ ^ ]
If you can retrieve information but not insert, update or delete rows, then the first thing to do is look at the permissions your connection user has been permitted - it may be that your user does not have delete permission.

It is normal to use the minimum possible permissions for security purposes: Principle of least privilege[^]


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

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