{"id":147779,"date":"2024-11-01T13:14:21","date_gmt":"2024-11-01T05:14:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/147779\/"},"modified":"2024-11-01T13:14:21","modified_gmt":"2024-11-01T05:14:21","slug":"wpf%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab-wpf%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/147779\/","title":{"rendered":"WPF\u5982\u4f55\u5be6\u73fe\u4fee\u6539\u6578\u64da\u5eab (wpf\u4fee\u6539\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"wpf%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab-ZvmxGTGKNO\">WPF\u5982\u4f55\u5be6\u73fe\u4fee\u6539\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0cWindows Presentation Foundation\uff08WPF\uff09\u662f\u4e00\u500b\u5f37\u5927\u7684\u6846\u67b6\uff0c\u5c08\u9580\u7528\u65bc\u69cb\u5efa\u684c\u9762\u61c9\u7528\u7a0b\u5e8f\u3002WPF\u4e0d\u50c5\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u7528\u6236\u754c\u9762\u529f\u80fd\uff0c\u9084\u80fd\u5920\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728WPF\u61c9\u7528\u7a0b\u5e8f\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u7684\u4fee\u6539\u64cd\u4f5c\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"wpf%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-ZvmxGTGKNO\">WPF\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u5728\u958b\u59cb\u4fee\u6539\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002WPF\u901a\u5e38\u4f7f\u7528ADO.NET\u6216Entity Framework\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528ADO.NET\u9023\u63a5SQL Server\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>using System.Data.SqlClient;\n\n\/\/ \u5b9a\u7fa9\u9023\u63a5\u5b57\u7b26\u4e32\nstring connectionString = \"Server=your_server;Database=your_database;User Id=your_username;Password=your_password;\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\u5c0d\u8c61\nusing (SqlConnection connection = new SqlConnection(connectionString))\n{\n    connection.Open();\n    \/\/ \u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n}\n<\/code><\/pre>\n<h2 id=\"%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c-ZvmxGTGKNO\">\u4fee\u6539\u6578\u64da\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<p>\u4e00\u65e6\u5efa\u7acb\u4e86\u9023\u63a5\uff0c\u5c31\u53ef\u4ee5\u9032\u884c\u6578\u64da\u7684\u4fee\u6539\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528SQL UPDATE\u8a9e\u53e5\u4f86\u4fee\u6539\u6578\u64da\uff1a<\/p>\n<pre><code>string updateQuery = \"UPDATE YourTable SET ColumnName = @Value WHERE Id = @Id\";\n\nusing (SqlCommand command = new SqlCommand(updateQuery, connection))\n{\n    command.Parameters.AddWithValue(\"@Value\", newValue);\n    command.Parameters.AddWithValue(\"@Id\", recordId);\n    \n    int rowsAffected = command.ExecuteNonQuery();\n    if (rowsAffected &gt; 0)\n    {\n        Console.WriteLine(\"\u6578\u64da\u4fee\u6539\u6210\u529f\uff01\");\n    }\n    else\n    {\n        Console.WriteLine(\"\u672a\u627e\u5230\u5339\u914d\u7684\u8a18\u9304\u3002\");\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8entity-framework%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e4%bf%ae%e6%94%b9-ZvmxGTGKNO\">\u4f7f\u7528Entity Framework\u9032\u884c\u6578\u64da\u4fee\u6539<\/h2>\n<p>\u9664\u4e86ADO.NET\uff0cEntity Framework\uff08EF\uff09\u4e5f\u662f\u4e00\u500b\u6d41\u884c\u7684\u9078\u64c7\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u9032\u884c\u8907\u96dc\u6578\u64da\u64cd\u4f5c\u6642\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528EF\u9032\u884c\u6578\u64da\u4fee\u6539\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>using (var context = new YourDbContext())\n{\n    var record = context.YourTable.Find(recordId);\n    if (record != null)\n    {\n        record.ColumnName = newValue;\n        context.SaveChanges();\n        Console.WriteLine(\"\u6578\u64da\u4fee\u6539\u6210\u529f\uff01\");\n    }\n    else\n    {\n        Console.WriteLine(\"\u672a\u627e\u5230\u5339\u914d\u7684\u8a18\u9304\u3002\");\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e8%99%95%e7%90%86%e7%95%b0%e5%b8%b8-ZvmxGTGKNO\">\u8655\u7406\u7570\u5e38<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u8655\u7406\u7570\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f\u4f7f\u7528ADO.NET\u9084\u662fEntity Framework\uff0c\u90fd\u61c9\u8a72\u4f7f\u7528try-catch\u584a\u4f86\u6355\u7372\u53ef\u80fd\u767c\u751f\u7684\u7570\u5e38\uff1a<\/p>\n<pre><code>try\n{\n    \/\/ \u6578\u64da\u5eab\u64cd\u4f5c\u4ee3\u78bc\n}\ncatch (SqlException ex)\n{\n    Console.WriteLine(\"\u6578\u64da\u5eab\u932f\u8aa4: \" + ex.Message);\n}\ncatch (Exception ex)\n{\n    Console.WriteLine(\"\u5176\u4ed6\u932f\u8aa4: \" + ex.Message);\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ZvmxGTGKNO\">\u7e3d\u7d50<\/h2>\n<p>\u5728WPF\u61c9\u7528\u7a0b\u5e8f\u4e2d\u4fee\u6539\u6578\u64da\u5eab\u662f\u4e00\u500b\u76f8\u5c0d\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u7121\u8ad6\u662f\u4f7f\u7528ADO.NET\u9084\u662fEntity Framework\uff0c\u90fd\u80fd\u5920\u6709\u6548\u5730\u5be6\u73fe\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\u3002\u901a\u904e\u6b63\u78ba\u7684\u9023\u63a5\u5b57\u7b26\u4e32\u3001SQL\u8a9e\u53e5\u6216LINQ\u67e5\u8a62\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u548c\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684WPF\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528WPF\u5be6\u73fe\u6578\u64da\u5eab\u4fee\u6539\uff0c\u5305\u62ec\u6578\u64da\u7d81\u5b9a\u3001\u547d\u4ee4\u548cEntity Framework\u7684\u61c9\u7528\u6280\u5de7\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-147779","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/147779","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=147779"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/147779\/revisions"}],"predecessor-version":[{"id":147780,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/147779\/revisions\/147780"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=147779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=147779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=147779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}