I am newbie to C # ASP.Net I use Microsoft.SqlServer.Management.Smo that has connected to SQL Server 2012 but can not load data from SQL form. You can guide me to use SMO.dll
Thanks!
The easiest way I have found is to use an ORM named Dapper with one of its plugins Dapper.FastCRUD.
https://github.com/StackExchange/Dapper
https://github.com/MoonStorm/Dapper.FastCRUD
There are many different ORMs out there and Entity Framework comes with Visual Studio. They are all very similar in operation.
first you would create a model which directly maps to your table structure. If using Dapper alone you would create your query in a string or specify a stored procedure to execute. Depending on the type of operation the result would either be a bool or model. When executing the operation you would set a cast of the model for the ORM to recognize when sending data to the server and receiving. If you are using a plugin like FastCRUD, EasyCRUD or others the query would be automatically created for you and based upon the model and the type of CRUD operation would execute the appropriate query on the server.
This is oversimplified but there is good documentation o Dapper that will get you moving pretty quick. the documentation is lacking for FastCRUD IMO but EasyCRUD does have better documentation just not updated as often from what I can tell.
Related
Ok, I realize this is a loaded question, so bear with me while I break it down.
I have a local network with a database, and then I have multiple remote locations with their own databases with similar schemas. The only way to query the databases at the remote locations is to go through a (somewhat) isolated machine. So, I can't directly connect to the remote databases, BUT I created a Windows service running on the isolated machine that I can feed a SQL command and parameters to, and the service will then run the query against the remote database and return the query results (the current version of this service uses WCF).
But with evolving technology and my desire to create a RESTful version of the service explained above... I'm looking at setting up the service with Entity Framework Core, so instead of passing a generic SQL command string and returning a DataTable, I'd like to send a generic query and return EF entities.
Only problem is I'm not familiar enough to know if I can create a generic method that I can pass any generic EF query... I know about generic repository patterns, but I'm not looking to run only predefined queries.
Below is an example of what I'm doing now, is there a way to do this in Entity Framework by somehow prebuilding the EF query and passing it into another method to actually handle the query on the server?
public DataTable GenericQuery(string genericSqlCommand, List<Tuple<string, object>> sqlParameters)
{
//method processes genericSqlCommand and sqlParameters using C# MySqlConnector library and returns query results as a generic DataTable to be sent as response to application calling service
}
I understand the problems with the above method and recognize that simply going to a cloud-based system where all systems could connect to a single database would solve a lot of headache... unfortunately, my hands are tied on that matter. So what I've explained above is the best solution I've been able to think of in the current situation. It's honestly worked quite well for a few years.
Well since you have to expose your database to certain queries, I think that GraphQL would help you out to do that since it is 100% RESTFull.
Its objective is to expose a relational or nonrelational database using Rest.
PS. I have not tested this tutorial nor GraphQL with .NET Core but it seems like a good start for your needs: https://dev.to/dotnet/how-you-can-build-a-web-api-using-graphql-net-core-and-entity-framework-1ago
After spending more time on this than I care to admit... there's no way to do this. From my understanding, Entity Framework is designed with entities/objects in mind... So there's no way to do things generically like I was hoping. Web API is the same... it expects specific object types in and out.
So the best way I found to do this is to just continue using the generic DataTable for generic queries. I can serialize and deserialize DataTable all day with little effort using the Newtonsoft.Json library.
I have been looking at the Microsoft examples and most use Entity Framework. However it seems overkill for when I need to make a quick connection. Gets some data from a report and the return it to my web page. It also seems like it is difficult to compose such things as complex SQL that might require a multi-table join and some input parameters.
So what other options are available?
If you want a quick way to connect to your datasrouce you can look into some micro ORM-frameworks:
https://code.google.com/p/dapper-dot-net/
https://github.com/robconery/massive
http://www.toptensoftware.com/petapoco/
These are all tiny libraries that allow you to write your own SQL but still help you (a bit) with mapping the results to classes.
Here is what we do for our reports app that fetches data using Web Api and SQL.
1. Stored Procedures with EF
Since you mention you need to fetch data that might require a multi-table join, best approach here would be to use Stored Procedures to get that data. Import that Stored Procedure to the EntityFramework.
2. Service Layer Class & AutoMapper
Now create a similar object that will map to the Data Layer's object that will be returned from the Stored Procedure you just added. To map these two classes we use a mapper called as AutoMapper. We create two classes because we dont want our web app to directly access the Data Layer classes.
3. Expose your Web API
Now this data can be sent from the api to whoever wants to use it.
Hope this helps.
I want to use FoxPro database in backend and c# .net in front end but i don't know how to connect with foxpro database in .net
For connectivity what code i use, please suggest...
You may want to look at .Net Interop.
Also take a look at West-Wind web connect. They have a framework that allows you to use the Visual studio IDE to create webforms but also use your VFP business logic and data source. This works well.
West wind also has a wwDotNetBridge that allows you to access .Net components from VFP.
Check out their website below.
http://west-wind.com/WestwindClientTools.aspx
http://www.west-wind.com/presentations/VFPDOTNETiNTEROP/VFPDOTNETINTEROP.HTM
I don't think you necessarily need to go the way of "interop", but get a basic understanding of connecting and querying data.
First, get Microsoft's OleDB provider located here
Here is a sample doing a connection and running a simple query to get data but this one sends the results to another VFP table instead of bringing back to C# for process/usage.
This example shows Inserting records and uses parameters to help prevent any attempts at SQL-injection attacks
And another using SQL-Update
Once you get the basics down, its not that difficult. I've actually made a simple "wrapper" class to centralize ensuring a valid connection, execute a given query and closing connection when done. Then, I've just added methods to it for each thing I wanted to do (or could be subclassed too). Anyhow, when I need to do a certain action, I would just call that function and pass in whatever parameter(s) was(were) needed.
I am working on a Visual C# program and have added a local database through the Add->New Item... dialog.
The guides I have read about using this type of database then instructed to make a data source using the database and then in the data sources toolbar just drag and drop a table to create the tools for entering data into the database and reading data from it.
This isn't how I want to access the database, though. I want to have completely different controls for the user and then just directly interface with the database in my code. I haven't been able to find anything that explains how to do this, though. Can anybody offer any advice? Do I need to use LINQ for this or would it be just as good for me to use SQL?
You may want to check out at http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx
I suggest that you must have to learn the Data Provider classes (ADO.NET). For direct access to Microsoft SQL Server use data provider classes for MsSql server (System.Data.SqlClient). In order to use LINQ, you must have know-how of Data Provider classes, Generics, Delegates and Anonymous methods.
I've made a local database for a C# project:
I know basic SQL commands, but haven't worked with databases in C#.
What I'd like to know specifically is:
How to read from the database (query)
How to add and update rows
The database only consists of 3 tables, so I don't think anything fancy is needed.
First, you should learn a bit about various technologies and APIs for connecting with a database.
The more traditional method is ADO.NET, which allows you to define connections and execute SQL queries or stored procedures very easily. I recommend digging up a basic tutorial on ADO.NET using Google, which may differ depending on what type of project you're creating (web app, console, WinForms, etc).
Now days, ORMs are becoming increasingly popular. They allow you to define your object model in code (such as every database table would be a class, and columns would be properties on that class) and bind to an existing database. To add a new row to a table, you'd just create an instance of a class and call a "Save" method when you're done.
The .NET framework has LINQ to SQL and the Entity Framework for this sort of pattern, both of which have plenty of tutorials online. An open source project I really like is Castle Active Record, which is built on top of NHibernate. It makes defining ORMs quite easy.
If you have specific questions about any of the above, don't hesitate to post a new question with more specific inquiries. Good luck!
Update:
I thought I'd also put in one last reference as it seems you might be interested in working with local database stores rather than building a client/server app. SQLite allows you to interact with local stores on the file system through SQL code. There's also a .NET binding maintained by the SQLite guys (which would in theory allow you to work with the other platforms I mentioned): http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
You can use SQLCE.
This blog will give you a good start.
http://weblogs.asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx
Here is a small tutorial that should be helpful to you.
You can make use of the SqlDataReader to read data
and the SqlCommand to Insert Update Delete rows from your tables.
http://www.dotnetperls.com/sqlclient
You could use it by adding following to your Startup.cs
services.AddDbContext<DemoDbContext>(options => options.UseSqlite("Filename=data.db"));