国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

adonet基礎示例分享(adonet連接數據庫)

2020-01-24 02:43:52
字體:
來源:轉載
供稿:網友
adonet基礎示例分享
復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;

namespace ADONET基礎 {
    /// <summary>
    /// Window1.xaml 的交互邏輯
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ////using() 自動關閉數據庫,回收資源。
            ////SqlConnection為建立和數據庫連接的對象。
            //using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myself;User Id=sa;Password=123;"))
            //{
            //    conn.Open();//打開連接
            //    //通過連接,創建一個向數據庫發命令的對象SqlCommand
            //    using (SqlCommand cmd = conn.CreateCommand())//釋放資源。
            //    {
            //        //CommandText為要執行的SQL的語句
            //        cmd.CommandText = "Insert into student(學號,姓名) values(110,'張五')";
            //        //ExecuteNonQuery一般用來執行Update Delete Insert 語句。
            //        cmd.ExecuteNonQuery();//執行上面的SQL語句。
            //    }
            //}

            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "select count(*) from student where 入學成績<570";
                    //cmd.CommandText = "select count(*) from student where 入學成績<570";
                    ////ExecuteScalar一般用來執行有且只有一行一列返回值的SQL語句。
                    //int i = (int)cmd.ExecuteScalar();
                    //MessageBox.Show(i+"人成績小于570分");
                    cmd.CommandText = "Insert into T_Student(Name,Age) output inserted.Id values('張顧',18);";
                    long i = (long)cmd.ExecuteScalar();
                    MessageBox.Show("Id為"+i);
                }
            }
            MessageBox.Show("執行完成");
        }
    }
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘泉县| 韶山市| 安阳市| 晋中市| 滕州市| 邯郸县| 柏乡县| 都江堰市| 蕲春县| 麻阳| 沭阳县| 乐至县| 增城市| 德阳市| 曲靖市| 田东县| 肥东县| 南靖县| 海兴县| 上思县| 区。| 宁国市| 昌图县| 宿松县| 长丰县| 太仆寺旗| 泰顺县| 永宁县| 同仁县| 元江| 马鞍山市| 全南县| 晋中市| 乌拉特中旗| 彭阳县| 黄石市| 孝感市| 丹凤县| 迁安市| 南通市| 黄浦区|