C# 正则表达式以分隔URL的锚点部分

private string GetAnchorPart()
{
   string anchorExp = "[#]......."; 
   Regex rex = new Regex(anchorExp); 
   Match anchorMatch = rex.Match(Request.RawUrl); 
   return anchorMatch.ToString(); 
}

C# SQL Stored Proc生成表示数据字段名称的C#常量列表。

SELECT 'public const string COLUMN_' + UPPER(col.TABLE_NAME) + '_' + UPPER(col.COLUMN_NAME) + ' = "' + col.COLUMN_NAME + '";' As [Name] 
FROM 		INFORMATION_SCHEMA.COLUMNS col
INNER JOIN 	INFORMATION_SCHEMA.TABLES tab ON tab.TABLE_NAME = col.TABLE_NAME 
WHERE 		tab.TABLE_TYPE = 'BASE TABLE'
AND			tab.TABLE_NAME NOT IN ('dtproperties', 'sysdiagrams')

UNION

SELECT 		'public const string TABLE_' + UPPER(tab.TABLE_NAME) + ' = "' + tab.TABLE_NAME + '";' As sName 
FROM 		INFORMATION_SCHEMA.TABLES tab
WHERE 		tab.TABLE_TYPE = 'BASE TABLE'
AND			tab.TABLE_NAME NOT IN ('dtproperties', 'sysdiagrams')
ORDER BY 	[Name] ASC

C# 显示DataGrid单元格的工具提示

private void dataGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
  try
  {
    DataGrid.HitTestInfo hti = dataGrid1.HitTest(e.X,e.Y);
    if(hti.Type == DataGrid.HitTestType.Cell) 
    {
      toolTip1.SetToolTip(dataGrid1, dataGrid1[hti.Row, hti.Column].ToString()); 
    }
  }
  catch{}
}

C# 使用可信连接将SQLDMO连接到服务器

SQLDMO.SQLServer serverManager = new SQLDMO.SQLServerClass();
serverManager.LoginSecure = true;
serverManager.Connect(serverName, null, null);

C# 使用WMI连接到服务器并检查服务

ServiceController sc = new ServiceController("MSSQLSERVER", serverName); 
string sqlStatus = sc.Status.ToString();

C# 从SQL存储过程中获取返回值

parameters[1] = new SqlParameter("@Return_Value", SqlDbType.Int); 
parameters[1].Value = -1; 
parameters[1].Direction = ParameterDirection.ReturnValue; 

if(Int32.Parse(parameters[1].Value.ToString()) != -1)
{
   //Success
}

C# 对整数的按位运算

//Bitwise Operations

//Turn a bit on
int startVal = 4;                 //100 
int bitVal = 2;                   //010
int newVal = startVal | bitVal;   //110 (6) 
//Alternatively
startVal |= bitVal; 
                  
//Turn a bit off
int startVal = 7;                 //111 
int bitVal = 5;                   //101
int newVal = startVal & bitVal;   //101 (5) 
//Alternatively
startVal &= bitVal; 


//Query bit status
int startVal = 6;                 //110  
int bitVal = 4;                   //100
int newVal = startVal & bitVal;   //100 
if(newVal != 0)
{
  //Bit was on.  
}

int startVal = 6;                 //110  
int bitVal = 1;                   //001
int newVal = startVal & bitVal;   //000 
if(newVal == 0)
{
  //Bit was off.  
}

C# 使用NUnitForms

using System;
using NUnit.Framework;
using NUnit.Extensions.Forms; 

namespace NUnitFormsTests
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[TestFixture]
	public class FormTests : NUnit.Extensions.Forms.NUnitFormsAssertTest
	{
		private string nextExpectedModal = ""; 
		private bool useHidden = true; 
		private TargetForm.Form1 testForm = null; 
		private System.Random rnd = new Random(); 

		public FormTests()
		{
		}
		[Test]
		public void FormCreate()
		{
			LaunchForm1(); 
			Assert.AreEqual("Test Form", testForm.Text); 
		}
		[Test]
		public void ButtonMessageBoxClick()
		{
			LaunchForm1(); 
			ButtonTester bt = new ButtonTester("btnTest", testForm); 
			nextExpectedModal = "Test Message Box"; 
			ExpectModal(nextExpectedModal, "OnModal_DismissMessageBox"); 
			bt.Click(); 
			nextExpectedModal = string.Empty; 
		}
		[Test]
		public void ButtonChangeTextClick()
		{
			LaunchForm1(); 
			ButtonTester bt = new ButtonTester("btnChangeText", testForm); 
			bt.Click(); 
			NUnit.Extensions.Forms.TextBoxTester tbt = new TextBoxTester("txtTestField", testForm); 
			Assert.AreEqual("New Value",tbt.Text); 
		}
		[Test]
		public void LabelTest()
		{
			LaunchForm1(); 
			NUnit.Extensions.Forms.LabelTester lt = new LabelTester("lblTestField", testForm); 
			Assert.AreEqual("Test Field:", lt.Text); 
		}
		public void LaunchForm1()
		{		
			testForm = new TargetForm.Form1(); 
			testForm.Show();
		}
        private void OnModal_DismissMessageBox()
		{
			System.Threading.Thread.Sleep(rnd.Next(3000)); 
			NUnit.Extensions.Forms.MessageBoxTester mbt = new MessageBoxTester(nextExpectedModal); 
			mbt.ClickOk(); 
		}
		public override bool UseHidden
		{
			get
			{
				return useHidden; 
			}
		}
	}
}

C# NUnit表单代码以解除模态表单(不是消息框)。

[Test]
public void SendTransaction()
{
  FormTester ft = LaunchForm(); 
  FillFormDetails(ft); 
  ButtonTester bt = new ButtonTester("cmdApply", ft.Properties); 
  ExpectModal("UpdateInfo","CloseUpdateWindow"); 			
  bt.Click(); 
}

private void CloseUpdateWindow()
{
  FormTester ft = new FormTester("UpdateInfo"); 
  Assert.IsTrue(ft.Text == "Updated successfully"); 
  ft.Close(); 
}

C# 数据库检索

try
        {
            // Create and open a connection object
            conn = new SqlConnection(configurationConnString);
            conn.Open();

            // Set Stored Proc 
            SqlCommand cmd = new SqlCommand("spGetConfigSettingsByApplication", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // Set Parameters
            cmd.Parameters.Add(new SqlParameter("@applicationName", SqlDbType.NVarChar, 50));
            cmd.Parameters["@applicationName"].Value = "FTU";

            // execute the command
            rdr = cmd.ExecuteReader();

            // iterate through results, printing each to console
            while (rdr.Read())
            {
                configSettings.Add(rdr["AppKey"], rdr["AppValue"]);
            }
        }
        finally
        {
            if (conn != null) conn.Close();
            if (rdr != null) rdr.Close();
        }