自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#開(kāi)發(fā)和使用中的33個(gè)技巧

開(kāi)發(fā) 后端
本文總結(jié)了C#在開(kāi)發(fā)和使用中的23個(gè)技巧,希望對(duì)大家有所幫助。

筆者在實(shí)踐過(guò)程中,總結(jié)了C#開(kāi)發(fā)和使用的若干技巧,如下:
1.怎樣定制VC#DataGrid列標(biāo)題?

  1.   DataGridTableStyle dgts = new DataGridTableStyle();  
  2.  
  3.   dgts.MappingName = "myTable"//myTable為要載入數(shù)據(jù)的DataTable  
  4.  
  5.   DataGridTextBoxColumn dgcs = new DataGridTextBoxColumn();  
  6.  
  7.   dgcs.MappingName = "title_id";  
  8.  
  9.   dgcs.HeaderText = "標(biāo)題ID";  
  10.  
  11.   dgts.GridColumnStyles.Add(dgcs);  
  12.  
  13.   ……  
  14.  
  15.   dataGrid1.TableStyles.Add(dgts);  

2.檢索某個(gè)字段為空的所有記錄的條件語(yǔ)句怎么寫(xiě)?

   ...where col_name is null

3.如何在c# Winform應(yīng)用中接收回車(chē)鍵輸入?

設(shè)一下form的AcceptButton.

4.比如Oracle中的NUMBER(15),在Sql Server中應(yīng)是什么?

NUMBER(15):用numeric,精度15試試。

5.sql server的應(yīng)用like語(yǔ)句的存儲(chǔ)過(guò)程怎樣寫(xiě)?

  1. select * from mytable where haoma like ‘%’ + @hao + ‘%’ 

6.vc# winform中如何讓textBox接受回車(chē)鍵消息(假?zèng)]沒(méi)有按鈕的情況下)?

  1. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)  
  2.  
  3.       {  
  4.  
  5.    if(e.KeyChar != (char)13)  
  6.  
  7.    return;  
  8.  
  9.    else 
  10.  
  11.    //do something;  
  12.  
  13.    }  

7.為什么(Int32)cmd.ExecuteScalar()賦值給Int32變量時(shí)提示轉(zhuǎn)換無(wú)效?

Int32.Parse(cmd.ExecuteScalar().ToString());

8.DataSource為子表的DataGrid里怎樣增加一個(gè)列以顯示母表中的某個(gè)字段?

 在子表里手動(dòng)添加一個(gè)列。 

  1.   DataColumn dc = new DataColumn("newCol", Type.GetType("System.String"));  
  2.  
  3.    dc.Expression = "Parent.parentColumnName";  
  4.  
  5.    dt.Columns.Add(dc); //dt為子表  

9.怎樣使DataGrid顯示DataTable中某列的數(shù)據(jù)時(shí)只顯示某一部分?

select ..., SUBSTR(string, start_index, end_index) as ***, *** from ***

10.如何讓winform的combobox只能選不能輸入?

DropDownStyle 屬性確定用戶能否在文本部分中輸入新值以及列表部分是否總顯示。

值:

DropDown --- 文本部分可編輯。用戶必須單擊箭頭按鈕來(lái)顯示列表部分。

DropDownList --- 用戶不能直接編輯文本部分。用戶必須單擊箭頭按鈕來(lái)顯示列表部分。

11.怎樣使winform的DataGrid里顯示的日期只顯示年月日部分,去掉時(shí)間?

 sql語(yǔ)句里加上to_date(日期字段,'yyyy-mm-dd')

12.怎樣把數(shù)據(jù)庫(kù)表的二個(gè)列合并成一個(gè)列Fill進(jìn)DataSet里?

dcChehao = new DataColumn("newColumnName", typeof(string));

dcChehao.Expression = "columnName1+columnName2";

dt.Columns.Add(dcChehao);

Oracle:

select col1  col2 from table

sql server:

select col1+col2 from table

13.如何從合并后的字段里提取出括號(hào)內(nèi)的文字作為DataGrid或其它綁定控件的顯示內(nèi)容?即把合并后的字段內(nèi)容里的左括號(hào)(和右括號(hào))之間的文字提取出來(lái)。

Select COL1,COL2, case

when COL3 like ‘%(%’ THEN substr(COL3, INSTR(COL3, ‘(’ )+1, INSTR(COL3,‘)’)-INSTR(COL3,‘(’)-1)

end as COL3

from MY_TABLE

 14.當(dāng)用鼠標(biāo)滾輪瀏覽DataGrid數(shù)據(jù)超過(guò)一定范圍DataGrid會(huì)失去焦點(diǎn)。怎樣解決?

  1.   this.dataGrid1.MouseWheel+=new MouseEventHandler(dataGrid1_MouseWheel);  
  2.  
  3.  private void dataGrid1_MouseWheel(object sender, MouseEventArgs e)  
  4.  
  5.   {  
  6.  
  7.   this.dataGrid1.Select();  
  8.  
  9.   }  

15.怎樣把鍵盤(pán)輸入的‘+’符號(hào)變成‘A’?

   textBox的KeyPress事件中

  1.   if(e.KeyChar == '+')  
  2.  
  3.   {  
  4.  
  5.   SendKeys.Send("A");  
  6.  
  7.   e.Handled = true;  
  8.  
  9.   }  

16.怎樣使Winform啟動(dòng)時(shí)直接***化?

this.WindowState = FormWindowState.Maximized;

17.c#怎樣獲取當(dāng)前日期及時(shí)間,在sql語(yǔ)句里又是什么?

c#: DateTime.Now

sql server: GetDate()

18.怎樣訪問(wèn)winform DataGrid的某一行某一列,或每一行每一列?

dataGrid[row,col]

19.怎樣為DataTable進(jìn)行匯總,比如DataTable的某列值‘延吉'的列為多少?

dt.Select("城市='延吉'").Length;

20.DataGrid數(shù)據(jù)導(dǎo)出到Excel后0212等會(huì)變成212。怎樣使它導(dǎo)出后繼續(xù)顯示為0212?

range.NumberFormat = "0000";

21.① 怎樣把DataGrid的數(shù)據(jù)導(dǎo)出到Excel以供打印?

② 之前已經(jīng)為DataGrid設(shè)置了TableStyle,即自定義了列標(biāo)題和要顯示的列,如果想以自定義的視圖導(dǎo)出數(shù)據(jù)該怎么辦?

③ 把數(shù)據(jù)導(dǎo)出到Excel后,怎樣為它設(shè)置邊框?。?/P>

④ 怎樣使從DataGrid導(dǎo)出到Excel的某個(gè)列居中對(duì)齊?

⑤ 數(shù)據(jù)從DataGrid導(dǎo)出到Excel后,怎樣使標(biāo)題行在打印時(shí)出現(xiàn)在每一頁(yè)?

⑥ DataGrid數(shù)據(jù)導(dǎo)出到Excel后打印時(shí)每一頁(yè)顯示’當(dāng)前頁(yè)/共幾頁(yè)’,怎樣實(shí)現(xiàn)?

 ①

   

  1. private void button1_Click(object sender, System.EventArgs e)  
  2.  
  3.    {  
  4.  
  5.    int row_index, col_index;  
  6.  
  7.    row_index = 1;  
  8.  
  9.    col_index = 1;  
  10.  
  11.    Excel.ApplicationClass excel = new Excel.ApplicationClass();  
  12.  
  13.    excel.Workbooks.Add(true);  
  14.  
  15.    DataTable dt = ds.Tables["table"];  
  16.  
  17.    foreach(DataColumn dcHeader in dt.Columns)  
  18.  
  19.    excel.Cells[row_index, col_index++] = dcHeader.ColumnName;  
  20.  
  21.    foreach(DataRow dr in dt.Rows)  
  22.  
  23.    {  
  24.  
  25.    col_index = 0;  
  26.  
  27.    foreach(DataColumn dc in dt.Columns)  
  28.  
  29.    {  
  30.  
  31.    excel.Cells[row_index+1, col_index+1] = dr[dc];  
  32.  
  33.    col_index++;  
  34.  
  35.    }  
  36.  
  37.    row_index++;  
  38.  
  39.    }  
  40.  
  41.    excel.Visible = true;  
  42.  
  43.    }  
  44.  
  45.     private void Form1_Load(object sender, System.EventArgs e)  
  46.  
  47.    {  
  48.  
  49.    SqlConnection conn = new SqlConnection("server=tao; uid=sa; pwd=; database=pubs");  
  50.  
  51.    conn.Open();  
  52.  
  53.    SqlDataAdapter da = new SqlDataAdapter("select * from authors", conn);  
  54.  
  55.    ds = new DataSet();  
  56.  
  57.  da.Fill(ds, "table");  
  58.  
  59.    dataGrid1.DataSource = ds;  
  60.  
  61.    dataGrid1.DataMember = "table";  
  62.  
  63.    }  
  64.  

②dataGrid1.TableStyles[0].GridColumnStyles[index].HeaderText; //index可以從0~dataGrid1.TableStyles[0].GridColumnStyles.Count遍歷。

 ③ Excel.Range range;

   range=worksheet.get_Range(worksheet.Cells[1,1],xSt.Cells[ds.Tables[0].Rows.Count+1,ds.Tables[0].Columns.Count]);

   range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);

     range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;

   range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;

   range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;

  range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;

   range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;

④ range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter

⑤ worksheet.PageSetup.PrintTitleRows = "$1:$1";

⑥ worksheet.PageSetup.CenterFooter = "第&P頁(yè) / 共&N頁(yè)";

22.當(dāng)把DataGrid的Cell內(nèi)容賦值到Excel的過(guò)程中想在DataGrid的CaptionText上顯示進(jìn)度,但不顯示。WHY?

   ...

   dataGrid1.CaptionText = "正在導(dǎo)出:" + (row + 1) + "/" + row_cnt;

   System.Windows.Forms.Application.DoEvents();

   ...

處理當(dāng)前在消息隊(duì)列中的所有Windows消息。

當(dāng)運(yùn)行Windows窗體時(shí),它將創(chuàng)建新窗體,然后該窗體等待處理事件。該窗體在每次處理事件時(shí),均將處理與該事件關(guān)聯(lián)的所有代碼。所有其他事件在隊(duì)列中等待。在代碼處理事件時(shí),應(yīng)用程序并不響應(yīng)。如果在代碼中調(diào)用DoEvents,則應(yīng)用程序可以處理其他事件。

如果從代碼中移除DoEvents,那么在按鈕的單機(jī)事件處理程序執(zhí)行結(jié)束以前,窗體不會(huì)重新繪制。通常在循環(huán)中使用該方法來(lái)處理消息。

23.怎樣從Flash調(diào)用外部程序,如一個(gè)C#編譯后生成的.exe?

   fscommand("exec", "應(yīng)用程序.exe");

① 必須把flash發(fā)布為.exe

② 必須在flash生成的.exe文件所在目錄建一個(gè)名為fscommand的子目錄,并把要調(diào)用的可執(zhí)行程序拷貝到那里。

24.有沒(méi)有辦法用代碼控制DataGrid的上下、左右的滾動(dòng)?

  1.   dataGrid1.Select();  
  2.  
  3.   SendKeys.Send("{PGUP}");  
  4.  
  5.   SendKeys.Send("{PGDN}");  
  6.  
  7.   SendKeys.Send("{^{LEFT}"); // Ctrl+左方向鍵  
  8.  
  9.   SendKeys.Send("{^{RIGHT}"); // Ctrl+右方向鍵  

25.怎樣使兩個(gè)DataGrid綁定兩個(gè)主從關(guān)系的表?

  1.   DataGrid1.DataSource = ds;  
  2.  
  3.   DataGrid1.DataMember = "母表";  
  4.  
  5.   ...  
  6.  
  7.   DataGrid2.DataSouce = ds;  
  8.  
  9.   DataGrid2.DataMember = "母表.關(guān)系名";  

26.assembly的版本號(hào)怎樣才能自動(dòng)生成?特別是在Console下沒(méi)有通過(guò)VStudio環(huán)境編寫(xiě)程序時(shí)。

關(guān)鍵是AssemblyInfo.cs里的[assembly: AssemblyVersion("1.0.*")],命令行編譯時(shí)包含AssemblyInfo.cs

27.怎樣建立一個(gè)Shared Assembly?

用sn.exe生成一個(gè)Strong Name:keyfile.sn,放在源程序目錄下

在項(xiàng)目的AssemblyInfo.cs里[assembly: AssemblyKeyFile("..\\..\\keyfile.sn")]

生成dll后,用gacutil /i myDll.dll放進(jìn)Global Assembly Cach.

28.在Oracle里如何取得某字段***個(gè)字母為大寫(xiě)英文A~Z之間的記錄?

select * from table where ascii(substr(字段,1,1)) between ascii('A') and ascii('Z')

29.怎樣取得當(dāng)前Assembly的版本號(hào)?

  1. Process current = Process.GetCurrentProcess();  
  2.  
  3. FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(current.MainModule.FileName);  
  4.  
  5. Console.WriteLine(myFileVersionInfo.FileVersion);  

 30.怎樣制作一個(gè)簡(jiǎn)單的winform安裝程序?

 ① 建一個(gè)WinForm應(yīng)用程序,最最簡(jiǎn)單的那種。運(yùn)行。

 ② 添加新項(xiàng)目->安裝和部署項(xiàng)目,‘模板’選擇‘安裝向?qū)А?/P>

③ 連續(xù)二個(gè)‘下一步’,在‘選擇包括的項(xiàng)目輸出’步驟打勾‘主輸出來(lái)自’,連續(xù)兩個(gè)‘下一步’,‘完成’。

④ 生成。

⑤ 到項(xiàng)目目錄下找到Setup.exe(還有一個(gè).msi和.ini文件),執(zhí)行。

31.怎樣通過(guò)winform安裝程序在Sql Server數(shù)據(jù)庫(kù)上建表?

① [項(xiàng)目]—[添加新項(xiàng)]

類別:代碼;模板:安裝程序類。

名稱:MyInstaller.cs

② 在SQL Server建立一個(gè)表,再[所有任務(wù)]—[生成SQL腳本]。

生成類似如下腳本(注意:把所有GO語(yǔ)句去掉):

  1.  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MyTable]'and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
  2.  
  3. drop table [dbo].[MyTable]  
  4.  
  5. CREATE TABLE [dbo].[MyTable] (  
  6.  
  7. [ID] [intNOT NULL ,  
  8.  
  9. [NAME] [nchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL 
  10.  
  11. ON [PRIMARY]  
  12.  
  13. ALTER TABLE [dbo].[MyTable] WITH NOCHECK ADD 
  14.  
  15. CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED  
  16.  
  17. (  
  18.  
  19. [ID]  
  20.  
  21. ON [PRIMARY]  

③ [項(xiàng)目]—[添加現(xiàn)有項(xiàng)]。mytable.sql—[生成操作]-[嵌入的資源]。

④ 將MyInstaller.cs切換到代碼視圖,添加下列代碼:

先增加:

  1.   using System.Reflection;  
  2.  
  3.    using System.IO;  

然后:

   

  1. private string GetSql(string Name)  
  2.  
  3.    {  
  4.  
  5.    try 
  6.  
  7.    {  
  8.  
  9.     Assembly Asm = Assembly.GetExecutingAssembly();  
  10.  
  11.      Stream strm = Asm.GetManifestResourceStream(Asm.GetN  
  12.  
  13.  ame().Name + "." + Name);  
  14.  
  15.      StreamReader reader = new StreamReader(strm);  
  16.  
  17.      return reader.ReadToEnd();  
  18.  
  19.    }  
  20.  
  21.    catch (Exception ex)  
  22.  
  23.    {  
  24.  
  25.      Console.Write("In GetSql:"+ex.Message);  
  26.  
  27.      throw ex;  
  28.  
  29.    }  
  30.  
  31.    }  
  32.  
  33.    private void ExecuteSql(string DataBaseName,string Sql)  
  34.  
  35.    {  
  36.  
  37.    System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();  
  38.  
  39.    sqlConn.ConnectionString = "server=myserver; uid=sa; password=; database=master";  
  40.  
  41.    System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConn);  
  42.  
  43.    Command.Connection.Open();  
  44.  
  45.   Command.Connection.ChangeDatabase(DataBaseName);  
  46.  
  47.    try 
  48.  
  49.    {  
  50.  
  51.    Command.ExecuteNonQuery();  
  52.  
  53.    }  
  54.  
  55.    finally 
  56.  
  57.    {  
  58.  
  59.    Command.Connection.Close();  
  60.  
  61.    }  
  62.  
  63.    }  
  64.  
  65.    protected void AddDBTable(string strDBName)  
  66.  
  67.    {  
  68.  
  69.    try 
  70.  
  71.    {  
  72.  
  73.    ExecuteSql("master","create DATABASE "+ strDBName);  
  74.  
  75.    ExecuteSql(strDBName,GetSql("mytable.sql"));  
  76.  
  77.    }  
  78.  
  79.    catch(Exception ex)  
  80.  
  81.    {  
  82.  
  83.    Console.Write("In exception handler :"+ex.Message);  
  84.  
  85.    }  
  86.  
  87.    }  
  88.  
  89.    public override void Install(System.Collections.IDictionary stateSaver)  
  90.  
  91.    {  
  92.  
  93.    base.Install(stateSaver);  
  94.  
  95.    AddDBTable("MyDB"); //建一個(gè)名為MyDB的DataBase  
  96.  
  97.    }  

⑤ [添加新項(xiàng)目]—[項(xiàng)目類型:安裝和部署項(xiàng)目]—[模板:安裝項(xiàng)目]—[名稱:MySetup]。

⑥ [應(yīng)用程序文件夾]—[添加]—[項(xiàng)目輸出]—[主輸出]。

⑦ 解決方案資源管理器—右鍵—[安裝項(xiàng)目(MySetup)]—[視圖]—[自定義操作]。[安裝]—[添加自定義操作]—[雙擊:應(yīng)用程序文件夾]的[主輸出來(lái)自***(活動(dòng))]。

32.怎樣用TreeView顯示父子關(guān)系的數(shù)據(jù)庫(kù)表(winform)?

三個(gè)表a1,a2,a3, a1為a2看母表,a2為a3的母表。

a1: id, name

a2: id, parent_id, name

a3: id, parent_id, name

用三個(gè)DataAdapter把三個(gè)表各自Fill進(jìn)DataSet的三個(gè)表。

用DataRelation設(shè)置好三個(gè)表之間的關(guān)系。

  1. foreach(DataRow drA1 in ds.Tables["a1"].Rows)  
  2.  
  3. {  
  4.  
  5. tn1 = new TreeNode(drA1["name"].ToString());  
  6.  
  7. treeView1.Nodes.Add(tn1);  
  8.  
  9. foreach(DataRow drA2 in drA1.GetChildRows("a1a2"))  
  10.  
  11. {  
  12.  
  13.   tn2 = new TreeNode(drA2["name"].ToString());  
  14.  
  15.    tn1.Nodes.Add(tn2);  
  16.  
  17.    foreach(DataRow drA3 in drA2.GetChildRows("a2a3"))  
  18.  
  19.    {  
  20.  
  21.     tn3 = new TreeNode(drA3["name"].ToString());  
  22.  
  23.    tn2.Nodes.Add(tn3);  
  24.  
  25.    }  
  26.  
  27.     }  
  28.  
  29.    }  

33.怎樣從一個(gè)form傳遞數(shù)據(jù)到另一個(gè)form?

假設(shè)Form2的數(shù)據(jù)要傳到Form1的TextBox。

在Form2:

 

  1.   // Define delegate  
  2.  
  3.    
  4.  
  5.   public delegate void SendData(object sender);  
  6.  
  7.    
  8.  
  9.   // Create instance  
  10.  
  11.    
  12.  
  13.   public SendData sendData;  
  14.  

在Form2的按鈕單擊事件或其它事件代碼中:

  1.   if(sendData != null)  
  2.  
  3.   {  
  4.  
  5.    sendData(txtBoxAtForm2);  
  6.  
  7.   }  
  8.  
  9.   this.Close(); //關(guān)閉Form2  

 在Form1的彈出Form2的代碼中: 

  1.   Form2 form2 = new Form2();  
  2.  
  3.    form2.sendData = new Form2.SendData(MyFunction);  
  4.  
  5.    form2.ShowDialog();  
  1.   private void MyFunction(object sender)  
  2.  
  3.   {  
  4.  
  5.   textBox1.Text = ((TextBox)sender).Text;  
  6.  
  7.   }  

C#開(kāi)發(fā)和使用的技巧就給大家介紹到這里,希望會(huì)對(duì)大家有用。

【編輯推薦】

  1. C#調(diào)用VC DLL接口函數(shù)參數(shù)類型轉(zhuǎn)換方法介紹
  2. 解決C#中用Oracle執(zhí)行存儲(chǔ)過(guò)程返回DataSet的問(wèn)題
  3. C#線程同步技術(shù)之Monitor
  4. C#線程同步與死鎖
  5. C#線程:線程池和文件下載服務(wù)器
責(zé)任編輯:book05 來(lái)源: csdn
相關(guān)推薦

2010-09-28 08:52:00

C#Visual Stud

2009-08-27 16:54:59

C#開(kāi)發(fā)技巧

2021-10-14 07:55:17

提示技巧C#

2009-08-10 13:05:06

C# DLLC# Delphi開(kāi)發(fā)

2009-09-11 11:33:58

C# WinForm控Attribute

2009-08-17 09:39:40

C# Windows

2021-01-20 05:53:25

C# ValueTupleTuple

2009-08-18 14:29:27

DirectSound

2024-03-19 14:41:08

C#操作符開(kāi)發(fā)

2009-08-07 18:07:58

C#數(shù)據(jù)庫(kù)開(kāi)發(fā)

2009-08-24 16:11:35

C#項(xiàng)目開(kāi)發(fā)

2011-06-08 10:06:32

C#

2024-08-06 12:35:42

C#代碼重構(gòu)

2009-08-11 15:44:05

C#基本技巧

2015-07-27 09:36:09

storyboard

2024-02-28 08:06:17

2009-08-07 10:53:59

Visual C#Excel

2020-05-22 07:00:00

C#用戶注釋編程語(yǔ)言

2020-07-15 14:51:39

代碼C+開(kāi)發(fā)

2009-08-12 14:01:17

C# Excel編程技
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)