顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2011年7月28日 星期四

[C#] 更新、新增、刪除MySQL資料庫資料語法

//引用MySQL套件參考
using MySql.Data.MySqlClient;

//資料庫位址
MySqlConnection myConnection = new MySqlConnection("server=資料庫IP;user id=資料庫使用者帳號; password=帳號的密碼; database=資料庫名稱; CharSet=utf8");

//開啟資料庫
myConnection.Open();

/*更新*/
string update = string.Format("update 資料表 set 欄位1='{0}', 欄位2='{1}'", 值1,值2);
MySqlCommand updateCmd = new MySqlCommand(update, myConnection);
updateCmd.Connection = myConnection;
//執行更新
updateCmd.ExecuteNonQuery();

/*新增*/
string AddNew = string.Format("INSERT INTO 資料表(欄位1, 欄位2) VALUES('{0}','{1}')", 值1, 值2);
MySqlCommand AddNewCmd = new MySqlCommand(AddNew, myConnection);
AddNewCmd.Connection = myConnection;
//執行新增
AddNewCmd.ExecuteNonQuery();

/*刪除*/
string Del = string.Format("DELETE FROM 資料表 WHERE 欄位 = '{0}'", 值);
MySqlCommand DelCmd = new MySqlCommand(Del, myConnection);
DelCmd.Connection = myConnection;
//執行刪除
DelCmd.ExecuteNonQuery();


//關閉資料庫
myConnection.Close();

2011年7月3日 星期日

[C#] 儲存.ini檔

IniFile ini = new IniFile("C:\\檔名.ini");
ini.IniWriteValue("Info", "Key1", "Value");

[C#] 關閉執行中的程式

using System.Diagnostics;

//關閉執行中程式
Process[] list = Process.GetProcessesByName("IExplore");//關閉"IExplore",也可以關閉火狐"firefox" 
foreach (Process p in list)
{
    p.Kill();
}

[C#] 命令提示字元指令

//關機
System.Diagnostics.Process.Start("C:\\WINDOWS\\system32\\shutdown.exe", "-f -s -t 0");

//登出
System.Diagnostics.Process.Start("C:\\WINDOWS\\system32\\shutdown.exe", "-l");

//休眠
System.Diagnostics.Process.Start("C:\\WINDOWS\\system32\\rundll32.exe","powrprof.dll,SetSuspendState");

//重新開機
System.Diagnostics.Process.Start("C:\\WINDOWS\\system32\\shutdown.exe", "-f -r -t 0");

[C#] 倒數計時

private void Form1_Load(object sender, EventArgs e)
{
     timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    //要倒數的時間,也可以倒數5秒,例:DateTime MyEndDate = DateTime.Now.AddSeconds(5);
    DateTime MyEndDate = new DateTime(YY,MM,DD,hh,mm, 0);
    //取得電腦現在時間
    DateTime MyStartDate = DateTime.Now;
    //執行倒數
    TimeSpan MySpan = MyEndDate.Subtract(MyStartDate);
    string diffDay = Convert.ToString(MySpan.Days);
    string diffHour = Convert.ToString(MySpan.Hours);
    string diffMin = Convert.ToString(MySpan.Minutes);
    string diffSec = Convert.ToString(MySpan.Seconds);
    //顯示秒數
    label1.Text = diffDay + "天" + diffHour + "時" + diffMin + "分" + diffSec + "秒";
}

[C#] 列印

列印元件 printDocument
預覽列印 printPreviewDialog

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    //列印圖片
    e.Graphics.DrawImage(pictureBox1.Image, 160, 20, 250, 100);
    //列印文字
    e.Graphics.DrawString(textBox1.Text, textBox1.Font, new SolidBrush(Color.Black), new Point(50, 50));
}
也可以設定列印參數
//設定印A4的一半 直式
printDocument1.DefaultPageSettings.Landscape = true;
printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("pag",this.printDocument1.DefaultPageSettings.PaperSize.Width,this.printDocument1.DefaultPageSettings.PaperSize.Height / 2);

//列印字體設定
textBox1.Font = new System.Drawing.Font("新細明體", 12F, System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, ((byte)(136)));
執行列印
//預覽列印
printPreviewDialog1.ShowDialog();
//直接列印
printDocument1.Print();

[C#] DataGridView新增按鈕欄位

創建按鈕欄
using System.Windows.Forms;

DataGridViewButtonColumn column3 = new DataGridViewButtonColumn();
column3.Name = "欄位名稱";
column3.UseColumnTextForButtonValue = true;
column3.Text = "按鈕名稱";
column3.Width = 30;
DataGridView1.Rows.Add(column3);
新增按鈕跟資料
int Count = DataGridView1.Rows.Count - 2;
DataGridView1.Rows[Count].Cells[1].Value = "值";

[C#] 點選DataGridView欄位取得欄位值

private void DataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
    //取得欄位值
    string value = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString();
}

[C#] 顯示其他程式介面

在From1的程式區宣告From2視窗並呼叫顯示
//宣告From2視窗
From2 From2 = new From2();
From2.FM1 = this;

//呼叫顯示From2視窗
From2.ShowDialog();

在From2程式區宣告
//宣告From1
public Form1 FM1 = null;

如果要把From1的資料傳到From2的物件,From2物件的 Modifiers 屬性(表示物件的可視性層級)要改成 Internal

[C#] 改變解析度

using System.Runtime.InteropServices;

        //改變解析度引用
        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(int hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        [DllImport("user32.dll")]
        public static extern int GetSystemMetrics(int screensize);
        public const int SM_CXSCREEN = 0;
        public const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64;

[C#] 連結MySQL語法-DadataGridView顯示

using MySql.Data.MySqlClient;//安裝MySql.Data.msi套件,再引用參考
 
//資料庫位址,帳號,密碼,資料庫名稱,存取編碼
MySqlConnection myConnection = new MySqlConnection("server=localhost;user id=root; password=sa123; database=mer; CharSet=utf8");
 
MySqlDataReader sqlDataReader;
MySqlCommand sqlCmd;
 
myConnection.Open();
 
DataTable data;
MySqlDataAdapter da;
MySqlCommandBuilder cb;
data = new DataTable();

da = new MySqlDataAdapter(string.Format("SELECT * FROM ABC"), myConnection);
cb = new MySqlCommandBuilder(da);
da.Fill(data);

//放到DataGrid顯示
dataGridView1.DataSource = data;

//關閉資料庫
myConnection.Close();

[C#] 連結MySQL語法

using MySql.Data.MySqlClient;//安裝MySql.Data.msi套件,再引用參考

//資料庫位址,帳號,密碼,資料庫名稱,存取編碼
MySqlConnection myConnection = new MySqlConnection("server=localhost;user id=root; password=sa123; database=mer; CharSet=utf8");

MySqlDataReader sqlDataReader;
MySqlCommand sqlCmd;

myConnection.Open();

string sql = string.Format("SELECT * FROM health_care where Account_number ='{0}' and Password = '{1}'",id_textBox.Text,pass_textBox.Text);
sqlCmd = new MySqlCommand(sql, myConnection);
sqlDataReader = sqlCmd.ExecuteReader();

if (sqlDataReader.Read())
{
   string 變數 = MaxCodeSqlData["資料表名稱"].ToString();
}
sqlDataReader.Close();
myConnection.Close();