Db.Open(s) works if you have added a connectionstring in your web.config with that name (umbraco_db)
You can specify regular connection string using OpenConnectionString instead. And if you for example want to re-use the umbraco connection string you can do:
var umbracoDbConnectionString = System.Configuration.ConfigurationManager.AppSettings["umbracoDbDSN"];
using (var db = Database.OpenConnectionString(umbracoDbConnectionString,"System.Data.SqlClient"))
{
db...
}
Denis, the point is that yes of course you can do this - it doesn't mean it will automatically add all required dependencies and configure connection strings for you. You still have to write the code properly, add the dependencies and configure any configurables.
Just pasting that code into a template Console application wouldn't work, either, until you managed it accordingly.
But the answer is still yes: if you know what you're doing, yes you can do this.
System.ArgumentException: The relative virtual path 'usercontrols/aaSystemLogVisitors.ascx' is not allowed here. at System.Web.VirtualPath.FailIfRelativePath() at System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) at System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPath virtualPath) at System.Web.Hosting.HostingEnvironment.MapPath(VirtualPath virtualPath) at umbraco.IO.IOHelper.MapPath(String path, Boolean useHttpContext) at umbraco.IO.IOHelper.ValidateEditPath(String filePath, String validDir) at umbraco.developer.assemblyBrowser.Page_Load(Object sender, EventArgs e)
At this time, i want to develop simple script, at any way (c# script or razor) for log visitor page views ...
You need to add the WebMatrix.Data.dll to your /bin
That said, many prefer PetaPoco over WebMatrix.Data. I know I do. And since you dont already have WebMatrix.Data it's just as simple to add PetaPoco instead:
@{ var myDb = new PetaPoco.Database(my-connection-string-name); var keyName = "PageCounter"; myDb.Execute("UPDATE MyStatisticsTable SET SomeValue=SomeValue+1 WHERE SomeKey=@0",keyName); var counter = myDb.ExecuteScalar("SELECT SomeValue FROM MyStatisticsTable WHERE SomeKey=@0",keyName) } <p>Page counter: @counter</p>
or
@{ var myDb = new PetaPoco.Database(connectionstring); var someResult = myDb.Fetch("SELECT * FROM MyTable WHERE SomeColumn=@0", "Some value"); }
To my knowledge (whch isn't that much when it comes to Razor, granted) you can't add runat=server scripts like that and it would seem pointless because it's mixing the statelessness of ASP.NET MVC with the state-maintainedness of ASP.NET WebForms, but you can define a 'functions' section to house any number of methods then have the body or other sections utilise them.
Because Umbraco does allow you to mix the two, you can do this in your Templates.
I came across this post when looking for the simple way to make calls to an external database with v7.2, even though its dated and i wasnt sure if putting petapoco.cs inside the App_Code will create a conflict (petapoco is built-in now i believe),
I have simply copied the petapoco.cs to appcode and use .Fetch
Not sure if its still relavant, like creating a potential conflict on PetaPoco that ships with Umbraco 7.1 or 7.2, but it works!
Razor ans SQL commands
Is it possible with razor scripts manipulating sql data from database ?
Somthing like:
@using something ...
@{
var db = Database.Open("umbraco_db");
var insert = "INSERT INTO umbracoLog somthiing ...";
var select = "SELECT * FROM umbracoLog";
db.Execute(insert);
}
Yes, you can without issues (maybe only a lack of Intellisense). Did you try this?
This example is not working, i use mssql (not compact) database. I have error that Database is not existing.
Is it there any example how can i insert data to database, with connection settings and working commands ?
Please help ...
Db.Open(s) works if you have added a connectionstring in your web.config with that name (umbraco_db)
You can specify regular connection string using OpenConnectionString instead. And if you for example want to re-use the umbraco connection string you can do:
wwwroot\macroScripts\634725060163618433_systemLogVisitors.cshtml(2): error CS0103: The name 'Database' does not exist in the current context
Ah, I thought you already had the WebMatrix.Data, check out http://joeriks.com/2011/01/23/data-the-easy-way-webmatrix-data-in-umbraco/
HTH
Denis, the point is that yes of course you can do this - it doesn't mean it will automatically add all required dependencies and configure connection strings for you. You still have to write the code properly, add the dependencies and configure any configurables.
Just pasting that code into a template Console application wouldn't work, either, until you managed it accordingly.
But the answer is still yes: if you know what you're doing, yes you can do this.
ok, i have allredy custom connection string, but this webmatrix maybe wont work on live win 7 server with mssql !?!
i have try to ad namespace webmatrix, but not found ..
can i ask you one thing more, can i put c# script in cshtml, somthing like;
<script language="cs" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
Response.Write("ASDAASD");
}
</script>
this is also no working ...
in 4.7.2 if ii use ascx i have error message
System.ArgumentException: The relative virtual path 'usercontrols/aaSystemLogVisitors.ascx' is not allowed here. at System.Web.VirtualPath.FailIfRelativePath() at System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) at System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPath virtualPath) at System.Web.Hosting.HostingEnvironment.MapPath(VirtualPath virtualPath) at umbraco.IO.IOHelper.MapPath(String path, Boolean useHttpContext) at umbraco.IO.IOHelper.ValidateEditPath(String filePath, String validDir) at umbraco.developer.assemblyBrowser.Page_Load(Object sender, EventArgs e)
At this time, i want to develop simple script, at any way (c# script or razor) for log visitor page views ...
You need to add the WebMatrix.Data.dll to your /bin
That said, many prefer PetaPoco over WebMatrix.Data. I know I do. And since you dont already have WebMatrix.Data it's just as simple to add PetaPoco instead:
To do that - drop the .cs ( https://github.com/toptensoftware/PetaPoco/blob/master/PetaPoco/PetaPoco.cs ) to your /App_Code folder. Then you can do this in your razor scripts:
or
To my knowledge (whch isn't that much when it comes to Razor, granted) you can't add runat=server scripts like that and it would seem pointless because it's mixing the statelessness of ASP.NET MVC with the state-maintainedness of ASP.NET WebForms, but you can define a 'functions' section to house any number of methods then have the body or other sections utilise them.
Because Umbraco does allow you to mix the two, you can do this in your Templates.
Thank You very much for help mr. Erikkson, Umbraco and Support is THE BEST :-)
cool :)
I edited my answer, and unfortunately some code was removed in the edit. PetaPoco needs some type defs to work properly:
Hi Jonas
I came across this post when looking for the simple way to make calls to an external database with v7.2, even though its dated and i wasnt sure if putting petapoco.cs inside the App_Code will create a conflict (petapoco is built-in now i believe),
I have simply copied the petapoco.cs to appcode and use .Fetch
Not sure if its still relavant, like creating a potential conflict on PetaPoco that ships with Umbraco 7.1 or 7.2, but it works!
Thank you for sharing this!
Thanks mr. Grant
is working on a reply...