Insert/Select data in foxpro database using c# .net - c#

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.

Related

Does migrating data from SQL Server to SQLite have effects on the application codes

I have a project I developed with C# windows forms using SQL Server as database but I want to make the project standalone without any database server. I found SQLite as an option to go for, Is it possible to migrate my SQL Server database to SQLite and not affecting my code? And how do I go about it?
I used entityframework code first in connecting the SQL Server database
The answer is almost certainly going to be "yes." Depending on a few things, you might have to change very little (or no) code, or you might have to change a lot.
The first consideration is your SQL code. If you were very careful to write ANSI-compliant SQL and you didn't use any of the built-in SQL Server views or T-SQL-specific functions, you may not have to re-write much code at all. In reality, you probably will have to at least write some. In particular, while SQL Server's engine is meant to handle multiple concurrent sessions and queries, SQLite is not: you will need to manage your program carefully to ensure no two threads attempt to access the SQLite database at once.
The second consideration is how your application calls the database. Again, depending on your design, you may need to re-write almost no code, or you may need to re-write a lot. In my C# applications, I create an interface for database providers that defines common functionality (select, delete, insert, etc). Then I create simple wrapper classes for different RDBMS that implement the interface. When I need to switch databases, I simply instantiate and use a different class. If you have your project setup like this, then you'd simply need to create a new class for SQLite that implements your database interface and instantiate that instead of your SQL Server class. If you wrote a lot of SQL Server specific C# code into your business logic, you might have a lot of coding to do.

Single solutions with varous database management software

Actually I am new to the software development, I have an idea to create an web application in which I am going to use front end C#.Net and Back end:SQL Server. What happen if my client ask me to use other database management software other than SQL Server? Is there any solutions to run same application without changing the SQL code like dynamic database creation based on the client requirement?
Help me..
Each Database like SQL Server , Oracle is having it's own coding syntax and are totally different . It's not possible to use the code of one DBMS for another.
In most cases SQL statements will work across various Database as it's standard for relational databases . But moreover when work with different databases you need to work in it's own standard.
Also there are different kind of databases available such as RDBMS ,NoSQL etc.. so each different in it's own way.
There might be some tools available to help you to convert one code to another.
You need to isolate database access from the rest of the program so that database specific code is in one place only.
For this convenience you need to learn MVC to perform these tasks. It can happen to certain level but still altogether certains functionalities need specific methods to be called but it increases efficiency...

Create a localDB and use it when SQL server is down

I need your help to manage an issue with my C# program. I wrote a GUI that allows the user to manage a lot of data stored in a SQL Server database. Everything works fine but I want to be sure that the application works even when the server is down (for a generic issue).
My plan is to have a local database (e.g. *.mdf database used in Visual Studio) and update it every time the GUI is able to connect to the online SQL Server database.
What do you think? Is there something similar to a procedure or do I need to do it manually (create a .mdf file, check the online version, write the changes etc.)?
Apart from the comments noting that this may not be a good idea (which I agree):
Most of the work must be done manually. If you have a DB model within your application (like when using entity framework) it could be that it can create the DB structure in the local file. Most of your data will need timestamps to determine when they were changed the last time.
The Microsoft Sync Framework might utilize you but I have not used it personally. Look here http://msdn.microsoft.com/en-us/library/bb902854(v=sql.110).aspx

How to use a local database in c#?

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"));

How do I assign a sql record value to string in asp.net

I'm new to asp, so be patient :p
I have an sql database, with a users table that has username and a colour.
My asp master page is reading the current users username and passing it to the content fine.
What I am trying to now do is in the content page grab the colour from the sql table where the username is the current username, and assign it to a string inside the default.aspx.cs file.
How do I do this?
Cheers
Honestly, and don't take this offensively, but perhaps diving straight into writing a database-driven ASP.NET application isn't where you should be starting out.
I would attempt to learn more about the ASP.NET architecture first, learn the language well (VB.NET or C#), and start with something simpler.
With that said, and assuming you're going to attempt this anyway, what you are probably going to want to start out with is ADO.NET. Microsoft, through MSDN, provides a good page full of ADO.NET Code Examples.
You'll have to define a connection string to your database, which may include specifying a third-party driver if you're using a database such as Sybase or MySql, or using an entirely different set of provider classes such as what you'd need for Oracle. Next you'll create a command containing your SQL query and execute the command. You need to consume the results and pull your value out of a result set, and then clean up the close the connection.
Check out the link; hopefully it will get you started. There are a ton of examples of ADO.NET sprinkled across the Interwebs, I'm sure you can find a working example.
If you want to skip ADO.NET and try something a bit more O/R mappery (and are using Microsoft SQL Server), you might be interested in checking out LINQ to SQL.
EDIT
From your comments, your question is about how to read a value from SQL Server. The most basic way is by just opening a SqlConnection, create a SqlCommand, setting the command text, etc etc. There are many tutorials online. Unfortunately you run the risk of Sql Injection attacks that way.
The best way is to set up an OR Mapper, which creates objects for you, that map directly to your database. The best option there would probably be Entity Framework 4. There are plenty of tutorials online.
END EDIT
Probably the best thing to do would be to set a property in your master page, and then have your default.aspx page read the value from the master page, and do whatever it wants with it.
Having said that, it's usually a bad idea to be doing database reads from your UI. Usually you'll want to push that stuff off to another tier, and have your UI focus on just doing UI things.
But, assuming this is just a simple project, then something like the following line should get you where you need to go:
From default.aspx:
string value = (this.Master as YourMasterPageClass).YourProperty;

Categories

Resources