學點兒c#語言wpf開發

首發:我的博客,更新&糾錯&回覆java

visual studio 2015,界面愈來愈漂亮了。python

比起swift和python啥的,仍是c#外觀上更像java,windows的界面編程,wpf的方式跟android等編程也蠻像,聲明式的界面定義,界面和controller類固然綁定,總之玩着很爽就是了。android

如今在想的問題是想個啥主題作着……web

下面的代碼是學習關鍵點時練的,老規矩,界面交互+web請求+數據庫訪問——數據庫

using MySql.Data.MySqlClient;
using qdf_test_wpf_1;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
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;編程

namespace WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }json

        private void button_Click(object sender, RoutedEventArgs e)
        {swift

            //進行web請求
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://427studio.net/api/blog");
            httpRequest.Timeout = 2000;
            httpRequest.Method = "GET";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
            string result = sr.ReadToEnd();
            result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            int status = (int)httpResponse.StatusCode;
            sr.Close();c#

            //解析json
            List<Person> ps = JSON.parse<List<Person>>(result);
            foreach (Person p in ps)
            {
                Console.WriteLine(p.title);
            }
            jsonCount.Content = "js數組長度:" + ps.Count();windows


            //數據庫鏈接
            string str = "Server=那啥;User ID=那啥;Password=那啥;Database=那啥;CharSet=那啥;";
            MySqlConnection conn = new MySqlConnection(str);//實例化連接
            conn.Open();//開啓鏈接
            string strcmd = "select id, name from admin_user";
            MySqlCommand cmd = new MySqlCommand(strcmd, conn);
            MySqlDataReader reader = cmd.ExecuteReader();
            int rowCount = 0;
            while (reader.Read())
            {
                String title = reader.GetString("name");
                //Console.WriteLine(title);

                rowCount++;
            }
            dbCount.Content = "數據庫記錄行數:" + rowCount;
            reader.Close();
            conn.Close();


            //文件系統
            FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("由wpf程序寫入!");
            sw.Close();
        }
    }
}

長期歡迎項目合做機會介紹,項目收入10%用於酬謝介紹人。新浪微博:@冷鏡,QQ:908789432

相關文章
相關標籤/搜索