用于调用数据库中的写好的存储过程,这里的例子没有带参数,如果要带参数也可以用于登录时与数据库互动性的登录窗口,这样安全性要好,下次再实验一下:
-
app.config中定义连接字符串
-
<!-- 数据库连接字符串-->
-
<configuration>
-
<appSettings>
-
<add key="Helpdeskstr" value="server=(local);User ID=sa;Password=;database=Test" />
-
</appSettings>
-
-
</configuration>
-
-
-
string con = ConfigurationManager.ConnectionStrings["Helpdeskstr"].ConnectionString;
-
-
string sql = "del_uninst_softlist"; //要调用的存储过程名
-
SqlConnection constr = new SqlConnection(con); //SQL数据库连接对象,以数据库链接字符串为参数
-
DialogResult dr = MessageBox.Show("你确定删除残留软件吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
-
try
-
{
-
if (dr == DialogResult.OK)
-
{
-
SqlCommand comstr = new SqlCommand(sql, constr); //SQL语句执行对象,第一个参数是要执行的语句,第二个是数据库连接对象
-
comstr.CommandType = CommandType.StoredProcedure; //要使用的是存储过程,所以设置执行类型为存储过程
-
constr.Open(); //打开数据库连接
-
comstr.ExecuteNonQuery();//执行存储过程
-
MessageBox.Show("删除残留软件已经完成!");
-
}
-
else
-
{
-
return;
-
}
-
}
-
catch(Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
finally
-
{
-
constr.Close(); //关闭连接
-
}
以下是写的与数据库调用存储过程的账号更新与建立:这里自己写的一个方法,好像没有校验登录方法
-
public static int AccountRegister(string accountName, string password,string firstname,string position,string phone)
-
{
-
//string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
-
string connstr = configxml.xmlconn.readxml();
-
SqlConnection conn = new SqlConnection(connstr);
-
string bytePassword = Encrypt.GetMD5_32(password);//使用32位MD5加密
-
conn.Open();
-
-
SqlCommand cmd = new SqlCommand("Account_Add", conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
SqlParameter prmAccountName = cmd.Parameters.Add(new SqlParameter("@accountName", SqlDbType.NVarChar, 50));
-
prmAccountName.Value = accountName;
-
SqlParameter prmPassword = cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
-
prmPassword.Value = bytePassword;
-
SqlParameter prmFirstName = cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 50));
-
prmFirstName.Value = firstname;
-
SqlParameter prmPosition = cmd.Parameters.Add(new SqlParameter("@position", SqlDbType.NVarChar, 50));
-
prmPosition.Value = position;
-
SqlParameter prmPhone = cmd.Parameters.Add(new SqlParameter("@phone", SqlDbType.NVarChar, 50));
-
prmPhone.Value = phone;
-
int myInt = cmd.ExecuteNonQuery();
-
cmd.Dispose();
-
conn.Close();
-
return myInt;
-
}
-
//更新账户信息
-
public static int AccountUpdate(string accountName, string password)
-
{
-
//string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
-
string connstr = configxml.xmlconn.readxml();
-
string bytePassword = Encrypt.GetMD5_32(password);
-
SqlConnection conn = new SqlConnection(connstr);
-
conn.Open();
-
SqlCommand cmd = new SqlCommand("Account_Update",conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
SqlParameter prmAccountName = cmd.Parameters.Add(new SqlParameter("@accountName", SqlDbType.NVarChar, 50));
-
prmAccountName.Value = accountName;
-
SqlParameter prmPassword = cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
-
prmPassword.Value = bytePassword;
-
int myInt = cmd.ExecuteNonQuery();
-
cmd.Dispose();
-
conn.Close();
-
return myInt;
-
}
阅读(2952) | 评论(0) | 转发(0) |