1

Currently i am working on c# .net and i need to generate scripts ( sql insert scripts ) by using the data that is present in an excel sheet.

To be more specific,

If the excel sheet has three columns with

**column name** ColumName1, ColumName2, ColumName3 
**data like**   Value1,     Value2,     Value3

i need to write code to generate the insert script like -

INSERT [dbo].[TableName] ([ColumName1], [ColumName2], [ColumName3]) VALUES ('Value1', 'Value2', 'Value3')

Any ideas ?

2
  • do you want to create script in the excel sheet or some where else? Commented Jan 12, 2013 at 5:15
  • @krshekhar I just need the scripts to be generated. I must be able to run the scripts in my database (sql server). So its not a problem where it can be generated. I just need the scripts. You can do it your own way. Thanks. Commented Jan 16, 2013 at 5:48

3 Answers 3

1

You would need to write a forumla for the final column for instance:

="insert into tblyourtablename (intmine, strval) values ("&B4&", N'"&C4&"'); set @intpane = scope_identity(); INSERT INTO tblpane (nameid_fk,strtext,bitmine,vbarmine) VALUES (@intpane ,N'"&D8&"' ,0 ,convert(varbinary,''));"

This should get your started

EDIT

Why do some SQL strings have an 'N' prefix? You may have seen Transact-SQL code that passes strings around using an N prefix. This denotes that the subsequent string is in Unicode (the N actually stands for National language character set). Which means that you are passing an NCHAR, NVARCHAR or NTEXT value, as opposed to CHAR, VARCHAR or TEXT. See Article #2354 for a comparison of these data types.

For further reading please view the following link

http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.html

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response. I need to know what does N' exactly mean and what is it's operation here ?
Please see me edit for the explaination for having an N, I do not know your column datatypes but N is not necessary.
Thanks for the edit. It has the relevant information. And also i found this article that is really giving a best piece of understanding about our N' discussion. support.microsoft.com/kb/239530 'N - Its for Unicode data and stands for National language character set as you said. But it is not a mandatory one and depends on our requirement.
0
var myConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Language_Batch1_OneClick.xls';Extended Properties=Excel 8.0;"); ;
var myCommand = new OleDbCommand();
var upCommand = new OleDbCommand();
int i = 0;
try
{

    string sql = null;
    string Value1 =null;
    string Value2=null;
    string Value3=null;
    myConnection.Open();
    myCommand.Connection = myConnection;
    sql = "select ColumName1,ColumName1from,ColumName3  [sheet-name]";
    myCommand.CommandText = sql;
    var dataReader = myCommand.ExecuteReader();
    {
      value1=dataReader["ColumName1"].ToString();
      value2=dataReader["ColumName2"].ToString();
      value3=dataReader["ColumName3"].ToString();

    string newQuery="INSERT [dbo].[TableName] ([ColumName1], [ColumName2], [ColumName3]) VALUES ('"+Value1+"', '"+Value2+"', '"+Value3+"')"
    }

}

2 Comments

Thank you for responding and for the explanation.
you are welcome and please marked the answer it it helped you to solved your problem.
0

Read excel and get data ,then generate insert script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.