博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

Spring.net 获取SQL连接字符串

Posted on 2012-06-26 16:03  PHP-张工  阅读(825)  评论(0编辑  收藏  举报

Spring.net 获取SQL连接字符串

Spring.Context.IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext();
IBatisNet.DataMapper.SqlMapper sqlMap = (IBatisNet.DataMapper.SqlMapper)ctx.GetObject("sqlMap");
string conString = sqlMap.DataSource.ConnectionString;
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(conString))
{
    conn.Open();
    string cmdText = "SELECT 1";
    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(cmdText, conn);
    cmd.ExecuteNonQuery();
}