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, there is no major problem. 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, the user name and password already exist in the connection string, but I still ask the user name and password. After I enter the correct one, the user name and password are incorrect. I searched in the csdn forum and found a solution. However, 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
OleDbDataAdapter 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"); // 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; // set the report displayed on the 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, add a connection, and select Microsoft. jet.4.0 ole db provider as the new connection. 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.

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.