work on C# vs2008. I have a stored procedure ,This procedure holds temp table ,i need to get the temp table values.My sql query is bellow:
create procedure [dbo].[TestProcedure]
as
SELECT * INTO #temp1 from(select * from DischargePort) as b
select * from #temp1
drop table #temp1
My above query has a temp table named #temp1.After run this query in sql-server-management i get result ,but when i try to execute this procedure in linq ,I get no result.My linq syntax is bellow:
var r = ProviderName.TestProcedure();
Can anybody tell me why this problem arise,How to overcome this problem.Hope any body not say that linq can not handled the temp table or this kind of word.if have any query plz ask .Thanks in advance.
I don't think this is anything to do with the temporary table, but rather that Linq does not know what output is to expected.
With Dotnet Framework 4, this is easy, as you can do something like
(from r in ProviderName.TestProcedure().AsDynamic()
select new { r.Id, r.Description}).ToList()
(assumes Id and description are fields in DischargePort)
Otherwise, you need to do something in your designer to tell Linq what your procedure outputs. I have never had to do this, but perhaps this article will help.
When I think about it, in this particular case, you should be able to do something like
var results = (from r in ExecuteQuery<DischargePort>("exec TestProcedure")
select r ).ToList();
i would start by downloading linqpad to see the sql that linq is emitting, this may provide some clues. you could also use the sql profiler tool to see what query is being run.
Related
I've been trying everything to solve this problem, but couldn't get to a solution. This is my first time using MVC.
I have already the query in SQL Server that shows a list of table names and column names of a specific database. This is the query:
SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY table_name
Is there a way to execute this query in MVC and show the results in the html table?
select YOURTABLENAME,YOURCOLUMNNAME
from YOURDATABASENAME.INFORMATION.COLUMNS
Hope it helps
Why don't you use a stored procedure to call it. Try something like this:
var tableInfo = db.Database.SqlQuery<tableInfo>("tableInfo_Search");
tableInfo_Search would be your sproc's name. Then you can bind that to the table view you wish. If your sproc has parameters that it takes you can just add them after the proc's name.
But then again, your Linq to SQL DataContext already has the table names: context.Mapping.GetTables() should be much easier.
Please let me know if this helps.
select TABLE_NAME,COLUMN_NAME
from <database-name>.INFORMATION_SCHEMA.COLUMNS
Use the above query.
using (SampleContext dbContext = new SampleContext())
{
var tablelist = dbContext.Mapping.GetTables();
}
You can try the above code.
I have this LINQ query in C# for querying a db4o database.
IEnumerable<internetRecord> searchResult = from internetRecord ie in database
where ie.GSrecordID.Contains(txtSearchString.Text)
select ie;
What would be the equivalent query in SQL? (needed for comparison purposes) I have not worked with SQL much in the past and looking at it after using LINQ for a while it seems confusing.
SELECT *
FROM MyTable
WHERE GSRecordID LIKE '%txtSearchString%'
Select * from internetrecord where GSrecordID like '%your comparison string%'
Provided internetrecord is your SQL table GSrecordID is the column of your table.
I don't know much about db40 but in standard SQL it would be:
SELECT * FROM internetRecord
WHERE GSrecordID LIKE '%txtSearchString%'
YOu can do something like this
var result = database.Where(x => x.GSrecordID.Contains(txtSearchString.Text));
MSDN docs say that I can write a stored procedure like this
CREATE PROCEDURE MultipleResultTypesSequentially
AS
select * from products
select * from customers
then read it from LINQ like this
IMultipleResults sprocResults =
db.MultipleResultTypesSequentially();
// First read products.
foreach (Product prod in sprocResults.GetResult<Product>())
{
Console.WriteLine(prod.ProductID);
}
// Next read customers.
foreach (Customer cust in sprocResults.GetResult<Customer>())
{
Console.WriteLine(cust.CustomerID);
}
what if one of my select statements return something other then a regular table object - with a join or just selecting certain columns?
how do I let LINQ know that I want to read the next SELECT ??? basically , what I'm wondering is this example from MSDN reading Products first then Customers because they are written in that order in the stored procedure , or is writing .GetResult<Customer>() telling c# to find the result that maps to type Customer? and also what would the foreach loop look like for this unknown type?
I did a quick test with a similar stored procedure and found that if your foreach loops are not in the correct order, an InvalidOperationException is thrown, so it doesn't look like C# is able to find the correct result based on the type used in GetResult<>.
As for your select statement returning something other than a table, if you drag a stored procedure from the Server Explorer onto the Linq to Sql designer, the designer will autogenerate a class based on procedures output. I created a procedure with a couple of joined tables, neither of which existed in my project, and Linq to Sql created a type for me, named like StoredProcedureNameResult, so a procedure called GetCreditCard had a type named GetCreditCardResult.
I have a stored procedure and I want to average null columns.
This is my stored procedure :
SELECT
AVG(planned) AS Planned,
AVG(achieved) AS Achieved
FROM
Port
INNER JOIN
Technology ON Port.portID = Technology.portRef
I bind this stored procedure to a chart using datasource and when the column is null the C# code throws this error:
Value was either too large or too small for a Decimal.
How can I handle my stored procedure to avg those null columns?
This happens when the query does not return any values. Use this
SELECT
coalesce(avg(planned),0) as Planned,
coalesce(avg(achieved),0) as Achieved
FROM
Port inner join Technology on Port.portID = Technology.portRef
another way
SELECT avg(isnull(planned,0))as Planned,avg(isnull(achieved,0)) as Achieved
FROM Port inner join Technology on Port.portID = Technology.portRef
I have seen several people getting confused whether to use ISNULL or COALESCE.
I would strongly recommend COALESCE. In some cases both will run fine, but in some cases ISNULL if not properly handled will give wrong output.
Please check the below snippet :
SELECT
ISNULL(Nullif('test', 'test'), '12345') AS using_isnull,
COALESCE(Nullif('test', 'test'), '12345') AS using_coalesce,
ISNULL(Nullif('test', 'test'), 12345) AS int_using_isnull,
COALESCE(Nullif('test', 'test'), 12345) AS int_using_coalesce
Use the keyword unpivot
select id, AVG(Q)
from (select * from myTable) a
unpivot(Q for QQ IN(Q1,Q2,Q3,Q4,Q5)) b
group by id
If all the columns are null, it won't return anything though.
I use this code to define my stored procedure
CREATE PROCEDURE [dbo].[SP]
(#Country NVARCHAR(20))
AS
BEGIN
SET NOCOUNT ON;
SELECT c.*,O.* from Customers
as c inner join orders O on c.CustomerID=o.CustomerID
where c.Country=#Country
END
and this is my C# code:
IList<Entities.Customer> Customers;
using (var context = new NorthwindContext())
{
SqlParameter categoryParam = new SqlParameter("#Country", "London");
Customers = context.Database.SqlQuery<Entities.Customer>("SP #Country", categoryParam).ToList();
}
Problem is here :
I want to message the data from Orders table and my stored procedure generate this to me. How can I get the Orders data in my C# code? Remember I want to execute this stored procedure only once.
Take a look at Does Entity Framework Code First support stored procedures? and http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-4-1-using-stored-procedure-to-insert-data.aspx which talk about executing a stored proc via a DbContext object.
I think perhaps your issue is that you aren't getting the orders back as part of your query? is this correct? If so this is because you are only selecting customers. You need to either create an object of the same schema as you expect to be returned from your query (ie that has both customer and order properties) or select into a dynamic type (eww).
Having said that i strongly recommend doing this in linq instead:
from c in context.Customers.Include(c=>c.Orders)
where c.Country == country
select c;
This is a much better approach as you are using EF for what its designed for and not querying for something which doesn't fit your model