Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Database With Microsoft Visual Studio With C#

database

  • Please log in to reply
20 replies to this topic

#1 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 25 June 2012 - 05:12 PM

I want to create a form. For example, i have a rectangle with length 3m and breadth 2m. To find the area of the rectangle is length * breadth.
To solve this pblm, how to do I use database in microsoft visual studio 2010; what are the codings?
  • 0

#2 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 25 June 2012 - 05:25 PM

http://msdn.microsof...y/ms233763.aspx
this is how you can create a db in vs
for how to communicate with database inside C# you must look inside topic that i created here...it's
Null Values In Table After Saving Data
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 25 June 2012 - 06:29 PM

percis, you want to find the area of a rectangle which width/height are given. Why do you need a database to do that?
  • 0

#4 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 25 June 2012 - 06:51 PM

percis, you want to find the area of a rectangle which width/height are given. Why do you need a database to do that?

i get the values such as length and breadth from the database.
  • 0

#5 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 26 June 2012 - 12:47 AM

something similar to
SELECT width, height, (width * height) AS area FROM...

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 26 June 2012 - 04:14 AM

OK ... Let us assume that you have a database named 'Geometry' in SQL server which has a table named 'Rectangles' -- the table has two columns, width & height, of type int. Now our job is to retrieve that table data and show in a data-grid but with an extra column that will show the 'area' of the rectangle. So the steps are...
  • Create a SqlConnection object with the connection string for that database and open the connection.
  • Create a SqlAdapter object for that connection object and with the SQL query Orjan posted.
  • Create a DataTable object and ask the adapter to fill for that SQL query.
  • Add an extra column in the DataTable object -- 'Area'.
  • In a foreach loop, for each row, calculate the area from the width & height and update the each row of the DataTable object
  • Finally assign the DataTable object as a DataSource to the DataGridView.
And here is the code for the steps...

void DBTest()
{
   try
   {
      // Step 1
      System.Data.SqlClient.SqlConnection conn =
      new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Geometry;Integrated Security=True");
      conn.Open(); 

      // Step 2
      System.Data.SqlClient.SqlDataAdapter adapter =
new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM Rectangles", conn);

      // Step 3
      System.Data.DataTable table = new System.Data.DataTable();
      adapter.Fill(table);

      // Step 4
      table.Columns.Add("Area");

      // Step 5
      foreach (System.Data.DataRow row in table.Rows)
      {
            object[] cells = row.ItemArray;
            cells[2] = Convert.ToInt32(cells[0]) * Convert.ToInt32(cells[1]);
            row.ItemArray = cells;
      }

      // Step 6
      dataGridView1.DataSource = table;
   }
   catch (Exception exc)
   {
      Console.WriteLine("Error: " + exc.Message);
   }
}


  • 0

#7 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 26 June 2012 - 05:20 AM

my query does the calculation and adds the field area, kernelcoder, so that part shouldn't be needed in your code.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#8 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 26 June 2012 - 05:21 AM

my query does the calculation and adds the field area, kernelcoder, so that part shouldn't be needed in your code.

Agreed. So foreach loop can be removed.
  • 0

#9 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 26 June 2012 - 07:56 PM

OK ... Let us assume that you have a database named 'Geometry' in SQL server which has a table named 'Rectangles' -- the table has two columns, width & height, of type int. Now our job is to retrieve that table data and show in a data-grid but with an extra column that will show the 'area' of the rectangle. So the steps are...

  • Create a SqlConnection object with the connection string for that database and open the connection.
  • Create a SqlAdapter object for that connection object and with the SQL query Orjan posted.
  • Create a DataTable object and ask the adapter to fill for that SQL query.
  • Add an extra column in the DataTable object -- 'Area'.
  • In a foreach loop, for each row, calculate the area from the width & height and update the each row of the DataTable object
  • Finally assign the DataTable object as a DataSource to the DataGridView.
And here is the code for the steps...

void DBTest()
{
   try
   {
	  // Step 1
	  System.Data.SqlClient.SqlConnection conn =
	  new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Geometry;Integrated Security=True");
	  conn.Open();

	  // Step 2
	  System.Data.SqlClient.SqlDataAdapter adapter =
new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM Rectangles", conn);

	  // Step 3
	  System.Data.DataTable table = new System.Data.DataTable();
	  adapter.Fill(table);

	  // Step 4
	  table.Columns.Add("Area");

	  // Step 5
	  foreach (System.Data.DataRow row in table.Rows)
	  {
			object[] cells = row.ItemArray;
			cells[2] = Convert.ToInt32(cells[0]) * Convert.ToInt32(cells[1]);
			row.ItemArray = cells;
	  }

	  // Step 6
	  dataGridView1.DataSource = table;
   }
   catch (Exception exc)
   {
	  Console.WriteLine("Error: " + exc.Message);
   }
}


thaks a lot for the solution. I'm lost along the way. Is it possible to from step1 as where should i click in print screen and where should i enter the coding? Please....Im anxiously waiting for ur reply.
  • 1

#10 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 26 June 2012 - 08:36 PM

If you just want to execute the code successfully, you can call the DBTest method from your form's Load event handler. But note that you have a have a sql database and you have to change the connection string accordingly.
  • 0

#11 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 27 June 2012 - 12:12 AM

If you just want to execute the code successfully, you can call the DBTest method from your form's Load event handler. But note that you have a have a sql database and you have to change the connection string accordingly.


I'm lost. i really need help in setting up sql database/ any step from the beginning. Im using microsoft visual 2010. could u help me?
  • 0

#12 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 27 June 2012 - 12:47 AM

First you need to install, Microsoft SQL Server. Is it there installed on your machine? Note that, you also need to install SQL Management Studio. If yes, the rest is easy to create a database and table -- read this tutorial about it.

However, we can use Microsoft Access Database to store the height and width for rectangles. If you want to use MS-Access, we need to change the code a bit. Let us know which one you have successfully installed on your machine.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download