FoxPro Database and asp.net - asp.net

I have a website that uses asp.net 3.5 and Sql server 2008 as backend data store . now we want to add a new page to our website . but the data that we want to share in this page is already being created and saved by a FoxPro software in one of our local systems.
I've no experience in FoxPro . is there any way to use this data in our site ? specially without being have to create a new database in server and importing or writing all records all over again ? what is the best way in such a situation ?
by the way , is it possible to change the extension of a FoxPro database or not ? something like Database.customExt ?

You can connect to a FoxPro database using an OLEDB datasource in ADO.NET. It's up to you whether you feel the need to import the data into SQL Server or just access it directly in FoxPro. It would depend on whether you want to join the data with the SQL Server data in queries for one thing.

In addition to the answers that you already got. You might be interested in using LINQ to VFP or VFP Entity Framwork Provider to make the data access even easier by using LINQ.

You could create a linked server to this Foxpro Database.
Also, Foxpro tables can have any extension. However, you'll have to specify the extension when using the table or database. For Example:
OPEN DATABASE MyFoxProDatabase.customExt
USE mytable.custonExt

You have several options for structuring your data access to FoxPro. You can put the ADO.NET data access code in the codebehind, in it's own c# library or use the objectdatasource for 2 way binding.
private void LoadData(string parameter)
{
string sql = "SELECT a.myfield FROM mytable.customExt a WHERE a.whereField=?";
using(OleDbConnection conn = new OleDbConnection(myConnectionString))
{
using (OleDbCommand command = new OleDbCommand(sql, conn))
{
command.Parameters.AddWithValue("#Parameter", parameter);
try
{
conn.Open();
using(OleDbDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
{
myGridView.DataSource = dr;
myGridView.DataBind();
}
}
catch (Exception ex)
{
throw;
}
}
}
}
Here is a sample connection string to use in web.config:
<add name="myData" connectionString="Provider=VFPOLEDB.1;Data Source=C:\MyFiles\" providerName="System.Data.OleDb"/>
I belive ou can use a custom extension to your FoxPro tables and databases. All your SQL statements would then need to explicitly use that extension.
SELECT t.MyField FROM MyTable.customExt t

While you can change the extension for a Visual FoxPro table, that only affects the DBF file. If the table has any memo fields, there's an associated FPT file, for which you can't change the extension. Similarly, if the table has an associated index file, it must have a CDX extension.
So, if the reason you want to change the extension is to allow multiple tables with the same filestem, don't. It's a good way to really mess things up.

Related

Connect to SQL server using Linq, asp.net

I`m creating a website using asp.net, and I need to use a local SQL server (using Microsoft SQL server). And I have created database and tables in it using the MS SQL Server Management Studio.
Now I successfully connect to the database and do some simple add/query using the following commands:
string connectionString = "data source=ABCD\\SQLEXPRESS;initial catalog=PMD;Trusted_Connection=yes;";
string sqlQuery = "INSERT INTO PMD (username, userID, userAddress)";
sqlQuery += " VALUES (#user, id, add)";
SqlConnection dataConnection = new SqlConnection(connectionString);
SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection);
dataCommand.Parameters.AddWithValue("user", USER.Value);
dataCommand.Parameters.AddWithValue("id", ID.Value);
dataCommand.Parameters.AddWithValue("add", ADDRESS.Text);
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
The command above can add one column to the table, with values stated.
The query is done in a similar way. Compared with Linq, this is not very concise.
So I was wondering how can I change the code so I can use Linq.
The biggest question for me now is how to connect to the base. I already know all the syntax of Linq.
eg: var query=from c in db.username where c.Contain(“Micheal”) select c (or maybe db.PMD.username)
How can I get the db to link with ABCD/SQLEXPRESS, table PMD?
First you need an Object/Relational Mapper (O/RM). You can't just put LINQ on top of your old ADO.NET code.
Microsoft provides two: Linq2SQL and Entity Framework.
Linq2SQL has been discontinued. If I had to choose between the two, I'd go with Entity Framework.
Here you can find an introduction: http://www.asp.net/entity-framework
For example, install Entity Framework, then connect to sql server with entity framework

LINQ to SQL over multiple databases - Failover Partner?

I have an ASP.NET site using LINQ to SQL set up with multiple databases (by changing the Source for the tables on the other/"secondary" server). As seen here.
How do I set up a Failover Partner for this? I have it set up in the connection string, but I had to hardcode the Source with the server name, so that doesn't work.
The best method I could find/come up with was to create two LINQ External Mappings. On startup, I check to see if the main server is running using the main Mapping. If it's not, I set my connections to use the second LINQ mapping that has the FailOver database:
var xmlPath = #"C:\myAppFailOver.map";
System.Data.Linq.Mapping.XmlMappingSource linqMapping = System.Data.Linq.Mapping.XmlMappingSource.FromReader(System.Xml.XmlReader.Create(xmlPath));
using (DataClassesDataContext db = new DataClassesDataContext(connString, linqMapping))
{
//code
}

How to insert,Delete,Edit record in MYSQL database using Details View in Asp.net

I am new in asp net .
I wanted to is there any way to insert, delete and edit rows using details view directly
by a string.
I am using these connection string for connecting mysql database
server=localhost;User Id=root;password=test;database=mydatabase
when I use mdf file I see the insert, delete and edit option, but when I look the options by using directly connection string I can't see it
Thanks in advance
You'll want to put that connection string in your web.config (look up 'connectionstrings'). And then use an objectdatasource. They can connect to the MySQL database.
However, you typically do not want to put select/insert/delete statements in your code and will want to write procedures for those.

ASP.NET LINQ to SQL SubmitChanges() doesn't update the database

In my 2nd ASP.NET MVC project I'm facing a very weird problem: when I call the SubmitChanges method of the DataContext class, nothing updates in the database. It's weird because everything works fine with my first project.
I'm using a remote database created in Sql Server Management Studio, I tried doing some queries there and in Visual Studio 2010 (where I have the connection to the database), they all work.
Where might the problem be hidden?
DBDataContext DB = new DBDataContext();
var myuser = DB.Users.Single(u => u.ID == id);
myuser.Age = 45;
DB.SubmitChanges();
SOLUTION
This is embarrassing :D Indeed I didn't have a primary key. Now it works!
Thanks to everybody!
Table , KEY :)::):)
Insert KEY in TABLE !!!!!
Maybe there isn't anything to submit to the database? SubmitChanges() only will submit modified or new data, if you don't have either, then it will no have any persitent effect.
You may want to read ScottGu's series on Linq to understand a bit more about Linq, or could want to by a book like Linq in action.
Is it possible that the entities you are modifying are not connected to the DataContext? This would prevent them from causing updates to your database.
I'd check your connection string. You might not be connecting to the database you think you're connecting to.

Best way to establish a connection for use with an ADO.NET command object

I'm designing a web service in ASP.NET and VS2008, and am using typed datasets for retrieving table data. These work well and establish their own connections through their associated TableAdapter objects. Edit: I'm using VB, BTW!
I am now attempting to run a custom SQL string using a DataAdapter and a Command object, however I need to reference a Connection object in order for the Command to work. What is the best way to handle this? Should I:
a) Create a global connection object using Global.asax, retrieving the connection string from web.config? (I've been trying that one already, with not much success)
b) Create a class-level connection object using the InitialiseComponent method, also retrieving the ConnectionString from web.config?
c) Retrieve a Connection from one of the TableAdapters that I've already created in my typed DataSets?
d) Something else I haven't thought of yet?
BTW I've been finding it very difficult to extract a ConnectionString from web.config, so any help with that would be appreciated also!
I'm not entirely inexperienced with ASP.NET, but my last big project used VS2003, and I want to make sure that I'm using the current tools correctly.
To extract the connection string, use
WebConfigurationManager.ConnectionStrings["name"].ConnectionString
It's best to open and close the connections as close as possible to their use. ADO.NET will do connection pooling so that this won't be expensive:
var connectionString =
WebConfigurationManager.ConnectionStrings["name"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("query", conn))
{
conn.Open();
// Use the command
}
}
For Connection and data access problems I will advise you to go with some kind of Data Helpers like Microsoft Data Access Application Block
Here you can find small tutorial about how to use it.
For getting connectionstring from web.config use folowing methods
public static string GetConnectionString( string strConstringKey )
{
return ConfigurationManager.ConnectionStrings[strConstringKey];
}
public static bool GetConnectionString(string strConstringKey, ref string strConstring)
{
return (strConstring = ConfigurationManager.ConnectionStrings[strConstringKey] ) == null ? false : true ;
}

Resources