问题描述
小弟开始.net的开发。今天领导让我做个sql2005查询后导出xls。我在网上查了一段代码。然后自己该了该。现在有错了。我实在是不懂,来请各位帮帮小弟我。protectedvoidButton1_Click(objectsender,EventArgse){DataSetset=newDataSet();stringcmdText="selectqname,quniversity,q1,q2,q3,q4,q5,q6,q7,q8,q9,adddateFROMQuestionwherenid=1";set=SqlHelper.ExecuteDataSet(SqlHelper.strcon,CommandType.Text,cmdText,null);stringpath=HttpContext.Current.Request.ApplicationPath+"/images/";stringstr3=HttpContext.Current.Server.MapPath(path);stringstr4=Guid.NewGuid()+".xls";OleDbConnectionconnection=newOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+str3+str4+";ExtendedProperties=Excel8.0;");OleDbCommandcommand=newOleDbCommand();command.Connection=connection;command.Connection.Open();command.CommandText="Createtabletables([Name]varchar(250),[University]varchar(250),[Q1]varchar(250),[Q2]varchar(250),[Q3]varchar(250),[Q4]varchar(250),[Q5]varchar(250),[Q6]varchar(250),[Q7]varchar(250),[Q8]varchar(250),[Q9]varchar(250),[adddate]varchar(250))";command.ExecuteNonQuery();command.CommandText="insertintotables([Name],[University],[Q1],[Q2],[Q3],[Q4],[Q5],[Q6],[Q7],[Q8],[Q9],[AddTime]VALUES(@qname,@quniversity,@q1,@q2,@q3,@q4,@q5,@q6,@q7,@q8,@q9,@adddate)";//command.Parameters.Add(newOleDbParameter("@ID",OleDbType.Integer));command.Parameters.Add(newOleDbParameter("@qname",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@quniversity",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q1",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q2",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q3",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q4",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q5",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q6",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q7",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q8",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@q9",OleDbType.VarChar));command.Parameters.Add(newOleDbParameter("@adddate",OleDbType.VarChar));foreach(DataRowrowinset.Tables[0].Rows){for(inti=0;i<command.Parameters.Count;i++){command.Parameters[i].Value=row[i];}command.ExecuteNonQuery();}command.Connection.Close();HttpResponseresponse=HttpContext.Current.Response;response.Clear();response.WriteFile(str3+str4);stringstr6="attachment;filename=Shuju.xls";response.AppendHeader("Content-Disposition",str6);response.Flush();File.Delete(str3+str4);response.End();}
报错说我insert有错图片不会传。。就上文字了。“/MSRA”应用程序中的服务器错误。--------------------------------------------------------------------------------INSERTINTO语句的语法错误。说明:执行当前Web请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.Data.OleDb.OleDbException:INSERTINTO语句的语法错误。源错误:行51:command.Parameters[i].Value=row[i];行52:}行53:command.ExecuteNonQuery();行54:}行55:command.Connection.Close();源文件:d:系统文件我的文档VisualStudio2010WebSitesMSRATestQb.aspx.cs行:53堆栈跟踪:[OleDbException(0x80040e14):INSERTINTO语句的语法错误。]System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResulthr)+1070856System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMSdbParams,Object&executeResult)+247System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&executeResult)+194System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehaviorbehavior,Object&executeResult)+58System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehaviorbehavior,Stringmethod)+167System.Data.OleDb.OleDbCommand.ExecuteNonQuery()+113Qb.Button1_Click(Objectsender,EventArgse)ind:系统文件我的文档VisualStudio2010WebSitesMSRATestQb.aspx.cs:53System.Web.UI.WebControls.Button.OnClick(EventArgse)+118System.Web.UI.WebControls.Button.RaisePostBackEvent(StringeventArgument)+112System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(StringeventArgument)+10System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandlersourceControl,StringeventArgument)+13System.Web.UI.Page.RaisePostBackEvent(NameValueCollectionpostData)+36System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+5563--------------------------------------------------------------------------------版本信息:Microsoft.NETFramework版本:4.0.30319;ASP.NET版本:4.0.30319.1
解决方案
本帖最后由 w23p2010 于 2012-06-13 17:03:36 编辑
解决方案二:
怎么没有人呢。。各位前辈。。帮帮小弟。。。
解决方案三:
错误找到了。就是少了半拉括号。。。
解决方案四:
恭喜LZ可以结贴了有些问题自己仔细看看就能解决的很少在这发帖了人气不行了
解决方案五:
,[AddTime]VALUES之间少了),[AddTime])VALUES