C # connect to the Access database, involving Crystal Reports

Source: Internet
Author: User

The software recently used by a person involves Crystal Reports. Initially, we decided to use C # to connect to the access database. Some problems encountered during database connection. If it is a separate database connection, no
There are too many problems. However, when we use Crystal Reports, we feel that the problem is coming. Sometimes we don't know why. For example, when I connect to sqlserver
I have a user name and password, but I still ask my user name and password. When I enter the correct one, I still tell me that the user name and password are incorrect. Search in the csdn forum and find a solution.
Solution, but I am very lazy and don't want to try it. Besides, I feel that this is not a problem with database connection, and the crystal report is strange.

Now we have decided to use C # and access. I tried this evening and there was no problem with sqlserver. Think about it. In fact, although access may not match sqlserver in many ways, at least the user's computer does not need to install sqlserver, which is a good place.

Next, let's talk about how C # connects to the Access database, and then about C #, Access database, and crystal report.

First of all, C # is connected to the Access database, which is very simple. The code is ready directly. Here, only the absolute path is involved, but the relative path has some problems.

Method 1: Use
Oledbdatareader to read data.

// Connect to the Access Database
// 1. absolute path connection
Oledbconnection dbconn = new oledbconnection (@ "provider = Microsoft. Jet. oledb.4.0; Data Source = G:/crystal report/tryarea/a. mdb ");
Dbconn. open ();

// Operation data
Oledbcommand cmd = dbconn. createcommand ();
Cmd. commandtext = "select * from book ";
Cmd. commandtype = commandtype. text;
Oledbdatareader reader = cmd. executereader ();

// Method for reading data
// Read a row before displaying the data of this row.
Reader. Read ();
Textbox1.text = reader. getstring (1 );

// While (reader. Read ())
//{
// Textbox1.text = reader. getstring (1 );
//}
// Close Reader
Reader. Close ();

// Close the database connection
Dbconn. Close ();

Method 2: Use dataset to read data, which is convenient and flexible

// Use the absolute path to connect to the database and then use dataset to read the data
//
// 1. absolute path connection
Oledbconnection
Dbconn = new oledbconnection (@ "provider = Microsoft. Jet. oledb.4.0; Data Source = G:/crystal report/tryarea/a. mdb ");
Dbconn. open ();

// Operate data, with Dataset
Oledbdataadapte
R da = new oledbdataadapter ("select * from book", dbconn );

Dataset
DS = new dataset ();
Da. Fill (DS );

Textbox1.text = Ds. Tables [0]. Rows [0] ["bookname"]. tostring ();

// Close the database connection
Dbconn. Close ();

The following describes the Crystal Report, C #, access

1). The Code on the CS page is not difficult, as shown below:

// USE THE CRYSTAL REPORT
//
// 1. absolute path connection
OleDbConnection dbconn = new OleDbConnection (@ "Provider = Microsoft. Jet. OLEDB.4.0; Data source = G:/crystal report/tryArea/a. mdb ");
Dbconn. Open ();
// Operate data, with dataset
OleDbDataAdapter da = new OleDbDataAdapter ("select * from book", dbconn );

Dataset1 ds = new Dataset1 ();//
Dataset is a previously defined Dataset.
Da. Fill (ds, "book ");//
Be sure to specify the table to which the data is stored. Otherwise, the data will not be read into the defined data set. why?

// Da. Fill (ds); // if data is written in this way, no data is available at all. Although I don't know why this is the case, this is true for dataset1.
TextBox1.Text = ds. Tables [0]. Rows [0] [0]. ToString ();

CrystalReport1
ORtp = null; // The report. rpt name defined by the user.
ORtp = new CrystalReport1
();

ORtp. SetDataSource (ds); // on the code page, associate the report with the dataset. There is another setting in the report.

CrystalReportViewer1.
ReportSource = oRtp; // It is equivalent to setting the report displayed on a report control.

// Close the database connection
Dbconn. Close ();

2) Add a dataset, right-click Solution Explorer, add a new item, and add a dataset. On the. xsd page, right-click the data connection in server Resource Manager
Key, add a connection, and select the new connection as Microsoft. Jet.4.0 ole db Provider. The database will be selected, and so on.

3) on the add crystal report page, right-click Solution Explorer, add a new project, and add a crystal report. On the. rpt page, you can set the specific report format. The key is to associate the report with a specific dataset. Since a dataset has been created before, we need to associate this crystal report with this dataset.

4) In Solution Explorer, right-click to add a new reference to CrystalDecisions. CrystalReports. Engine.

5) Add the following on the cs page:

Using CrystalDecisions. CrystalReports. Engine;

// Required for access connection
Using System. Data. OleDb;

Well, that's enough. However, the design of the Crystal Report page is not that easy, and I have not learned yet.

In addition, it is better to use the relative path to connect to the database.

 

_____________________________________________________________________

Full text

URL: http://blog.csdn.net/mengaim_cn/archive/2006/06/06/775186.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.