i have a problem in my code for copy data from excell to sql using desktop appliction
i find error
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
and i don't know what is the problem
the excell file include one sheet include only 4 rows and the remain is empty:
george 1 1 1
willum 1 1 1
mickel 1 1 1
jinnefr 1 1 1
where all column types in sheet ara general
and sql database include table called childern include 9 columns
and i use 4 columns and datatypes ara in order (varchar(50)---smallint----tinyint----tinyint) not allow null
but the other coluns allow null
my code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
namespace ExcellToSql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Mode='Write';Integrated Security=SSPI;Data Source=D:\\MyData.xlsx;Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", con);
OleDbDataReader odr = cmd.ExecuteReader();
string name = " ";
short country = 0;
byte status = 0;
byte type = 0;
while (odr.Read())
{
name = valid(odr, 0);
country = short.Parse(valid(odr, 1));
status = byte.Parse(valid(odr, 2));
type = byte.Parse(valid(odr, 3));
insertdataintosql(name, country, status, type);
}
con.Close();
}
public void insertdataintosql(string name, short country, byte status, byte type)
{
SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;AttachDbFileName=|DataDirectory|exceltosql.mdf;Trusted_Connection=yes");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into Client(name,country,status,type) values(@name,@country,@status,@type)";
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
cmd.Parameters.Add("@country", SqlDbType.SmallInt).Value = country;
cmd.Parameters.Add("@status", SqlDbType.TinyInt).Value = status;
cmd.Parameters.Add("@type", SqlDbType.TinyInt).Value = type;
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected string valid(OleDbDataReader myreader, int stval)
{
object val = myreader[stval];
if (val != DBNull.Value)
return val.ToString();
else
return Convert.ToString(0);
}
}
}
so i need any one tell me how i can solve this error