SqlConnection sqlConnection1 = null;    string filePath = FileUpload1.PostedFile.FileName;    sqlConnection1 = new SqlConnection();
    sqlConnection1.ConnectionString = "server=(local);integrated security=SSPI;initial catalog=work";    //import excel into SQL Server 2000
    /*string importSQL = "SELECT * into live41 FROM OpenDataSource" + 
        "('Microsoft.Jet.OLEDB.4.0','Data Source=" + "\"" + "E:\\022n.xls" + "\"" + 
        "; User ID=;Password=; Extended properties=Excel 5.0')...[Sheet1$]";*/    //export SQL Server 2000 into excel
    string exportSQL = @"EXEC master..xp_cmdshell
'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" +
        " -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'";   //重点是这句不知道是什么意思,整个代码调试了,没有出现问题 我运行了这些sp_configure 'show advanced options',1
//reconfigure
//go
//sp_configure 'xp_cmdshell',1
//reconfigure
//go
//reconfigure
//代码执行了,没有提示出错,但是没有看到我的那个execl的数据,求高人指点下    
    try
    {
        sqlConnection1.Open();
        
        //SqlCommand sqlCommand1 = new SqlCommand();
        //sqlCommand1.Connection = sqlConnection1;
        //sqlCommand1.CommandText = importSQL;
        //sqlCommand1.ExecuteNonQuery();
        //MessageBox.Show("import finish!");
        
        SqlCommand sqlCommand2 = new SqlCommand();
        sqlCommand2.Connection = sqlConnection1;
        sqlCommand2.CommandText = exportSQL;
        sqlCommand2.ExecuteNonQuery();
       Response.Write("export finish!");
    }
    catch(Exception ex)
    {
        Response.Write(ex.ToString());
    }
}

解决方案 »

  1.   

    到底要什么的数据,好像后面-p -c 是参数,你可以分析这个存储过程,更改-p -q - c 他们
      

  2.   

    调试的时候局部变量是这个
    EXEC master..xp_cmdshell\r\n'bcp Library.dbo.live41 out C:\\Documents and Settings\\Administrator\\桌面\\新建文件夹 (2)\\个体数据.xls-c -q -S\"\" -U\"\" -P\"\"'
    我想要我的这个个体数据。xls的这个里边的数据,但是这句话放在sql里边就错误
      

  3.   

    楼主哪里找的方法,具体做什么用
    master..xp_cmdshell 这里还".."改"."吧
    然后怎么还有"\r\n" 你回车换行了,改这样这么写试试
    string exportSQL = @"EXEC master.xp_cmdshell 'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" + " -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'";
    自己还是看看有没有这个function : xp_cmdshell
      

  4.   

    就是我想把Excel里边的数据导入sql里边去,就在网上找了这个,换有一种是把excel放入dataset里边然后放输入数据库的,这种方法好像如果硬盘格式不符合时不能上传的,所以我就试下这个
      

  5.   

    不知,没见过这个方法,excel导入sql中去还是很多方法的,你再找找吧