Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication5
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- // step 1 . using System.Data.SqlClient;
- // step 2 . 宣告連接字串
- 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;";
- // step 3 . 建立SqlConnection
- SqlConnection conn = new SqlConnection(Constr);
- // step 4 . 宣告查詢字串
- string Sqlstr = "select * from Student_Info";
- // step 5. 建立SqlDataAdapter
- SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
- // step 6. 建立DataSet來儲存Table
- DataSet ds = new DataSet();
- // step 7. 將DataAdapter查詢之後的結果,填充至DataSet
- da.Fill(ds);
- // step 8 . 用DataGridView1 顯示出來
- //this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
- dataGridView1.DataSource = ds.Tables[0].DefaultView;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment