.NET
Witam Na blogu będę przedstawiał rozwiązania problemów dotyczących programowania na platformie .NET, których nie udało mi się znaleźć w Internecie. To jest mój pierwszy blog a moja przygoda z programowaniem trwa dopiero 1,5 roku więc poprawiajcie mnie jeżeli będziecie widzieć błędy.
piątek, 11 marca 2011
czwartek, 23 grudnia 2010
Connecting to different SQL Server (T-SQL)
If you working on one database and you want to do something on another database you have to create a linked server. To create linked server use sp_addlinkedserver stored procedure (more info)
After you executed obove query you can check if linked server was added correctly. Using sp_linkedservers sp you should see :
Now you can quering linked server, for example :
If you want to call remotly stored procedure you must execute these two statments too :
EXEC sp_addlinkedserver
@server = 'linkedServerName',
@srvproduct = '',
@provider = 'MSDASQL',
@provstr = 'DRIVER={SQL Server};SERVER=servername\instancename;DATABASE=databeseName;UID=login;PWD=password;';
After you executed obove query you can check if linked server was added correctly. Using sp_linkedservers sp you should see :
Now you can quering linked server, for example :
select * FROM linkedServerName.master.dbo.sysdatabases
If you want to call remotly stored procedure you must execute these two statments too :
exec sp_serveroption @server='linkedServerName', @optname='rpc', @optvalue='true'
exec sp_serveroption @server='linkedServerName', @optname='rpc out', @optvalue='true'
wtorek, 6 kwietnia 2010
Replacing windows notpad with notpad2
Is simple way to replace windows notepad with notepad2 (4.1.24 or newer), you have to create and run that script batch:
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v "Debugger" /t REG_SZ /d "\"%~dp0Notepad2.exe\" /z" /f
piątek, 11 września 2009
Linq To Entity - tips
I show you a few tips, which can make your work with linq to entity easier.
This is hypothetical datebase schema, it will be usefull for explaining tips.
Below you see the same datebase in Entity Data Model.
Simple way to get persons and their interests is:
...
If you want to uptate interests for person you don't have to query against database, for example:
You can do that:
Also you can update Jobs in this way:
This is hypothetical datebase schema, it will be usefull for explaining tips.
Below you see the same datebase in Entity Data Model.
Simple way to get persons and their interests is:
public class PersonInterests
{
public string Person { get; set; }
public List<string> Interest { get; set; }
}
...
testEntities te = new testEntities();
List<PersonInterests> persons = new List<PersonInterests>();
foreach (var item in te.Persons.Include("Jobs"))
{
var interests = te.Interests.
Where(p => p.Persons.Any(a => a.Id == item.Id)).
Select(r => r.Interest).
ToList<string>();
persons.Add(new PersonInterests()
{
Interest = interests,
Person = item.Name,
Id=item.Id,
Job = item.Jobs.Name
});
}
...
If you want to uptate interests for person you don't have to query against database, for example:
...
Persons person = te.Persons.Where(i => i.Id == personID).FirstOrDefault();
person.Interests.Clear(); //clear all interests
//add interest where id=2
person.Interests.Add(te.Interests.Where(i=>i.Id==2).FirstOrDefault());
te.SaveChanges();
...
You can do that:
...
Persons person = te.Persons.Where(i => i.Id == aa.Id).FirstOrDefault();
person.Interests.Clear();
Interests inte = new Interests() { Id = 2 };
te.AttachTo("Interests", inte);
person.Interests.Add(inte);
....
Also you can update Jobs in this way:
...
//change job on that with Id=3
person.JobsReference.EntityKey = new EntityKey("testEntities.Jobs", "Id", 3);
te.SaveChanges();
środa, 5 sierpnia 2009
Add ELMAH to MVC application
ELMAH (Error Logging Modules And Handlers) is the way logging runtime errors in a production environment. ELAMAH is open source library.
In few steps I show how add this library to mvc application.
1. Add elmah.dll assembly to your project. Assembly file you can find in ELMAH website
2.Registering ELMAH's HTTP Module and Hander
You must register ELMAH in web application configuration.
Start by adding to your section in web.config ErrorLogModule:
Next add to section Elmah module:
Next add to section following module and handler:
3. Configure ELMAH
In web.config you must defined:
In few steps I show how add this library to mvc application.
1. Add elmah.dll assembly to your project. Assembly file you can find in ELMAH website
2.Registering ELMAH's HTTP Module and Hander
You must register ELMAH in web application configuration.
Start by adding to your
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
Next add to
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
Next add to
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
...
<add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
</modules>
<handlers>
...
<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
3. Configure ELMAH
In web.config you must defined
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
...
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
</sectionGroup>
</configSections>
Next addsection:
<?xml version="1.0"?>
<configuration>
...
<elmah>
<security allowRemoteAccess="0" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ReviewsConnectionString" />
</elmah>
...
</configuration>
...
</configuration>
środa, 1 kwietnia 2009
Microsoft Windows Search API
Aby móc korzystać w aplikacjach z wbudowanego w Windows search'a należy do projetu dodać referencje Microsoft.Search.Interop. Można ją pobrać wraz z przykładowymi aplikacjami tutaj.
Przeszukiwanie zródeł lokalnych polega na przeszukiwaniu przy pomocy OleDbConnection katalogu SystemIndex. Poniżej zamieszam przykład zastosowania
Przeszukiwanie zródeł lokalnych polega na przeszukiwaniu przy pomocy OleDbConnection katalogu SystemIndex. Poniżej zamieszam przykład zastosowania
CSearchManager manager = new CSearchManager();
// the SystemIndex catalog is the default catalog that windows uses
CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
// get the ISearchQueryHelper which will help us to build SQL necessary to query the indexer
CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
//we get which column we can obtain (in this example file path and file name)
queryHelper.QuerySelectColumns = "System.ItemPathDisplay,System.FileName";
string dir = @"C:\" //path in which we want search
// then we add a scope in format of
queryHelper.QueryWhereRestrictions = "AND scope='file:" + dir + "' AND Contains(System.ItemType,'\".pdf\"') ";//we can search only specyfied type of file in this example pdf
// Generate SQL from our parameters
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(searchPhraze);
// --- Perform the query ---
// create an OleDbConnection object which connects to the indexer provider with the windows application
System.Data.OleDb.OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString);
// open it
conn.Open();
// now create an OleDB command object with the query we built above and the connection we just opened.
OleDbCommand command = new OleDbCommand(sqlQuery, conn);
// execute the command, which returns the results as an OleDbDataReader.
OleDbDataReader WDSResults = command.ExecuteReader();
while (WDSResults.Read())
{
string filePath = WDSResults.GetString(0);
string filename = WDSResults.GetString(1);
});
}
WDSResults.Close();
conn.Close();
wtorek, 31 marca 2009
Create custom ActionInvoker
W skrócie ControllerActionInvoker jest wywoływany przez kontroler, aby wykonać akcję.
Wiecej informacji na ten temat tutaj
Tworzymy własny ActionInvoker:
Tworzymy własny ControllerFactory. Przeładowywujemy funkcję GetControllerInstance, w której przypisujemy instancję własnego invoker'a.
CustomControllerFactory rejestrujemy w global.asax.cs
Wiecej informacji na ten temat tutaj
Tworzymy własny ActionInvoker:
/// <summary>
/// Custom Action Invoker. The ControllerActionInvoker is responsible for creating the parameters that are passed to a controller action.
/// </summary>
public class ContextActionInvoker : ControllerActionInvoker
{
public ContextActionInvoker(ControllerContext controllerContext)
: base()
{
}
/// <summary>
/// Invokes the specified action. Override to log browsing.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="actionName">The name of the action.</param>
/// <returns></returns>
public override bool InvokeAction(ControllerContext controllerContext, string actionName)
{
HttpContextBase context = controllerContext.HttpContext;
//Do sth
return base.InvokeAction(controllerContext, actionName);
}
}
Tworzymy własny ControllerFactory. Przeładowywujemy funkcję GetControllerInstance, w której przypisujemy instancję własnego invoker'a.
/// <summary>
/// Custom Controller Factory
/// </summary>
public class ContextControllerFactory : DefaultControllerFactory
{
/// <summary>
/// Gets the controller instance. Override to put down custom ActionInvoker to ActionInvoker
/// </summary>
/// <param name="controllerType">Type of the controller.</param>
/// <returns>A reference to the controller.</returns>
protected override IController GetControllerInstance(Type controllerType)
{
IController controller = base.GetControllerInstance(controllerType);
Controller contextController = controller as Controller;
if (contextController != null)
{
var context = new ControllerContext(this.RequestContext, contextController);
contextController.ActionInvoker = new ContextActionInvoker(context);
}
return controller;
}
}
CustomControllerFactory rejestrujemy w global.asax.cs
protected void Application_Start()
{
//Register new Controller Factory
ControllerBuilder.Current.SetControllerFactory(typeof(ContextControllerFactory));
RegisterRoutes(RouteTable.Routes);
}
Subskrybuj:
Posty (Atom)