㈠ C# 寫一個winform登錄與注冊的應用程序
登錄就是判斷你輸入的值和資料庫中的值是否相同,而注冊其實就是往資料庫中插入用戶名和密碼,比如注冊代碼如下,導入命名空間using System.Data.SqlClient;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=資料庫名;uid=用戶名;pwd=密碼;";
string strcmd = "insert into 用戶表 values('" + TextBox1.Text + "','" + TextBox2.Text + "')";
SqlCommand mycommand = new SqlCommand(strcmd, conn);
try
{
conn.Open();
mycommand.ExecuteNonQuery();
MessageBox.Show(" 注冊成功 ");
}
catch
{
MessageBox.Show("注冊發生錯誤");}
finally
{
conn.Close();
}
㈡ 求VS2010 C#FORM編寫強制關機代碼 只要關機的
using System.Runtime.InteropServices; // 提供DllImport等特性,是P/Invoke的關鍵
// 這個結構體將會傳遞給API。使用StructLayout(...特性,確保其中的成員是按順序排列的,C#編譯器不會對其進行調整。
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
// 以下使用DllImport特性導入了所需的Windows API。
// 導入的方法必須是static extern的,並且沒有方法體。調用這些方法就相當於調用Windows API。
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flg, int rea);
// 以下定義了在調用WinAPI時需要的常數。這些常數通常可以從Platform SDK的包含文件(頭文件)中找到
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
// 通過調用WinAPI實現關機,主要代碼再最後一行ExitWindowsEx,這調用了同名的WinAPI,正好是關機用的。
private static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(flg, 0);
}
private void button1_Click(object sender, EventArgs e)
{
DoExitWin(EWX_SHUTDOWN);
}