日期:2014-05-18  浏览次数:20963 次

SOS~~最后20分问个问题
从Excel   里读出一张表,把它影射到一个dataset   里面,
现在我想把这样dateset   里的内容全部插入SQL   server   2000里面,
代码我应该怎么写,请指教.谢~

------解决方案--------------------
#region 打开选中的Excel表
private void GetConnect()
{
string Inxls = this.txtExcel.Text;
string strConn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ' " + Inxls.ToString() + " ';Extended Properties = Excel 8.0 ";
OleDbConnection myconn = new OleDbConnection(strConn);
string strCom = "select * from [Sheet1$] ";
myconn.Open();
OleDbDataAdapter mycommand = new OleDbDataAdapter(strCom, myconn);
myDataSet = new DataSet();
mycommand.Fill(myDataSet, "[Sheet1$] ");
myconn.Close();
}
#endregion


#region 数据导入功能的实现
if (MessageBox.Show( "数据导入功能需要专业人员技术支持,请慎重操作!您确定要将Excel表里面的数据导入到数据库里面么? ", "提示 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
this.panel1.Visible = true;

strError = " ";
if (this.txtExcel.Text == " ")
{
strError += "请选择含有数据源的Excel表! " + "\r ";
}
if (this.ComShu.Text == " ")
{
strError += "请选择您要将Excel导入的数据表! " + "\r ";
}
if (strError.ToString() != " ")
{
MessageBox.Show(strError.ToString(), "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (strError.ToString() == " ")
{


//将数据表读取到DataGridView中

this.DataGrid1.Columns.Clear();
GetConnect();
DataGrid1.DataMember = "[Sheet1$] ";
DataGrid1.DataSource = myDataSet;

#region 数据表导入

SqlConnection conn = new SqlConnection(connstr);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
if (this.DataGrid1.ColumnCount.ToString() != "3 ")
{
MessageBox.Show( "导入数据失败,Excel表与处理表数据不匹配! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{

foreach (DataGridViewRow item in this.DataGrid1.Rows)
{
strError = " ";
cmd.CommandText = "AddToDispose ";
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter( "@PART ", item.Cells[1].Value));
cmd.Parameters.Add(new SqlParameter( "@DISPOSE ", item.Cells[2].Value));
try