how to find the data values from sql table using c# without writing any sql command? - c#-3.0

at Value Labs the interviewer had asked me such kind of question.
C# .Net Question
SqlCommand cmd=new SqlCommand("select empno,ename from employee where empid='101'");
cmd.ExecuteNonQuery();
but here we don't want to to use Query of command to take the values is this possible?

Related

Delete command in SQL

I want to delete 10 rows from access data
Oledbcommand cmd = new oledbcommand("Delete Top 10 From [Log], cnn);
cnn. Open();
cmd.ExecuteNonQuery();
But I have Syntax error in DELETE statement
The SQL syntax seems fine for SQL Server. (I assume you use SQL Server, because you use brackets for delimiting table or view Log. Sorry if I'm wrong...)
I think you got the error, because you forgot a closing quote for the SQL statement in your OleDbCommand constructor. And casing is sensitive in C#. You could try this:
OleDbCommand cmd = new OleDbCommand("Delete Top 10 From [Log]", cnn);
If you get a SQL related error, my assumption might be wrong and you need to look up the specific SQL syntax for the database system that you are using.
Tip: If you are using SQL Server, you may also look into the specialized SqlCommand class and corresponding classes in the System.Data.SqlClient namespace. They offer additional support for SQL Server functionality.

Handling multiple tables using List<> in MVC

Here is my prob, am new to mvc,
I have used a List<> to get Data from Store Procedure as below,
List<Sp_MM_GetDetails_Result> lDetails = objDB.Sp_MM_GetDetails(ArticleURL).ToList();
Its working fine for me, but my SP returning 2 table of data. However my List<> taking only first table's data.
what should do if I wanted to get data from second table?
In C#, we can done this by using
DataSet.Table[1]
You need to execute the raw SqlCommand and execute Translate to map to the matching types.
Check this sample
https://msdn.microsoft.com/de-de/data/jj691402

JPA: How to call a stored procedure

I have a stored procedure in my project under sql/my_prod.sql
there I have my function delete_entity
In my entity
#NamedNativeQuery(name = "delete_entity_prod",
query = "{call /sql/delete_entity(:lineId)}",
and I call it
Query query = entityManager.createNamedQuery("delete_entity_prod")
setParameter("lineId",lineId);
I followed this example: http://objectopia.com/2009/06/26/calling-stored-procedures-in-jpa/
but it does not execute the delete and it does not send any error.
I haven't found clear information about this, am I missing something? Maybe I need to load the my_prod.sql first? But how?
JPA 2.1 standardized stored procedure support if you are able to use it, with examples here http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Stored_Procedures
This is actually they way you create a query.
Query query = entityManager.createNamedQuery("delete_entity_prod")
setParameter("lineId",lineId);
To call it you must execute:
query.executeUpdate();
Of course, the DB must already contain the procedure. So if you have it defined in your SQL file, have a look at Executing SQL Statements from a Text File(this is for MySQL but other database systems use a similar approach to execute scripts)
There is no error shown because query is not executed at any point - just instance of Query is created. Query can be executed by calling executeUpdate:
query.executeUpdate();
Then next problem will arise: Writing some stored procedures to file is not enough - procedures live in database, not in files. So next thing to do is to check that there is correct script to create stored procedure in hands (maybe that is currently content of sql/my_prod.sql) and then use that to create procedure via database client.
All JPA implementations do not support calling stored procedures, but I assume Hibernate is used under the hood, because that is also used in linked tutorial.
It can be the case that current
{call /sql/delete_entity(:lineId)}
is right syntax for calling stored procedure in your database. It looks rather suspicious because of /sql/. If it turns out that this is incorrect syntax, then:
Consult manual for correct syntax
Test via client
Use that as a value of query attribute in NamedNativeQuery annotation.
All that with combination MySQL+Hibernate is explained for example here.

How to execute a raw sql query from LINQ to Entities 4.5?

Problem
I need to execute a raw SQL query from LINQ to Entities and retrieve the result. The query returns the current date/time from the SQL Server instance, and looks like this:
SELECT GETDATE()
[Edit]
I'm using a data model that was created database-first.
[/Edit]
What I've Tried
I've researched this issue on the interwebz and been unable to find a technique to do this. I was able to learn how to do this using LINQ to SQL, but since I'm not using that, it's of no help.
Heres what you are after
var time = context.Database.SqlQuery<DateTime>("SELECT GETDATE()").FirstOrDefault();
You can read more about raw sql and EF here http://msdn.microsoft.com/en-us/data/jj592907.aspx

TSQL - export query to xls /xslx / csv

I have a complicated dynamic query in TSQL that I want to export to Excel.
[The result table contains fields with text longer than 255 chars, if it matters]
I know I can export result using the Management Studio menus but I want to do it automatically by code. Do you know how?
Thanks in advance.
You could have a look at sp_send_dbmail. This allows you to send an email from your query after it's run, containing an attached CSV of the resultset. Obviously the viability of this method would be dependent on how big your resultset is.
Example from the linked document:
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'AdventureWorks2008R2 Administrator',
#recipients = 'danw#Adventure-Works.com',
#query = 'SELECT COUNT(*) FROM AdventureWorks2008R2.Production.WorkOrder
WHERE DueDate > ''2006-04-30''
AND DATEDIFF(dd, ''2006-04-30'', DueDate) < 2' ,
#subject = 'Work Order Count',
#attach_query_result_as_file = 1 ;
One way is to use bcp which you can call from the command line - check out the examples in that reference, and in particular see the info on the -t argument which you can use to set the field terminator (for CSV). There's this linked reference on Specifying Field and Row Terminators.
Or, directly using TSQL you could use OPENROWSET as explained here by Pinal Dave.
Update:
Re;: 2008 64Bit & OPENROWSET - I wasn't aware of that, quick dig throws up this on MSDN forums with a link given. Any help?
Aside from that, other options include writing an SSIS package or using SQL CLR to write an export procedure in .NET to call directly from SQL. Or, you could call bcp from TSQL via xp_cmdshell - you have to enable it though which will open up the possible "attack surface" of SQL Server. I suggest checking out this discussion.
Some approaches here: SQL Server Excel Workbench
I needed to accept a dynamic query and save the results to disk so I can download it through the web application.
insert into data source didn't work out for me because of continued effort in getting it to work.
Eventually I went with sending the query to powershell from SSMS
Read my post here
How do I create a document on the server by running an existing storedprocedure or the sql statement of that procedure on a R2008 sql server
Single quotes however was a problem and at first i didn't trim my query and write it on one line so it had line breaks in sql studio which actually matters.