如果一直要建立測試資料庫,除了可以將 Create Table 的語法保留下來之外,對於資料,若也要保留的話,可以使用 SQL Server 內建的"產生指令碼"功能。
操作步驟如下
可以選擇特定或全部物件來建立指令碼
可以選擇將物件產生的指令碼分別存成單一檔案或分開存
點擊上圖的"進階"後,選擇下圖中的"結構描述和資料"
完成後,只要執行產生的 .sql,就可以將資料直接寫進資料表,而不用手動建資料或匯入資料了。
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("myID");
dt.Columns.Add("myValue");
dt.Rows.Add(new object[] { "1", "Bermuda 百慕達" });
dt.Rows.Add(new object[] { "2", "Bosnia and Herzegovina 波希尼亞及赫塞哥維那" });
cbxExamination.DataSource = dt;
cbxExamination.DisplayMember = "myValue";
cbxExamination.ValueMember = "myID";
// 在資料繫結後加入以下語法
SetDropDownWidth(comboCountry);
}
/// <summary>
/// 設定下拉選單下拉後的長度為最長內容的長度
/// </summary>
/// <param name="myCombo"></param>
/// <returns></returns>
private void SetDropDownWidth(ComboBox myCombo)
{
int maxSize = 0;
System.Drawing.Graphics g = CreateGraphics();
for (int i = 0; i < myCombo.Items.Count; i++)
{
myCombo.SelectedIndex = i;
SizeF size = g.MeasureString(myCombo.Text, myCombo.Font);
if (maxSize < (int)size.Width)
{
maxSize = (int)size.Width;
}
}
myCombo.DropDownWidth = myCombo.Width;
if (myCombo.DropDownWidth < maxSize)
{
myCombo.DropDownWidth = maxSize;
}
}
結果如下