Những mẹo cần biết khi lập trình .NET

4 411 3
Những mẹo cần biết khi lập trình .NET

Đang tải... (xem toàn văn)

Thông tin tài liệu

Những mẹo cần biết khi lập trình .NET Chúng tôi xin đưa ra các phương pháp giải quyết các vấn đề mà các nhà phát triển .NET thường gặp. Hy vọng chúng sẽ giúp ích cho các bạn. 1. Làm thế nào giới hạn một chương trình chỉ chạy một lần Trong form chính đổi thành như sau: static void Main() { Process ThisProcess = Process.GetCurrentProcess(); Process [] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName); if (AllProcesses.Length > 1) { MessageBox.Show(ThisProcess.ProcessName + '' is already running'', ThisProcess.ProcessName, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Application.Run(new MainForm()); } } 2. Di chuyển con trỏ đến dòng và cột xác định (RichTextBox) Dùng phương thức GoToLineAndColumn public void GoToLineAndColumn(int Line, int Column) { Cursor.Current = Cursors.WaitCursor; int Offset = 0; int i = 0; foreach (String L in Lines) { if (i < Line - 1) { Offset += L.Length + 1; } else { break; } i++; } Select(Offset + Column - 1, 0); Cursor.Current = Cursors.Arrow; } 3.Xác định cột hiện thời. (RichTextBox ) public int GetColumn() { int LineNumber = GetLineFromCharIndex(SelectionStart); int LineOffset = 0; int i = 0; foreach (String Line in Lines) { if (i < LineNumber) { LineOffset += Line.Length + 1; } else { break; } i++; } return SelectionStart - LineOffset + 1; } 3. Chạy JScript.NET trong ứng dụng C# Tạo một JScript.NET ''package'' bao gồm một phương thức toàn cục (public) package JScript { class Eval { public function DoEval(expr : String) : String { return eval(expr); } } } try { Result = (int) Application.UserAppDataRegistry.GetValue(''Resolution''); } catch(Exception) { } Và thêm một reference đến chương trình C# của bạn và sử dụng JScript.Eval E = new JScript.Eval(); String Expression = ExpressionTextBox.Text; try { ResultTextBox.Text = E.DoEval(Expression); } catch(Microsoft.JScript.JScriptException jse) 4.Lưu thông số cấu hình vào Registry Đầu tiên vào AssemblyInfo.cs và bỏ tất cả các thông số từ AssemblyVersion: [assembly: AssemblyVersion(''1.0.0.0'')] Mặc dù mỗi lần bạn build ứng dụng khoá register sẽ thay đổi. Lưu giá trị bằng cách sau Application.UserAppDataRegistry.SetValue(''Value'', Value); Nạp lại các thông số : try { Value = (int) Application.UserAppDataRegistry.GetValue(''Value''); } catch(Exception) { } SQL Server: UDF IsValidNumber Hàm trong SQL Server rất hữu dụng cho các bạn. Hàm kiểm tra một chuỗi có phải là một số không. Hàm này chấp nhận một chuỗi và kiểm tra nếu chuỗi có bao gồm các kí tự không phải 0-9 hoặc dấu thập phân (decimal ). Hàm trả về 0 nếu đúng là số; 1 nếu không phải dạng số. CREATE FUNCTION udfIsValidNumber ( @thestring varchar(50), @numdecimals int = 0 ) RETURNS int AS BEGIN DECLARE @not int, @ascii int, @pos int, @dec int SET @pos = 1 SET @not = 0 SET @dec = 0 --first check to see if it is a valid number IF @thestring IS NULL SET @not =1 IF len(@thestring) = 0 SET @not = 1 WHILE @pos<= len(@thestring) BEGIN SELECT @ascii = ascii(substring(@thestring, @pos, 1)) IF (@ascii > 57) SET @not = 1 IF (@ascii < 46) SET @not = 1 IF (@ascii = 47) SET @not = 1 IF (@ascii = 46) SET @dec = @dec + 1 SET @pos = @pos + 1 END IF @dec > 1 SET @not = 1 IF @not > 0 RETURN @not -- invalid number --valid number now check number of decimals SELECT @dec = charindex('.',@thestring) SET @pos = len(@thestring) - @dec -- find the number of characters right of decimal IF @pos > @numdecimals SET @not = 1 RETURN @not END ADO/SQL Server nText inserts/updates Rất nhiều lập trình viên hỏi làm thế nào để thêm (insert) dữ liệu vào trong một trường nText vào SQL Server với ADO. Phần lớn các câu SQL thường dùng string chuẩn và nó sẽ gặp vấn đề khi cập nhật các ký tự đặc biệt. Sau đây chúng tôi sẽ giúp các bạn tránh được các lỗi thường gặp đó. Dim lRecs Dim moADOCon Dim moADOCom Set moADOCon = Server.CreateObject(''ADODB.Connection'') Set moADOCom = Server.CreateObject(''ADODB.Command'') moADOCon.Open ''your connection string'' With moADOCom .ActiveConnection = moADOCon .CommandText = ''spPost'' .CommandType = adCmdStoredProc .Parameters.Append .CreateParameter(''@RETURN_VALUE'', adInteger, adParamReturnValue,0) .Parameters.Append .CreateParameter(''@ReplyToID'', adInteger, adParamInput, , msPostID) .Parameters.Append .CreateParameter(''@fk_author_id'', adInteger, adParamInput, , clng(Session(''intMemberID''))) .Parameters.Append .CreateParameter(''@fk_interest_id'', adInteger, adParamInput, , msInterestID) .Parameters.Append .CreateParameter(''@subject'', adVarWChar, adParamInput, 50, msSubject) .Parameters.Append .CreateParameter(''@bodytext'', adVarWChar, adParamInput, 1073741823, msBodyText) .Execute lRecs, , adExecuteNoRecords End With moADOCon.Close Set moADOCom = nothing Set moADOCon = nothing . Những mẹo cần biết khi lập trình .NET Chúng tôi xin đưa ra các phương pháp giải quyết các vấn đề mà các nhà phát triển .NET thường gặp return SelectionStart - LineOffset + 1; } 3. Chạy JScript .NET trong ứng dụng C# Tạo một JScript .NET ''package'' bao gồm một phương thức

Ngày đăng: 26/10/2013, 01:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan