Bo is a novice still in college, if there is anything wrong, please be sure to help me point out, thank you!
To complete a crystal report that dynamically alters the data, you can store and output data by creating a broker for the data.
By this transit station, we can achieve the purpose of getting the Crystal Report with Dynamic Data by inputting the data dynamically and then exporting the data to the Crystal Report.
Only in vs2008, many of its own applications can also be used to achieve the role of the broker, such as arrays, datasets and so on.
Bloggers have just been learning SQL so they tried to do Crystal Reports with VS connection database.
1. First, the blogger creates a database named Xxs in SQL, and then creates a table named Xxs_1 in the database XXS, with a column named Ques in it.
2. Then go back to vs2008, create a Windows Forms application (bloggers are in order to input data to the database to use the form, netizens have other ways to be casual.) )
Write code for the program:
1SqlConnection conn2 =NewSqlConnection ();2Conn2. ConnectionString ="server=.; Database=xxs;uid=sa;pwd=123";3 conn2. Open ();4SqlCommand CMD2 =NewSqlCommand ();5Cmd2. Connection =conn2;6Cmd2.commandtype =CommandType.Text;7 stringSQL2 ="INSERT INTO Xxs_1 (ques) VALUES (@ques)";8sqlparameter[] Parameters2 =Newsqlparameter[]9 { Ten NewSqlParameter ("@ques", F) One A }; -Cmd2.commandtext =sql2; - CMD2. Parameters.addrange (parameters2); the CMD2. ExecuteNonQuery (); - conn2. Close (); -Cmd2. Dispose ();
Which conn2. ConnectionString is the path to the SQL database, the string SQL2 is the SQL statement that will be executed in SQL, and the string f is the data to add for column ques.
In order to add enough data to the column, the blogger used the Loop statement, which is not discussed here, after all, just the above statement to loop, for F repeated assignment, and then write columns.
3. After completing the data entry, we can set up the Crystal Report.
As mentioned above, bloggers have used form forms in order to enter data into the database XXS.
The following two button components are applied to the blogger in the form.
The "Generate Question bank" is the 2nd step in the application of the code to enter data for the database, and "preview report" is what we have to say now to establish the Crystal Report.
Before writing the code, the blogger did some preparatory work first.
(1) Connect to the database.
Click the Data button on the menu bar/Click the Add new data source in the Open menu/Select the data source from the database in the pop-up Configuration Wizard, click Next/Click New Connection/write the server name in the Add Connection window that pops up (typically. (yes, it's a "dot"), choose to use SQL SEVER authentication, enter the user name and password, then you can write the database name in "Select or enter a database name", then click OK, click Next/Go to "Select Database Object", select the tables and columns you need to display in the Crystal Report. Click Done.
(2) Create a Crystal report.
Right-click the first project under Solution Explorer/select Add in the popup menu bar, and tap new item. /Select the Crysal report in reporting in the Add New item that pops up (below). /
/Then the Crystal Report library form will pop up, select Use the Reporting Wizard and Standard, and select OK. /Open the project data in the Standard Report Creation Wizard, open the ADO dataset to select the table in the database you want, and add it to the right. Click Next/Select the desired column, add it to the right, and click Next. /grouping and record selection I didn't use it, so I clicked the next step, I chose the table and clicked done.
At this point, we have completed the creation of the Crystal Report.
4. Add data to the Crystal Report.
In order to make the Preview report function perfect, the blogger adds a form application Form2 (below) to the project.
Only one Crystalreportviewer control is added to the Form2.
Then double-click the Form2 form to write the code:
1CrystalReport1 cry =NewCrystalReport1 ();2SqlConnection conn =NewSqlConnection ();3Conn. ConnectionString ="server=.; Database=xxs;uid=sa;pwd=123";4 Conn. Open ();5SqlCommand cmd =NewSqlCommand ();6Cmd. Connection =Conn;7Cmd.commandtype =CommandType.Text;8 stringsql ="Select ques from Xxs_1";9Cmd.commandtext =SQL;TenDataSet ds =NewDataSet ();//Create a new staging database OneSqlDataAdapter ADT =NewSqlDataAdapter (); AAdt. SelectCommand =cmd; -Adt. Fill (DS,"inf");//import data into DS -DataTable dt = ds. tables["inf"];//simply write DS directly for the convenience of referencing later. tables["INF") is also possible.
Use the above code to remove the data from the column ques in table Xxs_1 in the database XXS.
Finally, write the code again:
1 cry. Setdatasource (DT);
2 this. Crystalreportviewer1.reportsource = Cry;
The data can be written to the Crystal Report.
5. Of course, in order to be able to repeatedly input data into SQL, and then export the data to the Crystal report, we need to clear the data in the SQL database after the Crystal Report is established, so at the end of all the above is the last write code:
1SqlConnection conn1 =NewSqlConnection ();2Conn1. ConnectionString ="server=.; Database=xxs;uid=sa;pwd=123";3 conn1. Open ();4SqlCommand cmd1 =NewSqlCommand ();5Cmd1. Connection =conn1;6Cmd1.commandtype =CommandType.Text;7 stringSQL1 ="TRUNCATE TABLE Xxs_1";8Cmd1.commandtext =SQL1;9 cmd1. ExecuteNonQuery ();TenConn1. Close ();
This completes a Dynamic Data Crystal Report using SQL database as the data broker.
Here is the effect demo:
When I run the program press "Generate Question bank" is, the program will add data to the database, for example, I want to generate 40 operations, you can get such a table
When I Press "preview report", I get a crystal report like this:
C # using VS2008 to connect SQL database to make dynamic Crystal Report