ak77now

Untitled

Aug 23rd, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication5
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             try
  24.             {
  25.                 // step 1 . using System.Data.SqlClient;
  26.  
  27.                 // step 2 . 宣告連接字串
  28.  
  29.                 string Constr = @"Server=tcp:iii89.database.windows.net,1433;Initial Catalog=外部連線測試資料庫;Persist Security Info=False;User ID=iii89;Password=Aa123456;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
  30.  
  31.                 // step 3 . 建立SqlConnection
  32.                 SqlConnection conn = new SqlConnection(Constr);
  33.  
  34.                 // step 4 . 宣告查詢字串
  35.                 string Sqlstr = "select * from Student_Info";
  36.  
  37.                 // step 5. 建立SqlDataAdapter
  38.                 SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
  39.  
  40.                 // step 6. 建立DataSet來儲存Table
  41.                 DataSet ds = new DataSet();
  42.  
  43.                 // step 7. 將DataAdapter查詢之後的結果,填充至DataSet
  44.                 da.Fill(ds);
  45.  
  46.                 // step 8 . 用DataGridView1 顯示出來
  47.                 //this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
  48.                 dataGridView1.DataSource = ds.Tables[0].DefaultView;
  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                 MessageBox.Show(ex.Message);
  53.             }
  54.         }
  55.  
  56.         private void Form1_Load(object sender, EventArgs e)
  57.         {
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment