首先Models层模型如下:
QueryUsageLog.cs,UsageLog.cs和UserInfo.cs
public class QueryUsageLog
{
public string SessionId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string MachineName { get; set; }
public string LoginAccount { get; set; }
}
public class UsageLog
{
public int Id { get; set; }
public string SessionId { get; set; }
public DateTime TimeStamp { get; set; }
public string LoginAccount { get; set; }
public string MachineName { get; set; }
public string TypeName { get; set; }
public string MethodName { get; set; }
public string Arguments { get; set; }
public string LogMessage { get; set; }
public string ErrorMessage { get; set; }
}
public class UserInfo
{
public string UserId { get; set; }
public string ProductId { get; set; }
}
DBConfig文件夹全是配置文件,在bin目录
文件夹下有5个配置文件,properties.config是连接字符串属性文件:
providers.config是数据库提供程序配置:
SELECT Top(1) SessionId
FROM MRUsageLog
WHERE LoginAccount = #value#
select
Id,
SessionId,
TimeStamp,
LoginAccount,
MachineName,
TypeName,
MethodName,
Arguments,
LogMessage,
ErrorMessage
from MRUsageLog
GetUsageLog
Usr_GetUserSettings
SqlMap.config全局配置文件:
数据访问层的MyBatisHelper.cs代码:
public sealed class MyBatisHelper
{
private static volatile ISqlMapper mapper = null;
private static string GetConfigPath()
{
//string baseDir = AppDomain.CurrentDomain.BaseDirectory;
string baseDir = Path.GetFullPath(@"../../"); ;
return baseDir + ConfigurationManager.AppSettings["DBConfig"];
}
public static void Configure(object obj)
{
mapper = (ISqlMapper) obj;
}
public static void InitMapper()
{
string configPath = GetConfigPath();
ConfigureHandler hanlder = new ConfigureHandler(Configure);
DomSqlMapBuilder builder = new DomSqlMapBuilder();
mapper = builder.ConfigureAndWatch(configPath, hanlder);
}
public static ISqlMapper Instance
{
get
{
try
{
if (mapper == null)
{
lock (typeof (SqlMapper))
{
if (mapper == null)
{
InitMapper();
}
}
}
return mapper;
}
catch (Exception ex)
{
throw ex;
}
}
}
public static T QueryForObject(string statementName, object parameterObject)
{
T result = default(T);
try
{
result = Instance.QueryForObject(statementName, parameterObject);
return result;
}
catch (Exception ex)
{
throw ex;
}
}
public static T QueryForList(string statementName, object parameterObject)
{
T result = default(T);
try
{
result= Instance.QueryForObject(statementName, parameterObject);
return result;
}
catch (Exception ex)
{
throw ex;
}
}
}
控制台应用程序app.config:
Program.cs的代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using DO.SLM.DataAccess;
using IBatisNet.DataMapper;
using Models;
using log4net.Config;
using System.IO;
namespace ConsoleApplication1
{
public class Program
{
private static void Main(string[] args)
{
var logConfig = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log4net.config");
XmlConfigurator.ConfigureAndWatch(logConfig);
//stADODatabase();
//ar ret = testUserDataBase();
//Console.WriteLine(ret);
testMyBatisHelper();
Console.Read();
}
public static void testMyBatisHelper()
{
string rest = MyBatisHelper.QueryForObject("FindSessionId", "zxi");
var allLog = MyBatisHelper.QueryForList("SelectUsageLog", null);
QueryUsageLog queryUsageLog = new QueryUsageLog
{
StartDate = new DateTime(2012, 9, 1),
EndDate = new DateTime(2016, 9, 15)
};
var queryLog = MyBatisHelper.QueryForList("GetUsageLog", queryUsageLog);
Console.WriteLine("FindSessionId: " + rest);
Console.WriteLine(allLog.SessionId);
Console.WriteLine(queryLog.SessionId);
}
public static string testUserDataBase()
{
UserInfo userInfo = new UserInfo
{
UserId = "09b7e0f3-d9f9-45c3-8f38-52436e85809c",
ProductId = "2262e3b5-8f49-49c2-9a39-5099fc3b1435"
};
string rest = "";
try
{
rest = MyBatisHelper.QueryForObject("GetUserSettings", userInfo);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return rest;
}
public static void testADODatabase()
{
string connString = @"Data Source=Adaccessqadb1;User Id=msdomain1;PWD=1344444;Initial Catalog=Users";
try
{
using (SqlConnection sqlConnection = new SqlConnection(connString))
{
sqlConnection.Open();
SqlCommand cmd = new SqlCommand("dbo.Usr_GetUserSettings", sqlConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserId", "09b7e0f3-d9f9-45c3-8f38-52436e85809c"));
cmd.Parameters.Add(new SqlParameter("@ProductId", "2262e3b5-8f49-49c2-9a39-5099fc3b1435"));
SqlDataReader dbReader = cmd.ExecuteReader();
while (dbReader.Read())
{
Console.WriteLine(dbReader["Settings"]);
}
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
public static ISqlMapper EntityMapper
{
get
{
try
{
//得到由SqlMap.Config文件中定义的SqlMapper的实例。
ISqlMapper mapper = Mapper.Instance();
//第二种方法
//用于创建连接的连接字符串
//mapper.DataSource.ConnectionString = @"data source=qq\SQLEXPRESS;User ID=wu;PWD=11111;Initial Catalog=ManagerResearchLog";
return mapper;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
log4Net.config配置文件放到bin目录下:
结果如图:
USE [ManagerResearchLog]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MRUsageLog](
[Id] [int] IDENTITY(1,1) NOT NULL,
[SessionId] [varchar](50) NULL,
[TimeStamp] [datetime] NOT NULL,
[LoginAccount] [varchar](50) NULL,
[MachineName] [varchar](50) NULL,
[TypeName] [varchar](50) NULL,
[MethodName] [varchar](50) NULL,
[Arguments] [varchar](50) NULL,
[LogMessage] [varchar](200) NULL,
[ErrorMessage] [varchar](200) NULL,
CONSTRAINT [PK__MRUsageL__3214EC077F60ED59] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ__MRUsageL__C9F49291023D5A04] UNIQUE NONCLUSTERED
(
[SessionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO