I have a word document with a table and three columns and each columns have certain data, it may be images also. I want to collect each row data and insert into the database. Can you please help me how to read the column of the table present in the document and store the value in a dataset. I am attaching a sample document in reference.
Regards
Sai
Hi
Thanks for your request. I think that you can try using the following code as an example.
// Create connection
string connectionString = "server=Web1;database=TestDB;uid=sa;pwd=dsa43r;";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
string commandString = "INSERT INTO TestTable ([FirstName], [LastName], [Photo]) VALUES (@FirstName, @LastName, @Photo)";
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);
// Open document
Document doc = new Document(@"Test137\in.doc");
// Get table from document
Table docTable = doc.FirstSection.Body.Tables[0];
using (conn)
{
// Open connection to datat base
conn.Open();
// Loop through all rows in the table
foreach (Row row in docTable.Rows)
{
// Exclude first row (this row containf field names)
if (!row.Equals(docTable.FirstRow))
{
command.Parameters.Clear();
// Add parameters
command.Parameters.Add("FirstName", row.Cells[0].ToTxt());
command.Parameters.Add("LastName", row.Cells[1].ToTxt());
// Check if the third cell containf image
NodeCollection shapes = row.Cells[2].GetChildNodes(NodeType.Shape, true, false);
if (shapes.Count > 0 && (shapes[0] as Shape).HasImage)
// If so add image bytes as value of query parameter
command.Parameters.Add("Photo", (shapes[0] as Shape).ImageData.ImageBytes);
else
// Else use NULL value
command.Parameters.Add("Photo", null);
// Execute commant
command.ExecuteNonQuery();
}
}
}
I hope this could help you.
Best regards.
Thanks a lot