C#数据库操作-创新互联

using Sqystem.Data;
using System.Data.SqlClient;
using System.Configuration;
//引用一个库Ststem.configuration
//修改根目录的web.config文件
<configuration>//最外层节点
......
<connectionStrings>
<add name="connString" connectionString = "Sqerver=.;DataBase=StudentManage;Uid=sa;Pwd=scale2018@">
</connectionStrings>
......
</configuration>
//数据库通用访问类
class  SQLHelper
{
	//数据库连接字符串
	private  static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();

	//写入日志文件
	public  void  WriteLog(string msg)
	{
		FileStream fs = FileStream("1.log",fileMode.Append);
		StreamWriter sw = new StreamWriter(fs);
		sw.writeLine(DateTime.Now.ToString()+" "+ msg);
		sw.Close();
		fs.Close();
	}
	//数据库更新
	public static int Update(string sql)
	{
		SqlConnection conn = new SqlConnection(connString);
		SqlConnection cmd = new SqlCommand(sql,conn);
		try
		{
			conn.Open();
			return  cmd.ExecuteNoQuery();
		}
		catch(Exception ex)
		{
			WriteLog("执行更新时发生异常"+ex.Message);
			throw ex;
		}
		finally
		{
			conn.Close();
		}
	}
	public  static object GetSingleResult(string sql)
	{
		Sqlconnection conn = new SqlConnection(connString);
		SqlCommand  cmd = new SqlCommand(sql,conn);
		try
		{
			conn.Open();
			return cmd.ExecuteScalar();
		}
		catch(Exception ex)
		{
			WriteLog("执行单一结果查询"+ex.message);
			throw ex;
		}
		finally
		{
			conn.Close();
		}
	}
	//返回结果集
	public  static SqlDataReader  GetReader(string sql)
	{
		SqlConnection conn = new SqlConnection(connString);
		SqlCommand    cmd  = new SqlCommand(sql,conn);
		try
		{
			conn.Open();
			retrun cmd.ExecuteReader(CommandBehavior.CloseConnection);//不能在此关闭连接
		}
		catch(Exception ex)
		{
			WriteLog("读取结果集发生异常"+ex.message);
			throw ex;
		}
	}

	//带参数的存储过程
	public static int Update(string sqlOrProcedureName,SqlParameter[]param,bool isProcedure)
	{
		SqlConnection conn = new SqlConnection(connString);
		SqlConnection cmd = new SqlCommand(sqlOrProcedureName,conn);
		if(isProcedure)
		{
			 cmd.CommandType = CommmandType.StoredProcedure;
		}
		try
		{
			conn.Open();
			cmd.Parameters.AddRang(param);
			return  cmd.ExecuteNoQuery();
		}
		catch(Exception ex)
		{
			WriteLog("执行更新时发生异常"+ex.Message);
			throw ex;
		}
		finally
		{
			conn.Close();
		}
	}
}

//DAL调用数据访问模块

创新互联是一家集网站建设,吴川企业网站建设,吴川品牌网站建设,网站定制,吴川网站建设报价,网络营销,网络优化,吴川网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
class AdminService
{
	public UseInfo Login(UseInfo objUser)
	{
		string sql = "select UserName from UserTable where loginid=@LoginId and LoginPwn=@LoginPwn";
		SqlParameter[] param = new SqlParameter[];
		{
			new SqlPrameter("@LoginId",UseInfo.LoginId),
			new SqlParameter("@LoginPwd",UseInfo.LoginPwd)
		};
		try
		{
			SqlDataReader objReader = SQLHelper.GetReader(sql,param,false);
			if(objReader.Read())
			{
				objUser.UserName = objReader["UserName"].ToString();
			}
			else
			{
				objUser = null;
			}
			objReader.Close();
		}
		catch(Exception ex)
		{
			throw new  Exception("用户登录异常"+ex.message);
		}
		return  objUser;
	}
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

标题名称:C#数据库操作-创新互联
标题URL:https://www.cdcxhl.com/article44/cejohe.html

成都网站建设公司_创新互联,为您提供定制网站做网站服务器托管网站建设App开发商城网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管