C#winform中資料庫的串連

來源:互聯網
上載者:User

標籤:click   command   level   oca   串連   bsp   form   win   密碼   

一、串連VS2013內建的本機資料庫

using System.Data.SqlClient;

首先定義一個連接字串

 public static string ConnectString = "Server=(localdb)\\Projects;Initial Catalog=Test;Integrated Security=true";

其中:Server是伺服器名

   Ilitial Catalog是資料庫名 

   Integrated Security是整合驗證,使用Windows驗證的方式去串連到資料庫伺服器。不需要在連接字串中編寫使用者名稱和密碼,從一定程度上說提高了安全性。(該參數為false時或省略該項時,按照Userid和password來串連,設為SSPI時相當於true。)

 using (SqlConnection conn = new SqlConnection(ConnectString))             {                 string sql = "select password,level from [Test].[dbo].[User] where userid=‘" + username + "‘";                 using (SqlCommand cmd = new SqlCommand(sql, conn))                 {                     conn.Open();                     using (SqlDataReader sdr = cmd.ExecuteReader())                     {                         if(sdr.Read())                         {                             //則將對應該使用者名稱下的 第一個欄位 即使密碼(select的第一個欄位) 賦給 字串pwd,並且依次往後讀取 所有的密碼                             //Trim()方法為移除字串前後的空白                             string pwd = sdr.GetString(0).Trim();                             //讀取器sdr擷取了2列資料 第1列為密碼索引為0 第2列即索引為1的是使用者類型                             string uType = sdr.GetString(1).Trim();                             if (pwd == password)                             {                                 //擷取登陸成功後的使用者ID                                Uid = username;                                UserType = uType;                                if(UserType=="普通使用者")                                 {                                     this.Hide();                                    User muser = new User();                                     muser.ShowDialog();                                                            }                                       else if(UserType== "進階使用者")                                 {                                     this.Hide();                                     VipUser mvuser = new VipUser();                                     mvuser.ShowDialog();                                                                     }                                 else if(UserType=="管理員")                                 {                                     this.Hide();                                     Admimistor madministor = new Admimistor();                                     madministor.ShowDialog();                                                                   }                                                                                              }

SqlConnection:資料庫連接類

SqlCommand:資料庫操作類

SqlDataReader:讀取

二、對資料庫中的資料進行增、刪、查、改(在DataGridview中)

     SqlDataAdapter adp = null;     DataSet ds = null;                private void Admimistor_Load(object sender, EventArgs e)        {                adp = new SqlDataAdapter("select * from [Test].[dbo].[User]", Login.ConnectString);//執行個體化橋接器                      ds = new DataSet();//執行個體化資料集                       adp.Fill(ds);                    dataGridView1.DataSource = ds.Tables[0];                              }        //向表中編輯或添加資料        private void button1_Click(object sender, EventArgs e)        {            SqlCommandBuilder scb = new SqlCommandBuilder(adp);            //更新資料              try            {                //這裡是關鍵                  adp.Update(ds);            }            catch (SqlException ex)            {                MessageBox.Show(ex.Message);            }        }                     //刪除表中的選中行        private void button3_Click(object sender, EventArgs e)        {            foreach (DataGridViewRow dr in dataGridView1.SelectedRows)            {                MessageBox.Show("確認刪除嗎?");                    dataGridView1.Rows.Remove(dr);//刪除一條記錄            }        }

SqlDataAdapter是 DataSet和 SQL Server之間的橋接器,用於檢索和儲存資料。SqlDataAdapter通過對資料來源使用適當的Transact-SQL語句映射 Fill(它可更改DataSet中的資料以匹配資料來源中的資料)和 Update(它可更改資料來源中的資料以匹配 DataSet中的資料)來提供這一橋接。當SqlDataAdapter填充 DataSet時,它為返回的資料建立必需的表和列(如果這些表和列尚不存在)。


DataSet當成記憶體中的資料庫,DataSet是不依賴於資料庫的獨立資料集合。所謂獨立,就是說,即使斷開資料鏈路,或者關閉資料庫,DataSet依然是可用的,DataSet在內部是用XML來描述資料的,由於XML是一種與平台無關、與語言無關的資料描述語言 (Data Description Language),而且可以描述複雜關係的資料,比如父子關係的資料,所以DataSet實際上可以容納具有複雜關係的資料,而且不再依賴於資料庫鏈路。

部分引用自:

1.C#中SqlDataAdapter的使用小結 - CSDN部落格
http://blog.csdn.net/nicholaseenzhu/article/details/70417407

2.360百科

 

 

   


 

C#winform中資料庫的串連

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.