163 lines
3.3 KiB
C#
163 lines
3.3 KiB
C#
using System;
|
||
using System.Data ;
|
||
using System.Data .OleDb ;
|
||
using System.Collections ;
|
||
using System.Windows .Forms ;
|
||
namespace MailManageSystem.RegisterAccount
|
||
{
|
||
/// <summary>
|
||
/// AccountOperate 的摘要说明。
|
||
/// </summary>
|
||
public class AccountOperate
|
||
{
|
||
//定义一个连接数据库的字符串
|
||
string conn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"\\Manage.mdb";
|
||
|
||
public AccountOperate()
|
||
{
|
||
//
|
||
// TODO: 在此处添加构造函数逻辑
|
||
//
|
||
}
|
||
|
||
/// <summary>
|
||
/// 向数据库中插入数据
|
||
/// </summary>
|
||
/// <param name="insert"></param>
|
||
|
||
public void AddDelAccount(string insert)
|
||
{
|
||
OleDbConnection myconn=new OleDbConnection (conn);
|
||
myconn.Open ();
|
||
OleDbCommand cmd=new OleDbCommand(insert,myconn);
|
||
try
|
||
{
|
||
cmd.ExecuteNonQuery ();
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show (ex.ToString ());
|
||
}
|
||
finally
|
||
{
|
||
myconn.Close ();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选中的帐户的相关信息
|
||
/// </summary>
|
||
/// <param name="sql">选择的语句</param>
|
||
/// <returns></returns>
|
||
public string[] AccountFileInfo(string sql)
|
||
{
|
||
string []AccountInfo=new string [5];
|
||
OleDbConnection myconn=new OleDbConnection (conn);
|
||
myconn.Open ();
|
||
try
|
||
{
|
||
OleDbCommand mycmd=new OleDbCommand(sql,myconn);
|
||
OleDbDataReader myread=mycmd.ExecuteReader ();
|
||
while(myread.Read ())
|
||
{
|
||
AccountInfo[0]=myread["UserName"].ToString ();
|
||
AccountInfo[1]=myread["Passw"].ToString ();
|
||
AccountInfo[2]=myread["MailAddress"].ToString ();
|
||
AccountInfo[3]=myread["Smtp"].ToString ();
|
||
AccountInfo[4]=myread["port"].ToString ();
|
||
}
|
||
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show (ex.ToString ());
|
||
}
|
||
finally
|
||
{
|
||
myconn.Close ();
|
||
}
|
||
return AccountInfo;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从数据库中读出需要的数据(主要用于显示所有的帐户)
|
||
/// </summary>
|
||
/// <param name="sql">执行的语句</param>
|
||
/// <returns></returns>
|
||
|
||
public ArrayList ReadFromDataBase(string sql,string parameter)
|
||
{
|
||
ArrayList array=new ArrayList ();
|
||
OleDbConnection myconn=new OleDbConnection (conn);
|
||
myconn.Open ();
|
||
try
|
||
{
|
||
OleDbCommand mycmd=new OleDbCommand(sql,myconn);
|
||
OleDbDataReader myread=mycmd.ExecuteReader ();
|
||
while(myread.Read ())
|
||
{
|
||
array.Add (myread[parameter]);
|
||
}
|
||
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
MessageBox.Show (ex.ToString ());
|
||
}
|
||
finally
|
||
{
|
||
myconn.Close ();
|
||
}
|
||
return array;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 此函数的作用是来判断用户名是否已经被使用,调用的方法是通过Sql语句,以及一个字符串变量
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <param name="username"></param>
|
||
/// <returns></returns>
|
||
|
||
public bool CheckAccount(string sql,string username)
|
||
{
|
||
// 设置标志位用来标识是否被使用
|
||
|
||
|
||
bool state=false;
|
||
OleDbConnection myconn=new OleDbConnection (conn);
|
||
myconn.Open ();
|
||
OleDbCommand cmd2=new OleDbCommand(sql,myconn);
|
||
OleDbDataReader myread=cmd2.ExecuteReader ();
|
||
|
||
if(username!="")
|
||
{
|
||
try
|
||
{
|
||
while(myread.Read ())
|
||
{
|
||
//读取用户名来和用户输入的注册名匹配,如果匹配设置标志位为真
|
||
if(myread.GetString (0).Trim()==username)
|
||
{
|
||
state=true;
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|
||
catch(Exception E)
|
||
{
|
||
Console.WriteLine (E.ToString ());
|
||
}
|
||
finally
|
||
{
|
||
myread.Close ();
|
||
myconn.Close ();
|
||
}
|
||
}
|
||
return state;
|
||
}
|
||
}
|
||
}
|
||
|
||
|