sql数据库中的纪录导出保存成文件 如题。VS2005编写的网站,要求实现这样的功能,具体步骤如何?能否实现导出文件加密? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个功能用编程肯定是能实现,但用数据库本身的命令速度和效率肯定更高如果是SQL Server ,楼主可以google一下bcp命令 可以先把数据绑定到gridview控件上,然后再把数据用Excel文件的形式导出来,网上有例子的 要求是点击一个按钮,然后把纪录导出,这个按钮事件里能调用“bcp命令”吗? 应该可以,使用 System.Diagnostics.Process.Start(@"某路径下的\bcp ...详细指令"); “某路径下的\bcp ...详细指令”这个是如何写的? 可以的,导出成CSV格式............................具体做法要搜搜....很早以前做过. bcp "select * from database_name.dbo.userproduct" queryout "D:\Documents and Settings\Administrator\桌面\2008-6-7.xls" -c -q -S"server_name" -U"sa" -P"***"//ORbcp "database_name.dbo.userproduct" out "D:\Documents and Settings\Administrator\桌面\2008-6-7.xls" -c -q -S"server_name" -U"sa" -P"***" 谢谢ls的回答。现在直接在查询分析器里可以用“bcp”了。但是在程序里还是不行啊,string sql = "EXEC master..xp_cmdshell 'bcp [xxx].[dbo].userproduct out d:\\DT1.txt -c -S -Usa -P'"; //下面是执行查询部分 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["webCon"].ConnectionString); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open();conn.Close();执行后不报错,但“DT1.txt”里没纪录。另外如果导出成“.xls”文件,日期和原来不同了。如数据库里日期“2008-5-7”在导出文件里就成了“0:00:00”。求解。 这个应该不是DML语句,不能用CMD,你用启用进程bcp试试?lz问的问题都不错,都是我不会的.谢谢了. System.Diagnostics.Process.Start这个应该可以调用BCP命令啊. 有一个比较简单的方法,我一直在用思路是这样的1、将 GridView 的内容变成字串2、将上述字串变成 Steam 流提供给用户下载3、文件名称可以是 .htm 或者是 .xls(Excel 可以打开 HTML)上述功能,实际上是我把页面所看到的报表,用文件下载的形式让用户保存因为这不是我的电脑,没有代码贴给你看 现在搞得可以导出xls文件了。日期格式也对,但是一条纪录都写在一个单元格里了。而且导回数据库报“找不到可安装的isam”的错。再次求解。用“有一个比较简单的方法,我一直在用 思路是这样的 1、将 GridView 的内容变成字串 2、将上述字串变成 Steam 流提供给用户下载 3、文件名称可以是 .htm 或者是 .xls(Excel 可以打开 HTML) ”这个方法搞得文件导回数据库报“外部表不是预期的格式”的错误。 现在用{ SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["webCon"].ConnectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlConn; SqlDataAdapter sda = new SqlDataAdapter(cmd); sqlConn.Open(); cmd.CommandText = "Select * from userproduct1"; DataSet ds = new DataSet(); sda.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { string BcpExec = ""; for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//循环取本地文件名 { BcpExec = @"bcp [cangku].[dbo].userproduct1 out d:\\DT1.xls"; //BcpExec += ds.Tables[0].Rows[i]["path"].ToString(); BcpExec += " -S -Usa -P -c -t,";//组合bcp命令 //Response.Write();//执行bcp命令并显示操作结果 ExeCommand(BcpExec); } Response.Write("共" + ds.Tables[0].Rows.Count + "条数据导出成功!"); } } /**/ /// <summary> /// 执行Cmd命令 /// </summary> /// <param name="commandText"></param> /// <returns></returns> public static string ExeCommand(string commandText) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string strOutput = null; try { p.Start(); p.StandardInput.WriteLine(commandText); p.StandardInput.WriteLine("exit"); strOutput = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); } catch (Exception e) { strOutput = e.Message; } return strOutput; }可以 继续求正则表达式 期末课程设计 暑期培训的选择? 关于 EXCEL导出报错的问题, 我用VS2005测试都没问题 架到服务器上就报错 请进 ASP.NET权限管理框架,可以在线体验UI效果 asp.net 是怎么知道我点击了那个按钮? c#,vb的项目可以合并呢? asp.net如何调用JS文件? sql 条件查询语句 怎么把2007-12-14 14:38转换成毫秒 如(1186988194578) 关于一个树型递归算法 ! 高手来研究下! 谢谢!急用! 关于实现下载功能
但是在程序里还是不行啊,
string sql = "EXEC master..xp_cmdshell 'bcp [xxx].[dbo].userproduct out d:\\DT1.txt -c -S -Usa -P'"; //下面是执行查询部分
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["webCon"].ConnectionString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
conn.Close();
执行后不报错,但“DT1.txt”里没纪录。
另外
如果导出成“.xls”文件,日期和原来不同了。如数据库里日期“2008-5-7”在导出文件里就成了“0:00:00”。
求解。
思路是这样的1、将 GridView 的内容变成字串
2、将上述字串变成 Steam 流提供给用户下载
3、文件名称可以是 .htm 或者是 .xls(Excel 可以打开 HTML)上述功能,实际上是我把页面所看到的报表,用文件下载的形式让用户保存
因为这不是我的电脑,没有代码贴给你看
再次求解。
用
“有一个比较简单的方法,我一直在用
思路是这样的 1、将 GridView 的内容变成字串
2、将上述字串变成 Steam 流提供给用户下载
3、文件名称可以是 .htm 或者是 .xls(Excel 可以打开 HTML)
”
这个方法搞得文件导回数据库报“外部表不是预期的格式”的错误。
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["webCon"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sqlConn.Open();
cmd.CommandText = "Select * from userproduct1";
DataSet ds = new DataSet();
sda.Fill(ds); if (ds.Tables[0].Rows.Count > 0)
{
string BcpExec = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//循环取本地文件名
{
BcpExec = @"bcp [cangku].[dbo].userproduct1 out d:\\DT1.xls";
//BcpExec += ds.Tables[0].Rows[i]["path"].ToString();
BcpExec += " -S -Usa -P -c -t,";//组合bcp命令
//Response.Write();//执行bcp命令并显示操作结果
ExeCommand(BcpExec);
}
Response.Write("共" + ds.Tables[0].Rows.Count + "条数据导出成功!");
}
} /**/
/// <summary>
/// 执行Cmd命令
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
可以