用于调用数据库中的写好的存储过程,这里的例子没有带参数,如果要带参数也可以用于登录时与数据库互动性的登录窗口,这样安全性要好,下次再实验一下:

点击(此处)折叠或打开

  1. app.config中定义连接字符串
  2.    <!-- 数据库连接字符串-->
  3. <configuration>
  4.  <appSettings>
  5.       <add key="Helpdeskstr" value="server=(local);User ID=sa;Password=;database=Test" />
  6. </appSettings>
  7.    
  8. </configuration>


  9. string con = ConfigurationManager.ConnectionStrings["Helpdeskstr"].ConnectionString;

  10.             string sql = "del_uninst_softlist"; //要调用的存储过程名
  11.             SqlConnection constr = new SqlConnection(con); //SQL数据库连接对象,以数据库链接字符串为参数
  12.             DialogResult dr = MessageBox.Show("你确定删除残留软件吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  13.             try
  14.             {
  15.                 if (dr == DialogResult.OK)
  16.                 {
  17.                     SqlCommand comstr = new SqlCommand(sql, constr); //SQL语句执行对象,第一个参数是要执行的语句,第二个是数据库连接对象
  18.                     comstr.CommandType = CommandType.StoredProcedure; //要使用的是存储过程,所以设置执行类型为存储过程
  19.                     constr.Open(); //打开数据库连接
  20.                     comstr.ExecuteNonQuery();//执行存储过程
  21.                     MessageBox.Show("删除残留软件已经完成!");
  22.                 }
  23.                 else
  24.                 {
  25.                     return;
  26.                 }
  27.             }
  28.             catch(Exception ex)
  29.             {
  30.                 MessageBox.Show(ex.Message);
  31.             }
  32.             finally
  33.             {
  34.                 constr.Close(); //关闭连接
  35.             }

以下是写的与数据库调用存储过程的账号更新与建立:这里自己写的一个方法,好像没有校验登录方法

点击(此处)折叠或打开

  1. public static int AccountRegister(string accountName, string password,string firstname,string position,string phone)
  2.         {
  3.             //string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  4.             string connstr = configxml.xmlconn.readxml();
  5.             SqlConnection conn = new SqlConnection(connstr);
  6.             string bytePassword = Encrypt.GetMD5_32(password);//使用32位MD5加密
  7.             conn.Open();

  8.             SqlCommand cmd = new SqlCommand("Account_Add", conn);
  9.             cmd.CommandType = CommandType.StoredProcedure;
  10.             SqlParameter prmAccountName = cmd.Parameters.Add(new SqlParameter("@accountName", SqlDbType.NVarChar, 50));
  11.             prmAccountName.Value = accountName;
  12.             SqlParameter prmPassword = cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
  13.             prmPassword.Value = bytePassword;
  14.             SqlParameter prmFirstName = cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 50));
  15.             prmFirstName.Value = firstname;
  16.             SqlParameter prmPosition = cmd.Parameters.Add(new SqlParameter("@position", SqlDbType.NVarChar, 50));
  17.             prmPosition.Value = position;
  18.             SqlParameter prmPhone = cmd.Parameters.Add(new SqlParameter("@phone", SqlDbType.NVarChar, 50));
  19.             prmPhone.Value = phone;
  20.             int myInt = cmd.ExecuteNonQuery();
  21.             cmd.Dispose();
  22.             conn.Close();
  23.             return myInt;
  24.         }
  25.         //更新账户信息
  26.         public static int AccountUpdate(string accountName, string password)
  27.         {
  28.             //string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  29.             string connstr = configxml.xmlconn.readxml();
  30.             string bytePassword = Encrypt.GetMD5_32(password);
  31.             SqlConnection conn = new SqlConnection(connstr);
  32.             conn.Open();
  33.             SqlCommand cmd = new SqlCommand("Account_Update",conn);
  34.             cmd.CommandType = CommandType.StoredProcedure;
  35.             SqlParameter prmAccountName = cmd.Parameters.Add(new SqlParameter("@accountName", SqlDbType.NVarChar, 50));
  36.             prmAccountName.Value = accountName;
  37.             SqlParameter prmPassword = cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
  38.             prmPassword.Value = bytePassword;
  39.             int myInt = cmd.ExecuteNonQuery();
  40.             cmd.Dispose();
  41.             conn.Close();
  42.             return myInt;
  43.         }


09-22 02:40