Posts

Showing posts with the label spring

C# Define and execute bean from the spring configuration file

Create spring configuration file spring.xml with a sample bean definition <? xml version="1.0" encoding="utf-8" ?> < objects xmlns = "http://www.springframework.net" > < object id = "MyClass" type = "MyCompany.MyClass,MyCompany" > < property name = "name" value = "John" /> </ object > </ objects > Use XmlApplicationContext to access bean namespace MyCompany { [TestFixture] public class Test { public void Test1() { IApplicationContext context = new XmlApplicationContext(" spring.xml "); IObjectFactory factory = context; MyClass obj = (MyClass)factory[" MyClass "]; obj.Test(); } } public class MyClass { private string name; public string Name { get { return name; } ...

Using Spring.NET to load data to DataTable

public class Program { static void Main( string [] args) { IDbProvider provider = new DbPath(" server ", " database ").DbProvider; TestDao dao = new TestDao(provider); DataTable dt = dao.Select(); foreach (DataRow row in dt.Rows) { Console.WriteLine( string .Format(" {0}: {1} ",row[" Id "],row[" Name "])); } Console.ReadLine(); } } class DbPath { private const string connectionString = @" Persist Security Info=False; Integrated Security=SSPI; Server={0}; Database={1}; Connect Timeout={2} "; private readonly string server; private readonly string database; private int timeout = 30; public DbPath( string server, string database) { this .server = server; this .database = database; } public string ConnectionString { get { return string .Format(connectionString,server,database,timeout); } } public IDbProvider Db...