• sql连接及操作


                   string userid = string.Empty;
                   string roseid = string.Empty;
                   SqlConnection connect = new SqlConnection("server=192.168.0.1;uid=sa;pwd=sa;database=data1");
                    connect.Open();
                    //获取userid
                    SqlCommand ComUserid = new SqlCommand("select userid from aspnet_Users where username='" + name + "'", connect);
                    SqlDataReader dr = ComUserid.ExecuteReader();
                    if (dr.Read())
                    {
                        userid = dr["userid"].ToString();
                    }
                    dr.Close();
                    dr.Dispose();
                    ComUserid.Dispose();
                    SqlCommand ComDelete = new SqlCommand("delete from [aspnet_UsersInRoles] where userid = '" + userid + "'", connect);
                    ComDelete.ExecuteNonQuery();//删除aspnet_UsersInRoles表中所有此用户得所有角色.
                    ComDelete.Dispose();
                 try
                {
                    //获取选择得rolesid,更新角色.
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        if (CheckBoxList1.Items[i].Selected)
                        {
                            string rosename = CheckBoxList1.Items[i].Text.ToString();
                           SqlCommand ComRosid = new SqlCommand("select roleid from aspnet_Roles where rolename='" + rosename + "'", connect);
                            SqlDataReader drRid = ComRosid.ExecuteReader();
                            if (drRid.Read())
                            {
                                roseid = drRid["roleid"].ToString();
                            }
                            drRid.Close();
                            drRid.Dispose();
                            ComRosid.Dispose();
                            string aa = "insert into [aspnet_UsersInRoles] [userid],[roleid] values('" + userid + "','" + roseid + "')";
                            SqlCommand cominsert = new SqlCommand("insert into [aspnet_UsersInRoles] ([userid],[roleid]) values('" + userid + "','" + roseid + "')", connect);
                            cominsert.ExecuteNonQuery();
                            cominsert.Dispose();
                         
                        }
                    }
                    connect.Close();//关闭数据库.
                    connect.Dispose();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
  • 相关阅读:
    Arm的几种工作模式
    Linux 串口驱动初始化前置定位crash log
    GPS的经纬度格式及转换
    git 常用命令
    windows 下 mongodb studio 3t 破解无限使用脚本
    从头开始 一、第一天
    十九、IDEA的pom文件手动添加依赖
    十八、{ "a", "ab", "ac", "abc", "de", "bc", "e" }不使用Arrays.sort()进行排序
    十七、Java中数组常见的几种排序方法!
    十六、IDEA创建一个maven工程
  • 原文地址:https://www.cnblogs.com/qfb620/p/1082633.html
Copyright © 2020-2023  润新知