主题:[讨论]请各位大哥看看下面代码,指出错误!!!
我要搞一个水晶报表, 在水晶报表的左边是一个cmb2,textBox1,button1,button2,
我使用的数据库是 access , 想要实现的查询是cmb2下拉4个选项:正刊安装材料,正刊土建材料,正刊园林材料,正刊市政材料, 通过 cmb2 选择和 textBox1中输入的字:如 电线
在水晶报表中显示出.
但我在下面的代码中,debug后,进行查询后,却是全部材料显示,而非是我想要的查询显示,请各位高手指点:
( 另外,我是通过水晶报表向导创建报表的, 通过创建新连接,选择jet 4.0 ole db 然后找到这个xhbook.mdb 文件, 下一步,locale identfier 1033 完成,选定的表中选xhbook1,下一步,要显示的字段全显,后面的分组依据等都空着,最后选表,它就自已产生一个表了,里面就已能预览到数据了.)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using CrystalDecisions.CrystalReports.Engine;
using aeon.Library.DbProc;
namespace CrystalReportsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
OpenDb();
InitializeComponent();
}
OleDbConnection myConnection;
//建立OleDbConnection连接对象
OleDbDataAdapter myAdapter;
//建立OleDbCommand对象
//建立DataSet对象
DataTable mytable;
//定义一个整型变量来存放当前行数
public bool OpenDb()
{
String SQLstr = "";
//定义SQL查询字符串
SQLstr = "select * from xhbook1";
//建立SQL查询语句
try
{
myConnection = new OleDbConnection("Provider="
+ "Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\Common Files\\xhbook.mdb");
myAdapter = new OleDbDataAdapter(SQLstr, myConnection);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "xhbook1");
mytable = ds.Tables[0];
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "错误", MessageBoxButtons.OK);
return false;
}
return true;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (cmb2.Text.Trim() == "" | textBox1.Text.Trim() == "")
{
MessageBox.Show("请先分类选择,材料名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (string.Compare(cmb2.Text.Trim(), "正刊土建材料", true) == 0)
{
string SearchSQLStr;
SearchSQLStr = "SELECT * FROM xhbook WHERE 刊名='正刊土建材料' AND 产品名称 Like '%{0}%'";
string param = textBox1.Text.Trim().Replace("'", "''");
OleDbDataAdapter Searchcmd;
SearchSQLStr = string.Format(SearchSQLStr, param);
Searchcmd = new OleDbDataAdapter(SearchSQLStr, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\Common Files\\xhbook.mdb");
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\CrystalReport1.rpt";
ReportDocument rd = new ReportDocument();
rd.Load(reportPath);
rd.SetDataSource(mytable.DefaultView);
this.crystalReportViewer1.ReportSource = rd;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
我使用的数据库是 access , 想要实现的查询是cmb2下拉4个选项:正刊安装材料,正刊土建材料,正刊园林材料,正刊市政材料, 通过 cmb2 选择和 textBox1中输入的字:如 电线
在水晶报表中显示出.
但我在下面的代码中,debug后,进行查询后,却是全部材料显示,而非是我想要的查询显示,请各位高手指点:
( 另外,我是通过水晶报表向导创建报表的, 通过创建新连接,选择jet 4.0 ole db 然后找到这个xhbook.mdb 文件, 下一步,locale identfier 1033 完成,选定的表中选xhbook1,下一步,要显示的字段全显,后面的分组依据等都空着,最后选表,它就自已产生一个表了,里面就已能预览到数据了.)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using CrystalDecisions.CrystalReports.Engine;
using aeon.Library.DbProc;
namespace CrystalReportsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
OpenDb();
InitializeComponent();
}
OleDbConnection myConnection;
//建立OleDbConnection连接对象
OleDbDataAdapter myAdapter;
//建立OleDbCommand对象
//建立DataSet对象
DataTable mytable;
//定义一个整型变量来存放当前行数
public bool OpenDb()
{
String SQLstr = "";
//定义SQL查询字符串
SQLstr = "select * from xhbook1";
//建立SQL查询语句
try
{
myConnection = new OleDbConnection("Provider="
+ "Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\Common Files\\xhbook.mdb");
myAdapter = new OleDbDataAdapter(SQLstr, myConnection);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "xhbook1");
mytable = ds.Tables[0];
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "错误", MessageBoxButtons.OK);
return false;
}
return true;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (cmb2.Text.Trim() == "" | textBox1.Text.Trim() == "")
{
MessageBox.Show("请先分类选择,材料名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (string.Compare(cmb2.Text.Trim(), "正刊土建材料", true) == 0)
{
string SearchSQLStr;
SearchSQLStr = "SELECT * FROM xhbook WHERE 刊名='正刊土建材料' AND 产品名称 Like '%{0}%'";
string param = textBox1.Text.Trim().Replace("'", "''");
OleDbDataAdapter Searchcmd;
SearchSQLStr = string.Format(SearchSQLStr, param);
Searchcmd = new OleDbDataAdapter(SearchSQLStr, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Program Files\\Common Files\\xhbook.mdb");
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\CrystalReport1.rpt";
ReportDocument rd = new ReportDocument();
rd.Load(reportPath);
rd.SetDataSource(mytable.DefaultView);
this.crystalReportViewer1.ReportSource = rd;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}