473,749 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WPF project connection to sqlserver

anindya004
7 New Member
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>
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. namespace ContactManager.Model
  8. {
  9.     public class ContactRepository
  10.     {
  11.         private List<Contact> _contactStore;
  12.         private readonly string _stateFile;
  13.  
  14.         public ContactRepository()
  15.         {
  16.             _stateFile = Path.Combine(
  17.                 AppDomain.CurrentDomain.BaseDirectory,
  18.                 "ContactManager.state"
  19.                 );
  20.  
  21.             Deserialize();
  22.         }
  23.  
  24.         public void Save(Contact contact)
  25.         {
  26.             if (contact.Id == Guid.Empty)
  27.                 contact.Id = Guid.NewGuid();
  28.  
  29.             if (!_contactStore.Contains(contact))
  30.                 _contactStore.Add(contact);
  31.  
  32.             Serialize();
  33.         }
  34.  
  35.         public void Delete(Contact contact)
  36.         {
  37.             _contactStore.Remove(contact);
  38.             Serialize();
  39.         }
  40.  
  41.         public List<Contact> FindByLookup(string lookupName)
  42.         {
  43.             IEnumerable<Contact> found =
  44.                 from c in _contactStore
  45.                 where c.LookupName.StartsWith(
  46.                     lookupName,
  47.                     StringComparison.OrdinalIgnoreCase
  48.                     )
  49.                 select c;
  50.  
  51.             return found.ToList();
  52.         }
  53.  
  54.         public List<Contact> FindAll()
  55.         {
  56.             return new List<Contact>(_contactStore);
  57.         }
  58.  
  59.         private void Serialize()
  60.         {
  61.             using (FileStream stream =
  62.                 File.Open(_stateFile, FileMode.OpenOrCreate))
  63.             {
  64.                 BinaryFormatter formatter = new BinaryFormatter();
  65.                 formatter.Serialize(stream, _contactStore);
  66.             }
  67.         }
  68.  
  69.         private void Deserialize()
  70.         {
  71.             if (File.Exists(_stateFile))
  72.             {
  73.                 using (FileStream stream =
  74.                     File.Open(_stateFile, FileMode.Open))
  75.                 {
  76.                     BinaryFormatter formatter = new BinaryFormatter();
  77.  
  78.                     _contactStore =
  79.                         (List<Contact>)formatter.Deserialize(stream);
  80.                 }
  81.             }
  82.             else _contactStore = new List<Contact>();
  83.         }
  84.     }
  85. }
.
</code>

now in this file you can see that

Expand|Select|Wrap|Line Numbers
  1. _stateFile = Path.Combine(
  2.                 AppDomain.CurrentDomain.BaseDirectory,
  3.                 "ContactManager.state"
  4.  
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.
Attached Files
File Type: zip ContactManager.zip (21.7 KB, 416 views)
Mar 21 '10 #1
17 9287
tlhintoq
3,525 Recognized Expert Specialist
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

The tag is [code] not <code>
Mar 21 '10 #2
tlhintoq
3,525 Recognized Expert Specialist
My question is how to connect to sql server instead to this ContactManager. state file
now inside contactreposito ry.cs file
Until someone with more experience can offer more targeted advice I can point you toward these:

Database How-to parts 1 and 2
Database tutorial Part 1
Database tutorial Part 2
Mar 21 '10 #3
anindya004
7 New Member
I know the sql data connection to the windows forms.
Like the following
<code>
private void button1_Click(o bject sender, EventArgs e)
{
SqlConnection con = new SqlConnection(" Data Source=STUDENT-1D40FC5\\SQLEXP RESS; Initial Catalog=Library Mgnt;Integrated Security=SSPI") ;
string query = "insert into BookDetails(Tit le,Author,Publi sher,Subject,No _Of_Copies)valu es('" + title.Text + "','" + author.Text + "','" + textBox3.Text + "','" + subject.Text + "','" + no.Text + "')";
SqlCommand cmd = new SqlCommand(quer y, con);
con.Open();
SqlDataReader reader = cmd.ExecuteRead er();
MessageBox.Show ("The Book Details are saved.");
while (reader.Read())
{
}
reader.Close();

}
</code>

I want to know how to insert to the _stateFile in the code.
As if you see the full project and download it then you will see that it is using a binary file for the purpose of database connection and insert,update,d elete.

So in place
<code>
_stateFile = Path.Combine(
AppDomain.Curre ntDomain.BaseDi rectory,
"ContactManager .state"
</code>
how to connect to the sql database or .mdf file rather than a binary file.
Mar 22 '10 #4
anindya004
7 New Member
Experts I have a question---how to connect the following project to sql server and user insert/edit/update queries to the database-

1.Please open the page http://www.informit.com/store/produc...sbn=0672329859
2.click on Downloads
3.Please download the Contact Manager

4.Unzip ContactManager. zip
5.Please open ContactReposito ry.cs file
6.There you will find the following code

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. namespace ContactManager.Model
  8. {
  9.     public class ContactRepository
  10.     {
  11.         private List<Contact> _contactStore;
  12.         private readonly string _stateFile;
  13.  
  14.         public ContactRepository()
  15.         {
  16.             _stateFile = Path.Combine(
  17.                 AppDomain.CurrentDomain.BaseDirectory,
  18.                 "ContactManager.state"
  19.                 );
  20.  
  21.             Deserialize();
  22.         }
  23.  
  24.         public void Save(Contact contact)
  25.         {
  26.             if (contact.Id == Guid.Empty)
  27.                 contact.Id = Guid.NewGuid();
  28.  
  29.             if (!_contactStore.Contains(contact))
  30.                 _contactStore.Add(contact);
  31.  
  32.             Serialize();
  33.         }
  34.  
  35.         public void Delete(Contact contact)
  36.         {
  37.             _contactStore.Remove(contact);
  38.             Serialize();
  39.         }
  40.  
  41.         public List<Contact> FindByLookup(string lookupName)
  42.         {
  43.             IEnumerable<Contact> found =
  44.                 from c in _contactStore
  45.                 where c.LookupName.StartsWith(
  46.                     lookupName,
  47.                     StringComparison.OrdinalIgnoreCase
  48.                     )
  49.                 select c;
  50.  
  51.             return found.ToList();
  52.         }
  53.  
  54.         public List<Contact> FindAll()
  55.         {
  56.             return new List<Contact>(_contactStore);
  57.         }
  58.  
  59.         private void Serialize()
  60.         {
  61.             using (FileStream stream =
  62.                 File.Open(_stateFile, FileMode.OpenOrCreate))
  63.             {
  64.                 BinaryFormatter formatter = new BinaryFormatter();
  65.                 formatter.Serialize(stream, _contactStore);
  66.             }
  67.         }
  68.  
  69.         private void Deserialize()
  70.         {
  71.             if (File.Exists(_stateFile))
  72.             {
  73.                 using (FileStream stream =
  74.                     File.Open(_stateFile, FileMode.Open))
  75.                 {
  76.                     BinaryFormatter formatter = new BinaryFormatter();
  77.  
  78.                     _contactStore =
  79.                         (List<Contact>)formatter.Deserialize(stream);
  80.                 }
  81.             }
  82.             else _contactStore = new List<Contact>();
  83.         }
  84.     }
  85. }
  86.  
  87.  
7.Now here you are seeing the following
<code>
Expand|Select|Wrap|Line Numbers
  1. _stateFile = Path.Combine(
  2.                 AppDomain.CurrentDomain.BaseDirectory,
  3.                 "ContactManager.state"
</code>

8.What this project is doing is it is storing the data in a binary data file ContactManager. state
9.Then passing those values to the variable _stateFile
10. Now my question is how to connect this project to the sql server and in place of the binary data file I want a table to be user in sqlserver .
11.How that is to be done I do not know.
12.I know the basic data connection to the winform application and insert/update/delete operation
as followes

<code>

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             SqlConnection con = new SqlConnection("Data Source=STUDENT-1D40FC5\\SQLEXPRESS; Initial Catalog=LibraryMgnt;Integrated Security=SSPI");
  4.             string query = "insert into BookDetails(Title,Author,Publisher,Subject,No_Of_Copies)values('" + title.Text + "','" + author.Text + "','" + textBox3.Text + "','" + subject.Text + "','" + no.Text + "')";
  5.             SqlCommand cmd = new SqlCommand(query, con);
  6.             con.Open();
  7.             SqlDataReader reader = cmd.ExecuteReader();
  8.             MessageBox.Show("The Book Details are saved.");
  9.             while (reader.Read())
  10.             {
  11.             }
  12.             reader.Close();
  13.  
  14.         }
</code>

13.But in this project how that connection is to be established I do not know and also how insert/update/delete queries are to be used .

14.Experts I thought that ContactReposito ry.cs file only is responsible for the data connection if I am wrong then please see the whole project where the required changes are to be made.That is the main reason I have send the url of the code project .

I am totally new to WPF please explain step by step
Mar 22 '10 #5
tlhintoq
3,525 Recognized Expert Specialist
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

I know I have said this in a few of your posts now.
The tag for code is [code] not <code>
Stop making other people do this for you. It's lazy.
Mar 22 '10 #6
tlhintoq
3,525 Recognized Expert Specialist
I have to say, you are asking an aweful lot of people just to answer
How to connect the following project to sql server and user insert/edit/update queries to the database
There really isn't a need to download your entire project etc. to tell you how to connect to a database and use insert/edit/update queries.

Database How-to parts 1 and 2
Database tutorial Part 1
Database tutorial Part 2
Mar 22 '10 #7
tlhintoq
3,525 Recognized Expert Specialist
Please don't double-post your questions. It divides attempts to help you in an organized and cohesive manner. Your threads have been merged
Mar 22 '10 #8
anindya004
7 New Member
can you please suggest me how to replace the binary data file and what to do there as here binary data file is used
Mar 22 '10 #9
tlhintoq
3,525 Recognized Expert Specialist
What do you mean by "replace"?
Update the value in the database?
Or replace the use of a binary file with some other kind of data: Something different than binary file?

If you mean 'update the database'... That is very well covered in the tutorials. Did you *do* the tutorials?
Mar 22 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2025
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent users So I designed the project as master Node that has all administration
3
8183
by: ebrahimbandookwala | last post by:
Hi , I am trying to connect to MS Sql server 2000 from Java (1.4.2 / 1.5 ). I installed my Sql Server(8.00.382) from the one supplied with VS.NET 2001. When I installed it on my laptop it did not ask me for a user name and password. After install when I re-started my machine I see the server started up with a green light. Now when I connect to the server from VS.NET it works fine. This is because VS uses windows integrated security. I...
1
2486
by: Lauren Quantrell | last post by:
I'm wondering which is the best approach using an Access2K front end and a SQL Server 2K backend. I have a stored procedure running three INSERT INTO statements that inserts records into three different tables. Should I use the ADP project connection as set through the Access Connection window: Set cmd = New ADODB.Command Set cmd.ActiveConnection = CurrentProject.Connection
1
1717
by: Jim Devenish | last post by:
I am continuing my exploration about upsizing to SQLServer from Access 2000. I have a split database with a front-end and a back-end, each of which is A2K. I have spent some time in bookshops trying to learn about the way forward and trying to decide which to buy. The bit I am interested in is usually at page 900 and I am not sure whether I want to buy the first 800. It appears that upsizing creates an Access project as the...
5
1802
by: Dave Dudley | last post by:
Hi, I have a new ASP .Net project that has been developed on our development machine and connects to SQL Server 2000 on the same machine. It is connecting via a connection string similiar to: Data Source=;Initial Catalog=;uid=;Password=
1
959
by: Galina Grechka | last post by:
Hi Recently I started get error message every time when trying to open web project from Microsoft Visual Studio.Net: Unable to open Web project. The path does not correspond to URL. The two need to map to the same server location. HTTP Error 404: Object Not Found. It appears that our web server has several IP addresses. The work around is: delete from DNS all IP addresses except the one I'm connecting to,
3
1630
by: Dan Sikorsky | last post by:
Can I use SQLServer 2000 with ASP.NET 2.0 instead of SQLServer 2005, and use the .Net 2.0 Membership functionality? I've setup my Login page, controls, etc., and now it's time to use the Web Site Administration Tool (WAT) but it won't sense my SQLServer 2000 database.
2
2301
by: CriticalJ | last post by:
I have several projects that have been working for at least four years. Last Thursday, the connections for these projects began dropping immediately upon opening the project. When I test the connection, the connection will succeed. However, upon closing the connection dialog box, the database temporarily connects and then drops. This happens for both Access 2k and Access 2003. I spent Thursday and Friday trying to get things back to...
21
3925
by: adjo | last post by:
I am working on an app with an Access2002 frontend and Sql2005 backend. I have to use integrated security. I want to prevent my users from altering data in another way than via the frontend. It looks to me that the mechanism to do it is the Sqlserver sp_setapprole procedure. Works fine when programming directly to Sqlserver, and also een Access Data Project at first sight seems to work as it should via the call to the sp_setapprole proc....
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.