using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace Utility
{
public class SQLHelper
{
//AppSettings
public static string App_SQL_DB = ConfigurationManager.AppSettings["SqlCon"];
//ConnectionStrings
//static string Conn_SQL_DB = ConfigurationManager.ConnectionStrings["SqlCon"].ToString();
/// <summary>
/// 增 删 改方法
/// </summary>
/// <param name="cmdType"></param>
/// <param name="sql"></param>
/// <param name="parms"></param>
/// <returns></returns>
public static int ExecuteNonQuery(CommandType cmdType, string sql, SqlParameter[] parms)
{
int result = -1;
using (SqlConnection con = new SqlConnection(App_SQL_DB))
{
SqlCommand cmd = new SqlCommand();
PrepareSQLCommand(cmdType, sql, parms, con, cmd);
result = cmd.ExecuteNonQuery();
}
return result;
}
/// <summary>
/// 提取共同方法
/// </summary>
/// <param name="cmdType"></param>
/// <param name="sql"></param>
/// <param name="parms"></param>
/// <param name="con"></param>
/// <param name="cmd"></param>
private static void PrepareSQLCommand(CommandType cmdType, string sql, SqlParameter[] parms, SqlConnection con, SqlCommand cmd)
{
cmd.Connection = con;
if (con.State != ConnectionState.Open)
con.Open();
cmd.CommandText = sql;
cmd.CommandType = cmdType;
if (parms != null)
foreach (SqlParameter parm in parms)
{
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="cmdType"></param>
/// <param name="sql"></param>
/// <param name="parms"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string sql, SqlParameter[] parms)
{
SqlConnection con = new SqlConnection(App_SQL_DB);
SqlCommand cmd = new SqlCommand();
PrepareSQLCommand(cmdType, sql, parms, con, cmd);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
/// <summary>
/// 读取单个值
/// 结果集中第一行的第一列;如果结果集为空,则为空引用
/// </summary>
/// <param name="cmdType"></param>
/// <param name="sql"></param>
/// <param name="parms"></param>
/// <returns></returns>
public static int ExecuteScalar(CommandType cmdType, string sql, SqlParameter[] parms)
{
int result = -1;
//连接数据库
using (SqlConnection con = new SqlConnection(App_SQL_DB))
{
//执行
SqlCommand cmd = new SqlCommand();
PrepareSQLCommand(cmdType, sql, parms, con, cmd);
result = (int)cmd.ExecuteScalar();
return result;
}
}
/// <summary>
/// 事务提交多SQL语句的执行
/// <param name="cmdText">多sql语句的数组</param>
/// <returns>true表示成功,false表示失败</returns>
/// </summary>
public static int ExecuteNonQuery(ArrayList cmdText)
{
int flag = -1;
using (SqlConnection connection = new SqlConnection(App_SQL_DB))
{
SqlCommand cmd = new SqlCommand();
//SQLCmd.CommandText = null;
//SQLCmd.CommandType = cmdType;
//PrepareSQLCommand(cmdType, null, parms, connection, SQLCmd);
SqlTransaction Trans;
connection.Open();
Trans = connection.BeginTransaction();
try
{
cmd.Connection = connection;
cmd.Transaction = Trans;
for (int i = 0; i < cmdText.Count; i++)
{
//SQLCmd.CommandText = EscapeString(cmdText[i].ToString());
cmd.CommandText = cmdText[i].ToString();
flag = cmd.ExecuteNonQuery();
}
Trans.Commit();
}
catch (Exception e)
{
Trans.Rollback();
}
finally
{
connection.Close();
}
} return flag;
}
/// <summary>
/// 执行多条SQL语句的方法
/// </summary>
/// <param name="SQLStringList"></param>
public static void ExecuteNonQuerySQL(ArrayList SQLStringList)
{
using (SqlConnection conn = new SqlConnection(App_SQL_DB))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
/// <summary>
/// 转义字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string EscapeString(string str)
{
// str = str.Replace("[lt", "<");
// str = str.Replace("[gt", ">");
return str;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
using MYASP;
using System.Data;
namespace DAL
{
public class UsersDAL
{
/// <summary>
///
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public int UsesLogin(Users user)
{
int result = -1;
string SQL = "select id,username,password from users where password=@Password and username=@Username";
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@Username", user.Username);
param[1] = new SqlParameter("@Password", user.Password);
result = SQLDBHelper.ExecuteScalar(CommandType.Text, SQL, param);
return result;
}
/// <summary>
/// 添加
/// </summary>
/// <returns></returns>
public int AddUsers(Users user)
{
int result = -1;
string sql = "INSERT INTO Users(Username,Password) VALUES(@Username,@Password)";
SqlParameter[] parms = new SqlParameter[15];
parms[0] = new SqlParameter("@Username", user.Username);
parms[1] = new SqlParameter("@Password", user.Password);
result = SQLDBHelper.ExecuteNonQuery(CommandType.Text, sql, parms);
return result;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<!--连接数据库-->
<appSettings>
<add key="SqlCon" value="Data Source=JILONGLIANG\MSSERVER2005;Initial Catalog=CRM;Persist Security Info=True;User ID=sa;Password=admin1988"/>
</appSettings>
<log4net>
<!--定义输出到文件中-->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
<!--日志生成的路径
winform与web的生成日志文件不一样,在生成到项目的日志会生成到项目的Debug目录下
E:\ASP.NET\MyApp\MyApp\bin\Debug
-->
<param name="File" value="log/app"/>
<!--日志追加到文件方式-->
<param name="AppendToFile" value="true"/>
<!--是否按日期滚动生成-->
<param name="RollingStyle" value="Date"/>
<!--
动态生成日志文件的文件类型或是后缀名
<datePattern value="yyyyMMdd-HH:mm:ss" />
-->
<param name="DatePattern" value="yyyyMMdd.LOG"/>
<!--文件名是否动态生成-->
<param name="StaticLogFileName" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<!--<header name="Header" value="[Header] "/>-->
<!--每条日志末尾的文字说明-->
<footer value="==================================日志的结尾======================================================" />
<!--输出格式-->
<!--样例:记录时间:2012-07-26 16:02:53,500 线程ID:[3696] 日志级别:DEBUG 出错类:DAL.UsersDAL property:[(null)] - 错误描述:你引起了一个错误,错误ID为 Debug:1504984943-->
<!--<conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" />-->
<conversionPattern value="%n记录时间:%date%n 线程ID:[%thread] 日志级别:%-5level操作者ID:%property{Operator} 操作类型:%property{Action}%n 当前机器名:%property%n 当前机器名及登录用户:%username %n 记录类:%logger 记录当前类的方法:%location%n 消息描述:%property{Message}%n 异常:%exception %n 异常信息:%message%newline %n%n"/>
</layout>
</appender>
<!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。-->
<root>
<!--文件形式记录日志-->
<appender-ref ref="rollingFile" />
<!--控制台控制显示日志-->
<appender-ref ref="ConsoleAppender" />
<!--Windows事件日志-->
<appender-ref ref="EventLogAppender" />
<!-- 如果不启用相应的日志记录,可以通过这种方式注释掉
<appender-ref ref="AdoNetAppender_Access" />
-->
</root>
</log4net>
</configuration>