一次性全部导入SQL
然后再判断哪些需要删除
这样方便一点
好像

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Web;namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid dataGrid1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid1
    // 
    this.dataGrid1.DataMember = "";
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Location = new System.Drawing.Point(16, 32);
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(664, 464);
    this.dataGrid1.TabIndex = 0;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(688, 494);
    this.Controls.Add(this.dataGrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());


    } private void wirteSql()
    {
    DataTable tb;
    tb =OLEEXCEL().Tables[0];


    string strCreateTable = " create table table1 (";
    //strCreateTable = tb.Columns[0].ColumnName
    for(int col=0;col<tb.Columns.Count;col++)
    {

    if(col == tb.Columns.Count - 1)
    {
    strCreateTable = strCreateTable +" [" +tb.Columns[col].ColumnName + "]"+" varchar(50))";
    }
    else
    {
    strCreateTable = strCreateTable +" [" +tb.Columns[col].ColumnName + "]"+" varchar(50),";
    }
    }
    MessageBox.Show(strCreateTable);
    string sqlInsert="";
    for(int i=0;i<tb.Rows.Count;i++)
    {
    string colvalue="";
    for(int j =0;j<tb.Columns.Count;j++)
    {
    if(tb.Rows[i][j] == null)
    colvalue = "null";
    if(j == tb.Columns.Count -1)
    {
    colvalue =colvalue+"'"+tb.Rows[i][j].ToString()+"'"  ;
        }
    else
    {
    colvalue = colvalue+"'"+tb.Rows[i][j].ToString()+"'" + ",";
    }
    }
    sqlInsert = sqlInsert+ "insert into table1 values("+colvalue+")";
    }
    MessageBox.Show(sqlInsert);
    SqlConnection conn = new SqlConnection ("server=(local);database=pubs;uid=sa;pwd = sa;");
    conn.Open();
    try
    {
    SqlCommand cmd = new SqlCommand ();
    cmd.Connection = conn;
    cmd.CommandText = strCreateTable+sqlInsert;
    cmd.ExecuteNonQuery();
    }
    catch(Exception e)
    {

    MessageBox.Show(e.Message);
    }
    conn.Close();
    }

    private DataSet OLEEXCEL()
    {
    OleDbConnection oleDbConnXls = new OleDbConnection ();
    string   str;   
    str="Provider=Microsoft.Jet.OLEDB.4.0;";   
    str+="Data Source=E:\\001.xls;";
    str+="Extended Properties=Excel 8.0;";   
    //oleDbConnXls已申明;   
    oleDbConnXls.ConnectionString=   str;   
    try   
    {   
    oleDbConnXls.Open();   
    }   
    catch   
    {   
    MessageBox.Show("打开数据库失败!\n请检查数据库服务器后重新运行!");   
      
    }   
       
    OleDbDataAdapter oleda = new OleDbDataAdapter ("select *  from [aaa$]",oleDbConnXls);
    DataSet ds = new DataSet ();
    oleda.Fill(ds); 
    if(oleDbConnXls!=null && oleDbConnXls.State ==  ConnectionState.Open)
    {
    oleDbConnXls.Close();
    } dataGrid1.DataSource = ds.Tables[0];
    return ds;
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    OLEEXCEL();
    wirteSql();
    }
    }
    }
      

  2.   

    insert into abc(aa,bb,cc) select aa,bb,cc from opendatasource('Microsoft.Jet.OLEDB.4.0','data source="(Excel在服务器上的绝对路径)";Extended Properties=Excel 8.0 ')...[Sheet1$]
    此方法中的Excel文件必须和数据库在同一台服务器上,如果Excel跟数据库不在同一台服务器上,可以先将Excel文件上传到服务器。
    两个红色的地方必须一样
    而且Excel文件的第一行必须是和数据库的字段名一致。也就是说数据库里的字段是aa,bb,cc,那么Excel里的第一行必须是aa,bb,cc,从第二行开始是真正要导入的数据
      

  3.   

    谢谢大家,好像leon520qqq0 的比较好,我也是这种思想,做一下先,有问题在请教大家
      

  4.   

    TO:leon520qqq0 OLEEXCEL()
    在  oleda.Fill(ds)  这里提示错误:“用户代码未处理 Oledbexception   找不到可安装的ISAM。”