نوشته شده توسط
atf_1379
سلام جناب آقای صدیقی !
جسارتاً این آموزش (آموزش اجرای کد های C# .Net در اکسس) چه کاربردی در اکسس دارد و یا به عبارتی چه امتیازی برای اکسس ایجاد می کند ؟
لود کردن اکسس با C# .NET
Access.Application oAccess = null;
// Start a new instance of Access for Automation:
oAccess = new Access.ApplicationClass();
// Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(
"c:\\mydb.mdb", //filepath
true //Exclusive
);
Print or Preview an Access Report
To preview or to print an Access report, you call the OpenReport method of the DoCmd object. When you call OpenReport, one of the arguments that you pass determines whether the report is previewed on the screen, or whether it is sent to the printer:
// Preview a report named Sales:
oAccess.DoCmd.OpenReport(
"Sales", //ReportName
Access.AcView.acViewPreview, //View
System.Reflection.Missing.Value, //FilterName
System.Reflection.Missing.Value //WhereCondition
);
// Print a report named Sales:
oAccess.DoCmd.OpenReport(
"Sales", //ReportName
Access.AcView.acViewNormal, //View
System.Reflection.Missing.Value, //FilterName
System.Reflection.Missing.Value //WhereCondition
);
Notice that the View argument determines whether the report is displayed in Access or whether it is sent to the printer. The WhereCondition argument can limit the report's recordset, if you use a valid SQL WHERE clause (without the word WHERE.) Notice that you can use System.Reflection.Missing.Value to skip any object parameters that are optional.
If you are previewing a report, be sure to set the Visible property of the Application object so that Access is visible on the screen. In this way, the user can view the report in the Access window.
There is another way to print a report or other objects in the database. Use the PrintOut method of the DoCmdobject. In this example, you select a report named Employees in the Database window, and then you call PrintOutto print the selected object. The PrintOut method allows you to provide arguments that correspond to the Print dialog box in Access:
// Select the Employees report in the database window:
oAccess.DoCmd.SelectObject(
Access.AcObjectType.acReport, //ObjectType
"Employees", //ObjectName
true //InDatabaseWindow
);
// Print 2 copies of the selected object:
oAccess.DoCmd.PrintOut(
Access.AcPrintRange.acPrintAll, //PrintRange
System.Reflection.Missing.Value, //PageFrom
System.Reflection.Missing.Value, //PageTo
Access.AcPrintQuality.acHigh, //PrintQuality
2, //Copies
false //CollateCopies
);
Or, in some cases, you may want to use both the OpenReport and the PrintOut methods to print a report. Suppose you want to print multiple copies of the Employees report but only for a specific employee. This example first uses OpenReport to open the Employees report in preview mode, using the WhereCondition argument to limit the records to a specific employee. Then, PrintOut is used to print multiple copies of the active object:
// Open the report in preview mode using a WhereCondition:
oAccess.DoCmd.OpenReport(
"Employees", //ReportName
Access.AcView.acViewPreview, //View
System.Reflection.Missing.Value, //FilterName
"[EmployeeID]=1" //WhereCondition
);
// Print 2 copies of the active object:
oAccess.DoCmd.PrintOut(
Access.AcPrintRange.acPrintAll, //PrintRange
System.Reflection.Missing.Value, //PageFrom
System.Reflection.Missing.Value, //PageTo
Access.AcPrintQuality.acHigh, //PrintQuality
2, //Copies
false //CollateCopies
);
// Close the report preview window:
oAccess.DoCmd.Close(
Access.AcObjectType.acReport, //ObjectType
"Employees", //ObjectName
Access.AcCloseSave.acSaveNo //Save
);
Show and Edit an Access Form
Visual C# .NET has very powerful form capabilities. However, there may be times when you want the user to view a form that was previously developed in Access. Or, you may have a form in your Access database that provides criteria for a query or report, and you must open that form before you can preview or print the report. To open and show an Access form, you call the OpenForm method of the DoCmd object:
// Show a form named Employees:
oAccess.DoCmd.OpenForm(
"Employees", //FormName
Access.AcFormView.acNormal, //View
System.Reflection.Missing.Value, //FilterName
System.Reflection.Missing.Value, //WhereCondition
Access.AcFormOpenDataMode.acFormPropertySettings, //DataMode
Access.AcWindowMode.acWindowNormal, //WindowMode
System.Reflection.Missing.Value //OpenArgs
);
باز کردن فایل دارای پسورد
Access Security Dialog Boxes
When you automate Access, you may be prompted to enter a user name or a password, or both, when you try to open a database. If the user enters the wrong information, an error will occur in your code. There may be times when you want to avoid these dialog boxes and instead to programmatically provide the user name and password so that your Automation code runs uninterrupted.
There are two types of security in Microsoft Access: password-protected databases and user-level security through a workgroup file (System.mdw). If you are trying to open a database that is password protected, you will receive a dialog box prompting for the database password. User-level security is different from a password-protected database. When user-level security is activated, Access displays a logon dialog box prompting for both a user name and password before the user can open any database in Access. For more information about Access security and the workgroup information file, click the article number below to view the article in the Microsoft Knowledge Base:
305542 Understanding the role of workgroup information files in Access security
Avoiding Database Password Dialog Boxes
If you are opening a database that has been protected with a password, you can avoid the dialog box by providing the password to the OpenCurrentDatabase method
// Open a password-protected database in shared mode:
// Note: The bstrPassword argument is case-sensitive
oAccess.OpenCurrentDatabase(
"c:\\mydb.mdb", //filepath
false, //Exclusive
"MyPassword" //bstrPassword
);
Here is an example, where oAccess has been previously set to an instance of Access that does not have a database open. This code provides the password to the database to avoid a dialog box:
string sDBPassword = "MyPassword"; //database password
DAO._DBEngine oDBEngine = oAccess.DBEngine;
DAO.Database oDB = oDBEngine.OpenDatabase("c:\\mydb.mdb",
false, false, ";PWD=" + sDBPassword);
oAccess.OpenCurrentDatabase("c:\\mydb.mdb", false);
oDB.Close();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(oDB);
oDB = null;
System.Runtime.InteropServices.Marshal.ReleaseComO bject(oDBEngine);
oDBEngine = null;
و منبع
https://support.microsoft.com/en-us/...using-visual-c