Members I have a question how to connect to sql server tables of a database with this proect's data.
Can you suggest .
Members you can see that inside the bin\debug\Conta ctManager.state now in this ContactManager. state is the file where the data is getting stored when I am running the proj.
There is a _stateFile in the ContactReposito ry.cs where the values are getting stotred.This variable is later on used for all the operations.
My question is how to connect to sql server instead to this ContactManager. state file
now inside contactreposito ry.cs file
<code>
.
</code>
now in this file you can see that
is there .
Now to connect it to the sqlserver what I have to do is instead of the binary file "ContactManager .state " ....I want a it to be connected to a *.mdf .
How to do this please suggest me .
This is an open source project.
for the detail explanation please see the two attached files with this question which have attached with this question.
The code can be downloaded from the website:
1. Go to www.informit.com/title/9780672329715.
2. Click Downloads.
3. Click one of links that appear, and the download should start automatically.
Can you suggest .
Members you can see that inside the bin\debug\Conta ctManager.state now in this ContactManager. state is the file where the data is getting stored when I am running the proj.
There is a _stateFile in the ContactReposito ry.cs where the values are getting stotred.This variable is later on used for all the operations.
My question is how to connect to sql server instead to this ContactManager. state file
now inside contactreposito ry.cs file
<code>
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
namespace ContactManager.Model
{
public class ContactRepository
{
private List<Contact> _contactStore;
private readonly string _stateFile;
public ContactRepository()
{
_stateFile = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"ContactManager.state"
);
Deserialize();
}
public void Save(Contact contact)
{
if (contact.Id == Guid.Empty)
contact.Id = Guid.NewGuid();
if (!_contactStore.Contains(contact))
_contactStore.Add(contact);
Serialize();
}
public void Delete(Contact contact)
{
_contactStore.Remove(contact);
Serialize();
}
public List<Contact> FindByLookup(string lookupName)
{
IEnumerable<Contact> found =
from c in _contactStore
where c.LookupName.StartsWith(
lookupName,
StringComparison.OrdinalIgnoreCase
)
select c;
return found.ToList();
}
public List<Contact> FindAll()
{
return new List<Contact>(_contactStore);
}
private void Serialize()
{
using (FileStream stream =
File.Open(_stateFile, FileMode.OpenOrCreate))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, _contactStore);
}
}
private void Deserialize()
{
if (File.Exists(_stateFile))
{
using (FileStream stream =
File.Open(_stateFile, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
_contactStore =
(List<Contact>)formatter.Deserialize(stream);
}
}
else _contactStore = new List<Contact>();
}
}
}
</code>
now in this file you can see that
Code:
_stateFile = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"ContactManager.state"
Now to connect it to the sqlserver what I have to do is instead of the binary file "ContactManager .state " ....I want a it to be connected to a *.mdf .
How to do this please suggest me .
This is an open source project.
for the detail explanation please see the two attached files with this question which have attached with this question.
The code can be downloaded from the website:
1. Go to www.informit.com/title/9780672329715.
2. Click Downloads.
3. Click one of links that appear, and the download should start automatically.
Comment