本文介绍了','附近的语法不正确。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是错误的代码','附近的语法不正确。 我尝试了什么: public partial class CryReport:System.Web.UI.Page { public string UserName; SqlConnection con = new SqlConnection( @ 数据源= METHOUN;初始目录= ITReportDb;集成安全性=真); ReportDocument crypt = new ReportDocument(); 受保护 void Page_Load( object sender,EventArgs e) { DateTime strDate = Convert.ToDateTime(Request.QueryString [ DateFrom]); DateTime endDate = Convert.ToDateTime(Request.QueryString [ DateTo]); string txtUserName = Request.QueryString [ 用户名]; GenerateReport(strDate,endDate); } 受保护 void GenerateReport(DateTime strDate,DateTime endDate ) { con.Open(); // SqlCommand cmd = new SqlCommand(SELECT * FROM tblReport WHERE Date at'+ strDate + '和'+ endDate +',con); SqlCommand cmd = new SqlCommand( SELECT * FROM tblReport WHERE' + strDate + ',' + endDate + ',' + UserName + ',con); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataTable datatable = new DataTable(); da.Fill(datatable); // 根据imageID和fill数据集获取值 con.Close(); ReportDocument crystalReport = new ReportDocument(); // 创建水晶报表对象 crystalReport.Load(Server.MapPath( CrystalReport1.rpt)); // 报告路径 crystalReport.DataDefinition.FormulaFields [ DateForm]。文本= ' + strDate.ToString()+ '; crystalReport.DataDefinition.FormulaFields [ DateTo]。Text = ' + endDate.ToString()+ '; crystalReport.DataDefinition.FormulaFields [ UserName]。Text = ' + UserName.ToString()+ '; crystalReport.SetDataSource(datatable); // binding datatable CrystalReportViewer1.ReportSource = crystalReport; } } 解决方案 永远不要通过连接用户输入来构建SQL查询,它被命名为SQL注入,它对您的数据库很危险并且容易出错。 名称中的单引号和程序崩溃。 SQL注入 - 维基百科 [ ^ ] SQL注入 [ ^ ] What,s wrong this codes Incorrect syntax near ','.What I have tried:public partial class CryReport : System.Web.UI.Page { public string UserName; SqlConnection con = new SqlConnection(@"Data Source=METHOUN;Initial Catalog=ITReportDb;Integrated Security=True"); ReportDocument crypt = new ReportDocument(); protected void Page_Load(object sender, EventArgs e) { DateTime strDate = Convert.ToDateTime(Request.QueryString["DateFrom"]); DateTime endDate = Convert.ToDateTime(Request.QueryString["DateTo"]); string txtUserName = Request.QueryString["UserName"]; GenerateReport(strDate,endDate); } protected void GenerateReport(DateTime strDate, DateTime endDate) { con.Open(); //SqlCommand cmd = new SqlCommand("SELECT * FROM tblReport WHERE Date between'" + strDate + "'and'" + endDate + "'", con); SqlCommand cmd = new SqlCommand("SELECT * FROM tblReport WHERE Date between' " + strDate + "','" + endDate + "','"+UserName+" ' " , con); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataTable datatable = new DataTable(); da.Fill(datatable); // getting value according to imageID and fill dataset con.Close(); ReportDocument crystalReport = new ReportDocument(); // creating object of crystal report crystalReport.Load(Server.MapPath("CrystalReport1.rpt")); // path of report crystalReport.DataDefinition.FormulaFields["DateForm"].Text = "'" + strDate.ToString() + "'"; crystalReport.DataDefinition.FormulaFields["DateTo"].Text = "'" + endDate.ToString() + "'"; crystalReport.DataDefinition.FormulaFields["UserName"].Text = "'" + UserName.ToString() + "'"; crystalReport.SetDataSource(datatable); // binding datatable CrystalReportViewer1.ReportSource = crystalReport; } } 解决方案 这篇关于','附近的语法不正确。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 20:11