{"id":147781,"date":"2024-11-01T13:15:21","date_gmt":"2024-11-01T05:15:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/147781\/"},"modified":"2024-11-01T13:15:22","modified_gmt":"2024-11-01T05:15:22","slug":"wpf%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e7%95%8c%e9%9d%a2%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%a4%e4%ba%92%e5%af%a6%e7%8f%be-wpf%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e7%95%8c%e9%9d%a2%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/147781\/","title":{"rendered":"WPF\u7528\u6236\u767b\u9304\u754c\u9762\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u5be6\u73fe (wpf\u7528\u6236\u767b\u9304\u754c\u9762\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"wpf%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e7%95%8c%e9%9d%a2%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%a4%e4%ba%92%e5%af%a6%e7%8f%be-PgBhQPcDDm\">WPF\u7528\u6236\u767b\u9304\u754c\u9762\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u5be6\u73fe<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u7528\u6236\u767b\u9304\u754c\u9762\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002Windows Presentation Foundation\uff08WPF\uff09\u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u684c\u9762\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u6280\u8853\uff0c\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u7528\u6236\u754c\u9762\u5143\u7d20\u548c\u6578\u64da\u7d81\u5b9a\u529f\u80fd\uff0c\u4f7f\u5f97\u958b\u767c\u7528\u6236\u767b\u9304\u754c\u9762\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u548c\u9ad8\u6548\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528WPF\u5be6\u73fe\u7528\u6236\u767b\u9304\u754c\u9762\uff0c\u4e26\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<h2 id=\"wpf%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e7%95%8c%e9%9d%a2%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%b5%84%e6%88%90-PgBhQPcDDm\">WPF\u7528\u6236\u767b\u9304\u754c\u9762\u7684\u57fa\u672c\u7d44\u6210<\/h2>\n<p>\u4e00\u500b\u5178\u578b\u7684\u7528\u6236\u767b\u9304\u754c\u9762\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u5143\u7d20\uff1a<\/p>\n<ul>\n<li>\u7528\u6236\u540d\u8f38\u5165\u6846<\/li>\n<li>\u5bc6\u78bc\u8f38\u5165\u6846<\/li>\n<li>\u767b\u9304\u6309\u9215<\/li>\n<li>\u932f\u8aa4\u63d0\u793a\u4fe1\u606f<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684WPF\u767b\u9304\u754c\u9762\u7684XAML\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;Window x:Class=\"LoginApp.MainWindow\"\n        xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n        Title=\"\u7528\u6236\u767b\u9304\" Height=\"200\" Width=\"300\"&gt;\n    &lt;Grid&gt;\n        &lt;Label Content=\"\u7528\u6236\u540d:\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Margin=\"10,10,0,0\"\/&gt;\n        &lt;TextBox Name=\"UsernameTextBox\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Margin=\"80,10,0,0\" Width=\"200\"\/&gt;\n        \n        &lt;Label Content=\"\u5bc6\u78bc:\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Margin=\"10,50,0,0\"\/&gt;\n        &lt;PasswordBox Name=\"PasswordBox\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Margin=\"80,50,0,0\" Width=\"200\"\/&gt;\n        \n        &lt;Button Content=\"\u767b\u9304\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Margin=\"100,100,0,0\" Width=\"100\" Click=\"LoginButton_Click\"\/&gt;\n        &lt;TextBlock Name=\"ErrorMessage\" Foreground=\"Red\" HorizontalAlignment=\"Left\" VerticalAlignment=\"Top\" Margin=\"10,130,0,0\"\/&gt;\n    &lt;\/Grid&gt;\n&lt;\/Window&gt;<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%a4%e4%ba%92%e7%9a%84%e5%af%a6%e7%8f%be-PgBhQPcDDm\">\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u5be6\u73fe<\/h2>\n<p>\u70ba\u4e86\u5be6\u73fe\u7528\u6236\u767b\u9304\u529f\u80fd\uff0c\u6211\u5011\u9700\u8981\u5c07\u7528\u6236\u8f38\u5165\u7684\u6578\u64da\u8207\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u9032\u884c\u6bd4\u5c0d\u3002\u9019\u88e1\u6211\u5011\u5c07\u4f7f\u7528SQL Server\u4f5c\u70ba\u6578\u64da\u5eab\uff0c\u4e26\u4f7f\u7528ADO.NET\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n<h3 id=\"%e8%a8%ad%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab-PgBhQPcDDm\">\u8a2d\u7f6e\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728SQL Server\u4e2d\u5275\u5efa\u4e00\u500b\u7528\u6236\u8868\uff0c\u8a72\u8868\u5305\u542b\u7528\u6236\u540d\u548c\u5bc6\u78bc\u5b57\u6bb5\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u7528\u6236\u8868\u7684SQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>CREATE TABLE Users (\n    Id INT PRIMARY KEY IDENTITY,\n    Username NVARCHAR(50) NOT NULL,\n    Password NVARCHAR(50) NOT NULL\n);<\/code><\/pre>\n<h3 id=\"%e7%99%bb%e9%8c%84%e9%82%8f%e8%bc%af%e7%9a%84%e5%af%a6%e7%8f%be-PgBhQPcDDm\">\u767b\u9304\u908f\u8f2f\u7684\u5be6\u73fe<\/h3>\n<p>\u5728WPF\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5728\u767b\u9304\u6309\u9215\u7684\u4e8b\u4ef6\u8655\u7406\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u908f\u8f2f\u3002\u4ee5\u4e0b\u662f\u767b\u9304\u6309\u9215\u7684\u4e8b\u4ef6\u8655\u7406\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>private void LoginButton_Click(object sender, RoutedEventArgs e)\n{\n    string username = UsernameTextBox.Text;\n    string password = PasswordBox.Password;\n\n    using (SqlConnection connection = new SqlConnection(\"your_connection_string\"))\n    {\n        connection.Open();\n        string query = \"SELECT COUNT(*) FROM Users WHERE Username = @username AND Password = @password\";\n        SqlCommand command = new SqlCommand(query, connection);\n        command.Parameters.AddWithValue(\"@username\", username);\n        command.Parameters.AddWithValue(\"@password\", password);\n\n        int result = (int)command.ExecuteScalar();\n        if (result &gt; 0)\n        {\n            MessageBox.Show(\"\u767b\u9304\u6210\u529f\uff01\");\n        }\n        else\n        {\n            ErrorMessage.Text = \"\u7528\u6236\u540d\u6216\u5bc6\u78bc\u932f\u8aa4\uff01\";\n        }\n    }\n}<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-PgBhQPcDDm\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u5b58\u5132\u7528\u6236\u5bc6\u78bc\u6642\u61c9\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u9032\u884c\u52a0\u5bc6\uff0c\u800c\u4e0d\u662f\u4ee5\u660e\u6587\u5f62\u5f0f\u5b58\u5132\u3002\u6b64\u5916\uff0c\u61c9\u8003\u616e\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PgBhQPcDDm\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528WPF\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684\u7528\u6236\u767b\u9304\u754c\u9762\uff0c\u4e26\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u901a\u904e\u4f7f\u7528ADO.NET\uff0c\u6211\u5011\u80fd\u5920\u6709\u6548\u5730\u9a57\u8b49\u7528\u6236\u7684\u767b\u9304\u4fe1\u606f\u3002\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u958b\u767c\u8005\u61c9\u8a72\u63a1\u53d6\u9069\u7576\u7684\u63aa\u65bd\u4f86\u4fdd\u8b77\u7528\u6236\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728WPF\u4e2d\u5be6\u73fe\u7528\u6236\u767b\u9304\u754c\u9762\uff0c\u4e26\u8207\u6578\u64da\u5eab\u9032\u884c\u9ad8\u6548\u4ea4\u4e92\uff0c\u78ba\u4fdd\u5b89\u5168\u6027\u8207\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-147781","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/147781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=147781"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/147781\/revisions"}],"predecessor-version":[{"id":147782,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/147781\/revisions\/147782"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=147781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=147781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=147781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}