2010年9月29日 星期三

SQL PK重註

由於PK用太久
會導致資訊越來越分散
也會影響SQL速度
所以MS有分享了一個方式可以讓人檢查
也可以重註PK方式

檢查方式

SELECT a.index_id, name, avg_fragmentation_in_percent, *
FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID('MG_Strategic_Plan_MeasureGroup_default_partition'),
NULL, NULL, NULL) AS a
JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id

在MS提供文件說 avg_fragmentation_in_percent 不能超過30
超過表示太分散囉
要重註

重註語法

ALTER INDEX [PK_TableName]
ON dbo.[TableName]
REBUILD WITH (ONLINE = ON);


我是這樣弄的
再重新檢查一次
發現降下來或是變成0了
成功 ^_^

參考網站

2010年9月27日 星期一

TSQL INPUTBUFFER

暫記一下
列出目前執行的狀態


select * from sys.sysprocesses
dbcc INPUTBUFFER (17) --17是編號

2010年9月22日 星期三

C# UserControl Event 觸發

因為當然大家的UserControl的功能不可能全寫死
例如每次都會被用到才寫進去
不常用就留觸發出來就好了
所以就要寫觸發與新增功能來

這樣說會不會很模糊 XDDDD
例如Button 原本UserControl內的功能是A
但拉到form後又想新增Button按下去有B功能
就要這樣寫


public event EventHandler MyEvent; //這是外層宣告
protected void button1_Click(object sender, EventArgs e) //記得將private改成protected
{
MyEvent(sender, e); //觸發的新增功能
}

C# UserControl

原來UserControl要另外寫專案來包 >_<"

開一個WindowsFormsControlLib的專案
然後就可以建立UserControl了
然後執行後
產生的DLL檔
拉到元件列裡面
就可以拿出來使用囉
而要做對外的觸角 (給值與取值的地方)
就寫

public string TextValue
{
get { return this.textBox1.Text; }
set { this.textBox1.Text=value; }
}


一直以為可以直接做 = =+
原來是我誤會了 囧

2010年9月17日 星期五

C# Windows AP 被綁畫面!!!! DoEvents()

因為很多時候
執行AP的某些動作 其實AP畫面不會馬上SHOW
會等全部做完以後才顯示出來!
這時候就要用到


Application.DoEvents();


藥到病除!! XDDD

2010年9月14日 星期二

Linq To Entity Framework (Store Proceduce篇)

再看完前兩篇文章
就會想說
那 Entity Framework勒
應該也可以呼叫Store Proceduce吧
對囉
當然可以
只是
麻煩了一個步驟 XD

首先一樣先引入Store Proceduce進來
然後勒
要在他的地方點右鍵 選 Add Function Import
這樣他才能被呼叫出來
然後設定輸出要啥類型的 之類的東西
就可以正常呼叫囉

entity1.GetUserLevel("", "");

Linq To Sql 呼叫 Store Procedure

那天就在研究Store Procedure的東西
發現Linq To Sql 可以直接拉進來用欸
太優了 >/////<

方法一樣
開啟Linq To Sql後
將Store Procedure拉進來
一放
就可以呼叫囉 A_A

var data2 = from a in linq.SelectItemLevel(934) //SelectItemLevel是procedure 934是值
select a;

C# Linq To Entity Framework

最近因為Linq To Sql 要倒了
所以在找 Linq To Entity Framework的方法
結果 哈哈哈 差不多東西阿 換湯不換藥
使用起來還是很簡單的說 XD

只是一開始是做Linq To Sql 變成做Entity Data Model
呼叫方式變成

LinqDataContext linqDB = new LinqDataContext();
Entities1 entity1 = new Entities1();

接下來就都一樣囉 >_^

2010年9月9日 星期四

C# Textbox 浮水印 WaterMark

再應用程式內的浮水印
利用參考網站大大分享的CODE所弄的

要進去改Designer的元件

//this.textBox1 = new System.Windows.Forms.TextBox(); //這個改成下面那個
this.textBox1 = new wmgCMS.WaterMarkTextBox(); //改成這個
//---------元件設定部份
this.textBox1 .WaterMarkColor = System.Drawing.Color.Gray;
this.textBox1 .WaterMarkText = "浮水印要顯示的字";
//---------最下面
//private System.Windows.Forms.TextBox textBox1; //這個改成下面那個
private wmgCMS.WaterMarkTextBox textBox1; //改成這個



參考網站

C# 選擇資料夾,選擇檔案

選擇資料夾

FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
textBox1.Text = path.SelectedPath;


選擇檔案

OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
textBox1.Text = file.SafeFileName;

2010年9月8日 星期三

C# 控制遠端電腦

就是用WMI來做壞事 阿 不是啦 做控制
就是利用WMI可以做遠端電腦的控制
如 關機..重開..一些資訊擷取等

關機 重開

ConnectionOptions options = new ConnectionOptions();
options.Username = "Username"; // 使用者名稱
options.Password = "Password"; // 使用者密碼
options.Authentication = AuthenticationLevel.Default; // 認證模式設定 (採用預設)
options.Impersonation = ImpersonationLevel.Impersonate; // 設定 COM 模擬等級
options.EnablePrivileges = true; // 參考 **(特一)
try
{

ManagementScope MS_Conn = new ManagementScope("\\\\" + "IP" + "\\root\\cimv2", options);
MS_Conn.Connect();


ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");


ManagementObjectSearcher mos1 = new ManagementObjectSearcher(MS_Conn, oq);

ManagementObjectCollection moc1 = mos1.Get();


foreach (ManagementObject mo in moc1)
{

mo.InvokeMethod("Reboot", null);
}
}
catch (Exception err1)
{
textBox1.Text= err1.Message;
} 



參考網站1 參考網站2

2010年9月7日 星期二

C# 檔案,資料夾 Move Copy Del Insert

檔案複製

string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
System.IO.File.Copy(sourceFile, destFile, true);


檔案搬移

System.IO.File.Move(sourceFile, destinationFile);


檔案刪除

if(System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt"))
{
// Use a try block to catch IOExceptions, to
// handle the case of the file already being
// opened by another process.
try
{
System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}


建立資料夾

if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}



複製資料夾

if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);

// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}


搬移資料夾

System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");


刪除資料夾

if(System.IO.Directory.Exists(@"C:\Users\Public\DeleteTest"))
{
try
{
System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest", true);
}

catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}


參考網站

2010年9月5日 星期日

C# textbox 驗證

可以用keyPress做驗證
這樣在KEY入就檢查各個單字是否可輸入

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int ascii = Convert.ToInt16(e.KeyChar);
if ((ascii >= 97 && ascii <= 122) || (ascii == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}

或是用Validating來做驗證

private void textBox1_Validating(object sender, CancelEventArgs e)
{
try
{
int numberEntered = int.Parse(textBox1.Text);
if (numberEntered < 1 || numberEntered > 10)
{
e.Cancel = true;
MessageBox.Show("You have to enter a number between 1 and 10");
}
}
catch (FormatException)
{
e.Cancel = true;
MessageBox.Show("You need to enter an integer");
}
}

第一個是一輸入的時候就檢查
第二個是整個輸入結束執行時檢查

2010年9月2日 星期四

C# DataTable TO DataGridView Image 問題

最近試了DataGridView要Show圖
可是怎都怎錯
後來發現...
原來我DataTable沒設屬性
所以才會不能顯示

DataTable1.Columns.Add("ImageValue", typeof(Image));
Image OnlineImage = Image.FromFile("online.png");
DataTable1.Rows[i]["ImageValue"] = OnlineImage;
dataGridView1.DataSource = DataTable1;