by Pieter Brinkman
15. January 2009 06:04
You have to specify which related-data you want to retrieve from a object so you can access them outside the Linq data-context. You can achieve this by using the LoadWith method of the DataLoadOptions Class. The LoadWith method accepts an lambda expression that specifies which object you want to retrieve.
In the following example I have a employee table that has a relation with the company table. In my code I want to show the employee with the company name (outside the DataContext).
Employee employee;
using (LinqDataContext db = new (LinqDataContext())
{
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Employee>(e => e.Company);
db.LoadOptions = dlo;
employee = from item in db.Employees
select item).First<Employee>();
}
string companyName = employee.Company.Name;
Because of the DataLoadOptions I can now use the company properties to print the company name outside the DataContext.
Enjoy.
by Pieter Brinkman
13. December 2008 05:35
For a customer I needed to do a review there database (MSSQL 2000 database). For this I wanted to create some database diagrams. When I tried to create a diagram with Sql Sever Management Studio I got the following message:
"Database diagram support objects cannot be installed because this database does not gave a valid owner."
To fix this problem you need to set the owner and change the Compatibility Level of the database. You can do this with the following steps within Sql Sever Management Studio:
1. Right Click on your database, choose properties
2. Goto the Options Page
3. In the Dropdown at right labeled "Compatibility Level" choose "SQL Server 2005(90)"
4. Goto the Files Page
5. Enter "sa" in the owner textbox, or any other user you want to be the owner.
6. Click the OK button
by Pieter Brinkman
2. September 2008 05:33
When I was trying to publish a database to my shared hosting provider I encountered some problems with backup and restoring my database. To restore a database on my provider I needed a full Sql script (schema and data).
After a few attempts with SQL comparer and Management studio I found a link to ‘Microsoft SQL Server Database Publishing Wizard 1.1’. The Publising Wizard does complete backups to SQL script (including data).
When I tried running the Publishing Wizard on my Vista workstation it failed with the following error:
Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I fixed this error by installing the ‘Feature Pack for Microsoft SQL Server 2005 - November 2005’.
The Publishing Wizard a great tool!
by Pieter Brinkman
2. September 2008 05:25
Update 20 may 2009: New easy sollution
Set the Connection -> Application Settings property True. This will generate a connection string in your app.config.
Copy this connection string to your web.config and your all set!
=================================================================
Old post:
If you want to use your connectionstring from the web.config with Linq to Sql (dbml) you have to add the following partial class to your project:
namespace Your.Namespace
{
partial class yourDataContext
{
partial void OnCreated()
{
ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["YourConnectionString"];
if (s != null)
Connection.ConnectionString = s.ConnectionString;
}
}
}
Don't forget to change the Your.Namespace, yourDataContext and YourConnectionString to fit your project.
Hope this helps.
by Pieter Brinkman
26. June 2008 08:03
Execute the next query to enable CLR.
-- Turn advanced options on
EXEC sp_configure 'show advanced options' , '1';
go
reconfigure;
go
EXEC sp_configure 'clr enabled' , '1'
go
reconfigure;
-- Turn advanced options back off
EXEC sp_configure 'show advanced options' , '0';
go
More info about CLR on Microsoft MSDN.
by Pieter Brinkman
1. April 2008 02:27
When using an Linq Datasource (linq to sql) for a datagrid (Gridview, ListView, etc) you don't always want all rows from a table. To exclute data you can use the WhereParameters to add an where statement to your DataSource.
You can do limited where statements with the Visual Studio Wizards. When you need more than a limited statement you can dynamicly create an Parameter to add to your Linq DataSource.
The following examples shows how to filter the Linq Datasource that recovers all rows from the Post table and filters them on the BlogId. The BlogManager.CurrentBlogId gets the blog GUID from the Session.
Parameter whereparam = new Parameter();
whereparam.Name = "BlogId";
whereparam.DefaultValue = BlogManager.CurrentBlogId.ToString();
whereparam.Type = TypeCode.Object;
linqDataSource.WhereParameters.Add(whereparam);
linqDataSource.Where = "BlogId == Guid(@BlogId)";
You cast the @BlogId (string) to a Guid In the linqDataSource.Where property otherwise you get the following error:
Operator '==' incompatible with operand types 'Guid' and 'String'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.Query.Dynamic.ParseException: Operator '==' incompatible with operand types 'Guid' and 'String'