Question : How to connect Crystal Report to SQL database in .NET C#

I have a VB6 application which connects a Crystal Report to an SQL2008 database using the code fragment at the end of this message. I am trying to connect to the same SQL2008 DB under .NET C#, however the properties and methods have changed. How would I pass the connection/login info to the Crystal Reports object under .NET?

Thanks

VB6
        For Each dbTable In Report.Database.Tables
            SetTableDBInfo dbTable
        Next dbTablePrivate

Private Sub SetTableDBInfo(ByVal dbTable As CRAXDRT.DatabaseTable)
   
            dbTable.DllName = "crdb_odbc.dll"
            dbTable.ConnectionProperties.DeleteAll
            dbTable.ConnectionProperties.Add "Connection String", "Driver={SQL Server};Server=RICH-PC;Database=SW2011SQL;Trusted_Connection=Yes;"

End Sub

Answer : How to connect Crystal Report to SQL database in .NET C#

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
using CrystalDecisions.CrystalReports.Engine; 
using CrystalDecisions.Shared; 

private CrystalReport1 crReportDocument = new 
CrystalReport1 (); 
private Database crDatabase; 
private Tables crTables; 
private Table crTable; 
private TableLogOnInfo crTableLogOnInfo; 
private ConnectionInfo crConnectionInfo = new 
ConnectionInfo (); 

//Setup the connection information structure 
//to log on to the data source for the report. 
// If using ODBC, this should be the DSN. If using 
// OLEDB, etc, this should be the physical server name 


crConnectionInfo.ServerName = "DSN or 
Server Name"; 

// If you are connecting to Oracle there is no 
// DatabaseName. Use an empty 
// string i.e. crConnectionInfo.DatabaseName = ""; 

crConnectionInfo.DatabaseName = "DatabaseName"; 
crConnectionInfo.UserID = "Your UserID"; 
crConnectionInfo.Password = "Your Password"; 

// This code works for both user tables and stored 
procedures 

//Get the table information from the report 
crDatabase = crReportDocument.Database; 
crTables = crDatabase.Tables; 

//Loop through all tables in the report and apply the 
//connection information for each table. 
for (int i = 0; i < crTables.Count; i++) 
{ 
crTable = crTables ; 
crTableLogOnInfo = crTable.LogOnInfo; 
crTableLogOnInfo.ConnectionInfo = 
crConnectionInfo; 
crTable.ApplyLogOnInfo(crTableLogOnInfo); 

//If your DatabaseName is changing at runtime, specify 
//the table location. For example, when you are 
reporting 
//off of a Northwind database on SQL server 
//you should have the following line of code: 

crTable.Location = crConnectionInfo.DatabaseName + ".dbo." + 
crTable.Location.Substring(crTable.Location.LastIndexOf 
(".") + 1) 
} 

//Set the viewer to the report object to 
//be previewed. 

crystalReportViewer1.ReportSource = 
crReportDocument;
Random Solutions  
 
programming4us programming4us