2010年4月24日 星期六

如何備份資料庫 使用windows form 的方式

學會:
這個主題會讓你學會如何備份跟還原

前言
為什麼要學這個技術?
備份資料庫你直接開資料庫管理工具保證都會有這個備份功能,但是你想想看如果你是套裝軟體開發商,你的客戶搞不好連資料庫管理工具都不知道在那裡 ,那他要如何完成備份的工作呢?比如說進銷存軟體你要備份你的資料庫這個資料庫可能是你裝軟體的時候它幫你裝上的,所以就沒有管理工具囉(如mysql)這時候就必需透過套裝軟體的gui介面幫你完成備份&還原

我已經幫你介紹了一些背景相信你可以明白的知道這個技術會用在那裡囉以下我們要正式開始囉

開始:
這次花一點時間研究一下,如何備份資料庫的資料表,這件事如果透果DBMS來做的話可能是相當簡單的但是有時候透過一個GUI介面的BUTON也可以完成,但真實的理由是非專業人員該如何實現備份當然是按下備份鈕備份頂多選個備份時間,跟備份資料跟備份地點,當然備份完還要還原......................好了 ,我已經說明了大概的功能了接下來還有一些規畫。

備份就是要知道
資料表結構
1.有資料表名稱
2.資料表欄位名稱
3.資料表欄位定義
4.資料表欄位條件限制
4.資料表可否允許null
5.資料表唯一
6.資料列

我的意思是你必需從資料表得到這些後你才能用sql的語法在製造相同的table達到備份的目地
概念是不是很簡單呢? 那我該如何得到上述的資訊了 我從來都不知道這些定義存在那邊!!!所以在資料庫中資料表是放在那呢?

1.查資料庫是否存在?
select * from sysdatabases where sid> 0x01
可以找出自訂的資料庫系統的我們就跳過吧
2.查資料庫裡的所有資料表?
use TestReport; --選擇你要查的資料庫
select TABLE_NAME from INFORMATION_SCHEMA.TABLES;
go
3. 查資料表的欄位定義
use TestReport;
select * from INFORMATION_SCHEMA.COLUMNS ;
go

那我們來熱身一下吧

首先我先畫一下windows form的草稿gui 大概是長這樣

實際是長這樣

code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
label6.Text = "";
String ServerAddress = "";
String useId = "";
String pwd = "";
ServerAddress = textBox1.Text;
useId = textBox2.Text;
pwd = textBox3.Text;

try
{ //測試連接資料庫
SqlConnection conn = new SqlConnection("Data Source=" + ServerAddress + ";Initial Catalog=" + comboBox1.SelectedText + ";Persist Security Info=True;User ID=" + useId + ";Password=" + pwd);
SqlCommand cmd = new SqlCommand("select * from company ", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
//dr.Read()如果有資料的話會回傳true 沒有資料會回傳false
if (dr.Read())
{
label6.Text = "連線成功";
textBox1.ReadOnly = true;
}
else
{
label6.Text = "連線失敗";


}

conn.Close();
cmd.Cancel();
dr.Close();
}
catch (Exception ex)
{//登入異常時我們用 一個對話框來裝這些異常訊息
textBox1.ReadOnly = false;
//傳ex 異常訊息給對話框
Form2 f2=new Form2(ex);
f2.ShowDialog();//記的顯示它

}



}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: 這行程式碼會將資料載入 'masterDataSet.news' 資料表。您可以視需要進行移動或移除。
//this.newsTableAdapter.Fill(this.masterDataSet.news);
textBox2.Enabled = false;
textBox3.Enabled = false;

}


//輸入伺服器之後可以馬上查詢伺服器上所有的私有資料庫 以條件sid> 0x01篩選
private void textBox1_Leave(object sender, EventArgs e)
{
String QQ = "";
QQ = textBox1.Text;
try
{
SqlConnection conn2 = new SqlConnection("Data Source=" + QQ + ";Initial Catalog=master;Integrated Security=True");
//SqlCommand cmd2 = new SqlCommand("select * from sysdatabases where sid> 0x01", conn2);
conn2.Open();

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from sysdatabases where sid> 0x01", conn2);
da.Fill(ds, "sysdatabases");
// this.dataGridView1.DataSource = ds.Tables["sysdatabases"];
conn2.Close();
textBox2.Enabled = true;
textBox3.Enabled = true;
//combobox "name"是sysdatabases的屬性
comboBox1.DataSource = ds.Tables["sysdatabases"];
comboBox1.DisplayMember = "name";
}
catch(Exception ex)
{
textBox2.Enabled = false;
textBox3.Enabled = false;
Form2 f2 = new Form2(ex);
f2.ShowDialog();

}
}


}
}
/code

前題:
實現備份有很多方式
1.實體檔案複製之後還原在載入
2.parse 資料庫然後編寫標準sql語法
3.DBMS管理備份功能

所以要實現相同目標有三條路可以走parse是比較細工的做法也可以學到很多東西,三種在本篇議題都會討論實做

想法流程:
1. 用焦點event 當輸入伺服器名稱後馬上捉取該伺服器上的資料庫用combox裝起來
供使用者選擇,登入方式是windows 驗證
2.輸入使用者密碼後 可以測試連接,以後就資以server 驗證登入操作系統
3.備份原理就是parse 整個資料內容使之成為一個sql 語法的檔案,內容包含資料庫,資料表結構,
資料列,之後還會parse像條件限制,預存,檢視表,觸發

功能1:備份資料庫
點選備份資料庫後會備份所選的資料庫,備份的資料庫要存在那?
先找出資料庫內有多少資料表,使用一個list<>來裝資料名稱 後要找出資料欄位跟欄位型態就可以完成簡單的資料表建構資訊

2010年4月23日 星期五

日期的差距計算

很多時候我們會計算日期的差距
比如說年資, 到期日多久

我們使用一個含數 datadiff(參數1,參數2,參數3)
參數1:表示要以year=yy ,Month=MM,day=DD 來計算
是以參數3 - 參數2

範例:
Declare @day1 date
Declare @day2 date
declare @day3 int
set @day1='2009-04-20'
set @day2='2009-04-27'
select @day3=DATEDIFF(DD,@day1,@day2)
select @day3
----------------------------------------------
7

2010年4月22日 星期四

sql 語法 bteween 與 in 的用法 篩選資料

資料庫 :AdventureWorks sql2008 範例資料庫
一年前我去面試得時候 ,那時我還不知道between用法的時候 , 在sql裡面查詢某一個資料行的區間範圍時我們用
select * from HumanResources.Employee where BirthDate >='1972-05-15' And BirthDate <='1977-05-15' 所謂的區間值其實本身已經隱含排序的味道,比如說日期, 真實數字 所以才有區間的感覺 在between 的語法下: where 資料行1 between A and B PS:A必須要小於B

select * from HumanResources.Employee where BirthDate Between '1972-05-15' and '1977-05-15'
所以這樣是不是簡單多了呢

2010年4月8日 星期四

電話禮儀sop

1. 拿起話統
2. 說"XXXe公司 您好 敝姓 xx 很高興為您服務 請問有什麼事嗎?"
3. 根據問題按保留
4. 轉給適合的人接聽














以下是廣告
營養分析軟體

sql 語法 union

union
簡稱聯集

將兩個查詢結果 合併在一起

為什麼要用union
a:目前有兩家公司 如果你要知道兩家公司的員工資料的話該怎麼用呢?
這時後你就可以用union合併兩個公司的員工資料
以下是code
select companyA.companyName, companyA.companyId from companyA
UNION
select companyB.companyName, companyB.companyId from companyB

不過如果把兩個公司的員工資料表分開的話 這正規化也做太爛了吧

2010年3月14日 星期日

cystalreport 按下匯出會登入錯誤得問題?

在page_load()裡面呼叫report()
ui
只拉元件就好了
用SqlDataSource1捉使用者selectquery的dataset 給crystalreport
在到.rpt設定
protected void report()
{
SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings["TestReportConnectionString"].ConnectionString;
DataTable dt = new DataTable();
String openwin = "";
String sql = TextBox1.Text.Trim();
if (String.IsNullOrEmpty(sql))
{
SqlDataSource1.SelectCommand = "select * from company";
dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();

}
else
{
SqlDataSource1.SelectCommand = "select * from company where companyId=" + "'" + TextBox1.Text + "'";
dt = ((DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
}
Response.Cache.SetCacheability(HttpCacheability.NoCache); //clear cache
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("CrystalReport.rpt"));
rd.SetDataSource(dt);
CrystalReportViewer1.ReportSource = rd;
GridView1.DataSourceID = SqlDataSource1.ID;
}

2010年3月4日 星期四

下拉式選單+連結資料庫 c# 版

SqlConnection Conn1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestReportConnectionString"].ConnectionString);
SqlDataAdapter da1 = new SqlDataAdapter("select version from product where productName='萬能廚師系列'", Conn1);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "product");
DropDownList3.AutoPostBack = true;
DropDownList3.DataTextField = "version";
DropDownList3.DataValueField = "version";
DropDownList3.DataSource = ds1.Tables["product"].DefaultView;
DropDownList3.DataBind();
Conn1.Close();