往MySQL中插入資料
阿新 • • 發佈:2019-01-07
(1)使用ASP.NET畫出如下介面
(2)圖一的程式碼如下
using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace test { public partial class New2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void queding_Click(object sender, EventArgs e) { using (MySqlConnection sc = new MySqlConnection("Database=Student;Data Source=localhost;Port=3306;User Id=root;Password=;Charset=utf8;TreatTinyAsBoolean=false;")) { sc.Open(); MySqlCommand cmd = new MySqlCommand("insert into Login values('" + txtUserName.Text.Trim() + "','"+txtPassword.Text.Trim()+"')",sc); cmd.ExecuteNonQuery(); } } } }
(3)wep的配置檔案程式碼如下
<?xml version="1.0" encoding="utf-8"?> <!-- 有關如何配置 ASP.NET 應用程式的詳細資訊,請訪問 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.6.1"/> <httpRuntime targetFramework="4.6.1"/> <pages> <namespaces> <add namespace="System.Web.Optimization"/> </namespaces> <controls> <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/> </controls> </pages> </system.web> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f"/> <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"/> <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51"/> <bindingRedirect oldVersion="0.0.0.0-4.0.2.1" newVersion="4.0.2.1"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/> </dependentAssembly> </assemblyBinding> </runtime> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> </compilers> </system.codedom> </configuration>
(4)如果不會配置的話,可以訪問這個網址: https://go.microsoft.com/fwlink/?LinkId=169433
(5)MySQL的程式碼
CREATE database login
use login
CREATE TABLE login
(
UserName varachar(12),
Password varchar(12)
)
insert into login VALUES('UserName','Password')
SELECT *FROM login
(6)最後查詢資料庫的顯示結果
(7)寫完收工,希望能夠幫到有困惑的程式設計愛好者。