Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > inserting excel data into sql server through vb.net
 

inserting excel data into sql server through vb.net

I have written the following code to import data from excel to sql server. But the problem is when i try to insert new data into the table it just reports an error . I am using winfows forms and following code in vb.net

Dim si As String = ListBox1.SelectedItem

Dim fn As String = fname '"290000..xls" 'fname

'Dim connectionstring As String = "Data Source=.\sqlexpress;Initial Catalog=stats.mdf;integrated security=true;"

Dim connectionstring As String = "Server=aabc;Database=statlogs;integrated security=true"

Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionstring)

Dim cmdstr As String

connection.Open()

Try

Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand("Insert", connection)

command.CommandType = CommandType.Text

MessageBox.Show(fn)

command.CommandText = "select * INTOxyz FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0'," + "'Excel 8.0;Database=" + fname + "'" + ",'SElect * FROM [" + ListBox1.Text.ToString + "$]" + "')"

command.ExecuteNonQuery()

Catch ex As Exception

Console.WriteLine(ex.Message)

MessageBox.Show(ex.ToString)

Finally

connection.Close()

End Try

MessageBox.Show("File successfully inserted")

omar_aa  Saturday, April 19, 2008 9:09 PM

Hi,

With this error information, it’s much easier to diagnose the problem.

In some cases, a Command object has parameters which contain information needed for the execution of this command. These command parameters must be specified in the code. The error exactly asks you to give parameters to the command object.

I hope this document Configuring Parameters (ADO.NET) could help you.

Regards,

Jacob

Jacob Sui - MSFT  Thursday, April 24, 2008 10:24 AM

Hi,

I can't reproduce the scenario since I don't know thecontent of the EXCEL file and the schema of the statlogs.xyz table. The following are some general guidanceof trouble shooting your problem.

1. Check if you can run the insert SQL commend alone in the SQL Server Management Studio by doing this: set a break point at command.ExecuteNonQuery and run your program in the debug mode. When the program pauses at the break point, copy the value of the command.CommandText object and paste the text string in the SQL Server and execute this command seperately.

a. If this command executes successfully, itmeans that the syntax of this command is correct.The data in the Excel file are compatible with the schema of your database. The insertion doesn't violates any database constraints.

b. If this command fails, check that line of code.

2. Show us the detail information of the exception thrown by your application.

Regards,

Jacob

Jacob Sui - MSFT  Thursday, April 24, 2008 3:22 AM

HI!

I have tried all of the things you have written above but i am getting the following error:

OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "No value given for one or more required parameters.".

Msg 7320, Level 16, State 2, Line 1

Cannot execute the query "SElect First_name,ssn,date FROM [Sheet1$]" against OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".

Thanks

Best Regards

Awais Omar

omar_aa  Thursday, April 24, 2008 5:26 AM

Hi,

With this error information, it’s much easier to diagnose the problem.

In some cases, a Command object has parameters which contain information needed for the execution of this command. These command parameters must be specified in the code. The error exactly asks you to give parameters to the command object.

I hope this document Configuring Parameters (ADO.NET) could help you.

Regards,

Jacob

Jacob Sui - MSFT  Thursday, April 24, 2008 10:24 AM

but how do i can configure command objects because i dun't how to do this??

omar_aa  Thursday, April 24, 2008 10:28 AM

Hi,

If you read the link I gave you, you will find sample code telling you how to deal with command objects and their parameters. Although the sample code uses the SQLCommands and SQLParameters, but they all inherit from the Command and Parameter base class. That is to say, the Command and Parameter class for OLEDB function the same.

You will do something like this:

command.Parameters.Add(yourParameters);

Regards,

Jacob

Jacob Sui - MSFT  Friday, April 25, 2008 2:59 AM

You can use google to search for other answers

Custom Search

More Threads

• Control like firefox's Bookmarkmanager
• I can't see de Data menu, in order to add new data Source
• Details ComboBox Problem [Fixed]
• re: Print only visible columns
• DataGridView events firing oddly
• ListBox and Hashtable
• Filter DGV by combobox programmatically
• IDataGridViewEditingControl
• Need practical examples of row validation and error handling on DataGridView
• DataGridView.CurrentCell = differentCell causes infinite loop