Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends,
i have a project in c#, i need to insert multiple names of students and store all of those names on button click.
i have load data into gridview but is it possible to store records into database from gridview.
if yes, please tell me how ?
if no, what should be the alternate method. Please suggest with the code.

with regards
bishnu
Posted
Updated 28-Nov-11 17:39pm
v2

You can insert multiple values in SQL database table. following is the Query syntax

SQL
--in SQL 2008
insert into TABLENAME (COLUMNNAME) values (VALUE1), values (VALUE2), values (VALUE3)

--in SQL 2005
insert into TABLENAME (COLUMNNAME) select 'VALUE1' union select 'VALUE2' union select 'VALUE3'


OR

You can fetch Datatable from Gridview and update to database directly

Copy Data from a DataTable to a SQLServer Database using SQLServer Management Objects and SqlBulkCopy[^]

http://social.msdn.microsoft.com/Forums/zh/csharpgeneral/thread/7ab88e7d-2a7c-4554-a955-c9863c605a7d[^]
 
Share this answer
 
You can do it multiple ways..

1) With Direct call form Source

foreach (Employee item in employees)
{

   if (iCounter == 0)
  {
    cmd.BeginTransaction;
  }
  string sql = @"INSERT INTO Mytable (id, name, salary) 
    values ('@id', '@name', '@salary')";
 //add parameters
  cmd.CommandText = sql; 
  cmd.ExecuteNonQuery();
  iCounter ++;
  if(iCounter >= 500)
  {
     cmd.CommitTransaction;
     iCounter = 0;
  }
}

if(iCounter > 0)
   cmd.CommitTransaction;


2) Linq
C#
var db = new StoreDataContext();
using(var scope = new TransactionScope()){
    foreach(var emp in employee){
        db.employee.Insert();
        db.SubmitChanges(ConflictMode.FailOnFirstConflict);
    }
    scope.Complete();
}


3) Using Xml insert

Build an xml string, or convert the dataset to xml, and pass it to the database, and iterate in xml


CREATE PROCEDURE [dbo].[multipleInsert]
(
	@xmlData NVARCHAR(max)
)
AS
BEGIN
INSERT INTO employee (id, name, dob)
	SELECT		
	node.trnl.value('id', 'varchar(10)') id,
	node.trnl.value('name', 'nvarchar(100)') name,
	node.trnl.value('dob', 'nvarchar(8)') dob,
FROM @xml.nodes('/root/employee') node(trnl)

<root>
		<employee id="00163500C6" xml:space="preserve">
		<name>Rafeeq</name>
		<dob>12-12-2011</dob>    
	  </empoloyee>
	 </root>
END
 
Share this answer
 
On the second part of your question do some research on 'Bound DataGridView'. It's something C# does very well, where it mirrors what you do in the grid on the database.
 
Share this answer
 
you can try something like this

C#
for(int i = 0; i<gridview1.rows.count; i++)
{
// put your insert query here
}
 
Share this answer
 
v3
Comments
[no name] 29-Nov-11 1:57am    
EDIT: added "pre" tag
Iterate until you get the count of the items you are trying to insert, just like this:

C#
public void InsertData(string name)
{
   for(int ctr=0; ctr < maxCount; ctr++)
   {
      //DO CALL FOR INSERT WITH DYNAMIC PARAMETERS
      InsertData(name);
   }
}


where maxCount is the number of names you like to insert

-Eduard
 
Share this answer
 
v3
u can use SqlBulkCopy.WriteToServer Method (DataTable).

http://msdn.microsoft.com/en-us/library/ex21zs8x.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900