. Net C/S (winform) development skills

Source: Internet
Author: User

1. Data Binding. Datareader reads data, loads the data to the able using the able. Load (idatareader) method, and displays the output using the datagridview dview. Do not bind the dview directly to datareader. The data source can be obtained from the datagridview again when the data is exported to excel.
Do not edit and add data in the datagridview because the data type check is not strict (or strict check of the type requires a greater cost ).

2. export data to excel.CodeAs follows:

/** // <Summary>
///
/// ** Export datatable data to excel **
///

///
/// </Summary>
Excel. Application APP = new excel. Application ();
App. Visible = false;
Excel. Workbook WB = app. workbooks. Add (true );
Excel. worksheet Ws = (Excel. worksheet) WB. worksheets. Add (type. Missing,

Type. Missing );

Datatable dt = (datatable) dgvclientinfo. datasource;

For (INT I = 0; I <DT. Columns. Count; I ++)
...{
WS. cells [1, I + 1] = DT. Columns [I]. columnname;
}

For (Int J = 0; j <DT. Rows. Count; j ++)
...{
For (int K = 0; k <DT. Columns. Count; k ++)
...{
WS. cells [J + 2, K + 1] = DT. Rows [J] [k];
}
}
App. Visible = true;
Note: The subscript of cells [,] in Excel starts from, instead.

3. prevent repeated opening of subwindows. Make sure that a subwindow is opened only once. Code:

foreach (Form frm in this. mdichil.pdf)
... {
If (FRM is workerlist)
... {
frm. windowstate = formwindowstate. normal;
frm. activate ();
return;
}< BR >}

Workerlist WL = new workerlist ();
WL. mdiparent = this;
WL. Show ();
4. Use enumeration Enum to differentiate the similar information.

Hard coding is prone to confusion. For example, if the common telephone/frequently-used website address in this system is named-content-remarks, they can be saved in the same tableProgramTo differentiate information types, you can define the following enumeration:

Public Enum telweb
...{
Telephone, website
}
5. Add other winform controls to toolstrip.

For example, add datetimepicker to toolstrip.

Datetimepicker dt1 = new datetimepicker ();
Dt1.width = 110;
Toolstripcontrolhost host1 = new toolstripcontrolhost (dt1 );
Host1.alignment = toolstripitemalignment. Right;
Toolstrip1.items. insert (10, host1 );
6. Use Microsoft's printable rich text box controls to print formatted text.
Compared with the RichTextBox provided by. netfx, only the printing function is enhanced. This control allows you to set text fonts, colors, Alignment Methods, and paste images to print seemingly professional documents.

7. Save the Rich Text Format to the database.

Saved in binary format. The saved code is:

Memorystream MS = new memorystream ();
Rtbcontent. SaveFile (MS, richtextboxstreamtype. richtext );
Byte [] bt = Ms. toarray (); // save BT to the database
The read code is:

Byte [] bt = (byte []) sqlhelper. executescalar (sqlstr, null );
Memorystream MS = new memorystream (BT, false );
Rtbcontent. LoadFile (MS, richtextboxstreamtype. richtext );
The corresponding data type in sqlserver is: Image

8. Administrator permission control.

In the Administrator information table, the permission field stores a string. If you have this permission, the value of 1 is saved in the corresponding position of the string. If you do not have this permission, the value of 0 is saved. Determine the permission when the Administrator logs on to enable or disable corresponding menu items.

9. Information classification.

For example, customers are classified into software customers, website customers, and partner customers. If the data volume is not too large, you do not need to create a Category Table separately. The customer type is added. You can use ComboBox to search for existing customer types in the database during form loading and fill them in ComboBox. In this way, you can select an existing customer type or add a new customer type.

10. Install the database.

You do not need to integrate it into the installation package. You can write a separate winform program to collect information about the Connected Server (such as the server address, database name, user name, and password ), execute the script generated by the database to create data tables, views, stored procedures, indexes, and so on.
Code for reading and saving the database script file:

Public static string readdbscript (string filename)
...{
Streamreader sr = new streamreader (filename );
Return Sr. readtoend ();
}
Note: the "go" must be removed from the script generated by the database; otherwise, an error is returned.

11. security control.

Because it is a network version system, security should be fully considered. The database connection string is encrypted and stored in the configuration file. To prevent software xcopy from being sent to other machines, hardware (such as hard disk, Nic, and CPU) of the machine can be obtained) the part of the serial number plus your own private key as the encryption key (eight-bit ASCII) for the connection string, to ensure the privacy of the key and the uniqueness of each machine.

(1) obtain the first eight digits of the machine's cpu id as the key:

Public static string getprocessid ()
...{
Try
...{
String STR = string. empty;
Managementclass mccpu = new managementclass ("win32_processor ");
Managementobjectcollection moccpu = mccpu. getinstances ();

Foreach (managementobject m in moccpu)
...{
STR = m ["processorid"]. tostring (). Trim (). substring (0, 8 );
}
Return STR;
}
Catch (exception ex)
...{
Return "Zhenxing"; // If the accesskey fails, use the default accesskey.
}
}
(2) Encryption Algorithm :

Public static string encode (string daTa)
...{
Byte [] akey = system. Text. asciiencoding. ASCII. getbytes (getprocessid ());
Byte [] AIV = system. Text. asciiencoding. ASCII. getbytes (getprocessid ());

descryptoserviceprovider CP = new descryptoserviceprovider ();
memorystream MS = new memorystream ();
cryptostream cs = new cryptostream (MS, CP. createencryptor (akey, AIV), cryptostreammode. write);
streamwriter Sw = new streamwriter (CS);

Sw. Write (DATa );
Sw. Flush ();
CS. flushfinalblock ();
Sw. Flush ();
Return convert. tobase64string (Ms. getbuffer (), 0, (INT) ms. Length );
}
(3) decryption algorithm:

Public static string decode (string daTa)
...{
Byte [] akey = system. Text. asciiencoding. ASCII. getbytes (getprocessid ());
Byte [] AIV = system. Text. asciiencoding. ASCII. getbytes (getprocessid ());

byte [] ENC;
try
... {
ENC = convert. frombase64string (DA Ta);
}< br> catch
... {
return NULL;
}

descryptoserviceprovider CP = new descryptoserviceprovider ();
memorystream MS = new memorystream (ENC);
cryptostream cs = new cryptostream (MS, CP. createdecryptor (akey, AIV), cryptostreammode. read);
streamreader sr = new streamreader (CS);
return Sr. readtoend ();
}< br> (4) Save the database connection string to the configuration file:

Public static void savetoconfig (string connstr)
...{
Xmldocument Doc = new xmldocument ();
String fn = "zxjay.exe. config ";

Doc. Load (FN );

Xmlnodelist nodes = Doc. getelementsbytagname ("add ");

For (INT I = 0; I <nodes. Count; I ++)
...{
Xmlattribute ATT = nodes [I]. attributes ["key"];
If (Att. value = "sqlconectionstring ")
...{
ATT = nodes [I]. attributes ["value"];
Att. value = connstr;
Break;
}
}
Doc. Save (FN );
}
C # Open the selected folder and point to the selected file

Open the selected folder

System. Diagnostics. process. Start ("assumer.exe", filepath );

Open the selected folder and point to the selected file

System. Diagnostics. process. Start ("assumer.exe", "/select," + filepath );

C # How to get the file extension
Path. getextension (filename );

Reprinted on http://blog.csdn.net/zxjay/archive/2007/08/31/1766329.aspx

/// <Summary>
/// Determine whether it is a number
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> </returns>
Private bool isnumeric (string Str)
{
If (STR = NULL | Str. Length = 0)
Return false;
System. Text. asciiencoding ASCII = new system. Text. asciiencoding ();
Byte [] bytestr = ASCII. getbytes (STR );
Foreach (byte C in bytestr)
{
If (C <48 | C> 57)
{
Return false;
}
}
Return true;
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/codes/archive/2006/05/22/750012.aspx

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.