---
title: "Save Values in multiple tables at a time using C# and SQL SERVER 2008 R2"  
description: "In this blog I am creating a C# console application which is Insert Values at a time in multiple tables with SQL SERVER 2008 R2.Firstly we can two tab"  
author: "Vijay Shukla"  
published: 2012-12-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/404/save-values-in-multiple-tables-at-a-time-using-c-sharp-and-sql-server-2008-r2  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# Save Values in multiple tables at a time using C# and SQL SERVER 2008 R2

In this blog I am creating a C# [console application](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) which is Insert Values at a time in [multiple tables](https://www.mindstick.com/forum/159848/how-to-get-multiple-tables-using-entity-framework-core) with [SQL SERVER](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) 2008 R2.

Firstly we can two [tables in SQL](https://www.mindstick.com/forum/204/count-number-of-tables-in-sql-server) SERVER.

```
create table childName(      f_name varchar(50),      s_name varchar(50))create table parentName(      father_name varchar(50),      mother_name varchar(50))
```

Now after creating table we can create a [Store Procedure](https://www.mindstick.com/forum/193/auto-increment-by-store-procedure) Which is use to insert values in multiple tables.

```
CREATE PROCEDURE familyName(
    @FirstName varchar(50),    @SecondName varchar(50),    @FatherName varchar(50),    @MotherName varchar(50) )
AS
    SET NOCOUNT ON;    insert into dbo.childName values(@FirstName,@SecondName)    insert into dbo.parentName values(@FatherName,@MotherName)
GO
```

Store Procedure name is familyName.

After creating the Store Procedure I create a [C# console](https://www.mindstick.com/forum/160515/how-to-read-data-from-text-files-in-the-c-sharp-console-application) application.

```
using System;using System.Data.SqlClient;class MultipleTable{
   public static void Main(String []args)   {      try      {      Console.WriteLine();      Console.Write("First Name:: ");      string f_name=Console.ReadLine();      Console.WriteLine();      Console.Write("Last Name:: ");      string l_name=Console.ReadLine();      Console.WriteLine();      Console.Write("Father Name:: ");      string Father_name=Console.ReadLine();      Console.WriteLine();      Console.Write("Mother Name:: ");      string Mother_name=Console.ReadLine();      SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=master;Integrated Security=SSPI;User ID=sa;Password=password;");
      con.Open();
                  SqlCommand cmd = new SqlCommand("EXEC familyName'" +f_name+ "','" + l_name+ "','" +Father_name+ "','" +Mother_name+ "';",con);
                  cmd.ExecuteNonQuery();                  con.Close();      Console.WriteLine();      Console.WriteLine("Save Successfully");      }      catch(Exception e)      {            Console.Write("Error"+e);
      }
   }
}
```

After That compile this console application on the [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) [command prompt](https://answers.mindstick.com/qa/51716/how-to-make-bootable-pendrive-using-cmd-windows-command-prompt).

\
![Save Values in multiple tables at a time using C# and SQL SERVER 2008 R2](https://www.mindstick.com/blogs/1b195cae-53d7-411b-9a97-79f69df44a95/images/369343ab-0c23-4bf4-932b-23783f4c9230.png)

now check your table values saved in your multiple table.

![Save Values in multiple tables at a time using C# and SQL SERVER 2008 R2](https://www.mindstick.com/blogs/1b195cae-53d7-411b-9a97-79f69df44a95/images/0413fa58-0a2b-44a8-b332-da5855af9899.png)

---

Original Source: https://www.mindstick.com/blog/404/save-values-in-multiple-tables-at-a-time-using-c-sharp-and-sql-server-2008-r2

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
