Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,
i have a table in sql server management...with name Product..
that Product Table Contains pid,pname,description,rate attributes..
i have 100 records in Product Table..i didn't give description part while inserting attribute values...
so, now i want to insert that description attribute value..
i have a Excel with pid and description for 100 products..
now i need a script code to update description in Product Table using Excel, if Product Table PID is equals to Excel PID.....
Posted
Comments
pradiprenushe 20-Nov-13 7:56am    
Store ID & description in xls.
Read this article how to import data from xls.
http://www.codeproject.com/Articles/32581/Import-Data-from-Excel-to-SQL-Server
This article gives code for insertion you just have to change query to update instead of update.
Kumar Kovuru 21-Nov-13 0:35am    
i saw that article but it is showing error in con.open();
"Data Source=.\\sqlexpress;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes"
i was given like above, then where can i give my database user name and passord...
please help me regarding this...

Search in Google "Import data from Excel to Database in C#".

You just need to read the Excel file, run query for each row of Excel.
In that query, check if that ID exists in Table or not, if found, then update the Description field.
 
Share this answer
 
Comments
Kumar Kovuru 20-Nov-13 8:58am    
i did search, but i was confuse..
What is the confusion? Please elaborate.
Kumar Kovuru 21-Nov-13 0:37am    
i got this article in codeproject...
http://www.codeproject.com/Articles/32581/Import-Data-from-Excel-to-SQL-Server
but am getting error in con.open();
then where can i give my database user name and passord...
please help me regarding this...
Okay, let me check.
Check function "insertdataintosql" inside file "Excel2sql.aspx.cs".
The below line makes the connection...

SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;
AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");

It connects to a mdf file. As per your requirements, replace the Connection String.
Since this appears to be a one time update, I would suggest that you just export your Excel worksheet to either a text file or xml file. To create an XML File, follow the instructions here: http://office.microsoft.com/en-us/excel-help/create-an-xml-data-file-and-xml-schema-file-from-worksheet-data-HA010263509.aspx#BM1[^]

You could read an XML file in using something like this:
VB
Dim doc As New Xml.XmlDocument
' set the path to the XML data file
Dim docpath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Data.xml")
doc.Load(docpath)

For Each row As Xml.XmlNode In doc.GetElementsByTagName("Row") ' Row is the the tag created by the Excel XML export utility
      Dim pid As Xml.XmlElement = row("pid")
      Dim pid_value As Int32 = Int32.Parse(pid.InnerText) ' assumed that this is an integer
      Dim desc As Xml.XmlElement = row("description")
      ' update the database for this pid value with desc.innertext
Next
 
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